Asp.net mvc EF4.1 DbContext 和服务层

发布于 2024-11-28 10:12:54 字数 741 浏览 0 评论 0原文

在服务层中公开 DbContext 是一种不好的做法吗?

例如:

private readonly IRepository<SkillLevels> _repository;
private readonly IDatabaseFactory _databaseFactory;
private readonly IUnitOfWork _unitOfWork;

public SkillLevelService(IRepository<SkillLevels> repository, IDatabaseFactory databaseFactory, IUnitOfWork unitOfWork)
{
    _repository = repository;
    _databaseFactory = databaseFactory;
    _unitOfWork = unitOfWork;
}

public void InsertSkillLevel(SkillLevels entity)
{
    _repository.Insert(entity);
    _unitOfWork.Commit();
}

在这里,IDatabaseFactory 可以返回我的 DbContext 对象。我在这里公开它是因为如果我需要技能级别以外的其他实体,以便我可以在这个服务类中使用它们。如果您知道,对于复杂查询,当您需要选择多个表来获取数据时。

但我不确定这是否是一种不好的做法!

任何帮助表示赞赏。谢谢。

Is it a bad practice to expose DbContext in your service layer?

For Example:

private readonly IRepository<SkillLevels> _repository;
private readonly IDatabaseFactory _databaseFactory;
private readonly IUnitOfWork _unitOfWork;

public SkillLevelService(IRepository<SkillLevels> repository, IDatabaseFactory databaseFactory, IUnitOfWork unitOfWork)
{
    _repository = repository;
    _databaseFactory = databaseFactory;
    _unitOfWork = unitOfWork;
}

public void InsertSkillLevel(SkillLevels entity)
{
    _repository.Insert(entity);
    _unitOfWork.Commit();
}

Here, IDatabaseFactory can return my DbContext object. I exposed it here because if i need other entities other than SkillLevels, so that i can use them in this service class. In case you know, for complex queries when you need to select multiple tables to fetch data.

But i'm not sure if it makes a bad practice!!

Any help is appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下雨或天晴 2024-12-05 10:12:54

我看到的一个问题是您正在使用通用存储库。它可以适用于简单的情况,但正如您所说,如果您需要进行连接,则这种方法将不起作用。因此,一项改进是创建特定的存储库。

public interface ISkillLevelsRepository : IRepository<SkillLevels>
{
  //have specific methods to achieve what you want
}

另一种方法是在服务类中使用多个存储库并删除 Factory

private readonly IRepository<SkillLevels> _skillsRepository;
private readonly IRepository<Student> _studentRepository;
private readonly IUnitOfWork _unitOfWork;

public SkillLevelService(IRepository<SkillLevels> skillsRepository, IRepository<Student> studentRepository, IUnitOfWork unitOfWork)
{
    _skillsRepository= skillsRepository;
    _studentRepository = studentRepository;
    _unitOfWork = unitOfWork;
}

然后访问多个存储库来实现您所需要的。

另一件事是,如果您已使用所有导航字段正确定义了模型,则您可能很少需要执行联接,因为 EF 将在您引用导航属性时添加联接。

One issue I see is you are using a generic repository. It can work for simple cases but as you said if you need to do joins this approach is not going to work. So one improvement would be to create specific repos

public interface ISkillLevelsRepository : IRepository<SkillLevels>
{
  //have specific methods to achieve what you want
}

Another approach would be to use multiple repositories inside your service class and remove the Factory.

private readonly IRepository<SkillLevels> _skillsRepository;
private readonly IRepository<Student> _studentRepository;
private readonly IUnitOfWork _unitOfWork;

public SkillLevelService(IRepository<SkillLevels> skillsRepository, IRepository<Student> studentRepository, IUnitOfWork unitOfWork)
{
    _skillsRepository= skillsRepository;
    _studentRepository = studentRepository;
    _unitOfWork = unitOfWork;
}

Then access multiple repos to achieve what you need.

One more thing if you have defined your model correctly with all navigational fields you may rarely need to do joins because EF will add the joins when you refer the navigational properties.

懒猫 2024-12-05 10:12:54

在 Web 应用程序中,每个请求仅使用一个 DBContext 并在 BeginRequest 上创建它,然后将其传递给请求中的所有调用并在 EndRequest 上处置它是很常见的。

请参阅此示例 ASP 中的每个请求一个 DbContext。 NET MVC(没有 IOC 容器)

It is quite common in a web application to just use a single DBContext per request and create it on BeginRequest then pass it to all the calls in the request and dispose it on EndRequest.

See this example One DbContext per request in ASP.NET MVC (without IOC container)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文