实体框架最佳实践业务逻辑? [英] Entity Framework Best Practices In Business Logic?

查看:257
本文介绍了实体框架最佳实践业务逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的是实体框架是第一次,想知道如果我使用的最佳实践。

I am using the Entity framework for the first time, and would like to know if I am using in the best practice.

我在我的业务逻辑,将处理实体上下文中创建一个单独的类。这个问题我有,是我见过他们通常包裹上下文using语句,以确保其关闭了所有的视频​​,但很明显,我不能在我的业务逻辑做到这一点的情况下将被关闭之前,其实我可以用它?

I have created a separate class in my business logic which will handle the entity context. the problem I have, is in all the videos I have seen they usually wrap the context in a using statement to make sure its closed, but obviously I can't do this in my business logic as the context will be closed before I can actually use it?

所以,这是正确的我在做什么?一对夫妇的例子:

So is this ok what I'm doing? A couple of examples:

    public IEnumerable<Article> GetLatestArticles(bool Authorised) 
    {
        var ctx = new ArticleNetEntities();
        return ctx.Articles.Where(x => x.IsApproved == Authorised).OrderBy(x => x.ArticleDate);
    }

    public IEnumerable<Article> GetArticlesByMember(int MemberId, bool Authorised)
    {
        var ctx = new ArticleNetEntities();
        return ctx.Articles.Where(x => x.MemberID == MemberId && x.IsApproved == Authorised).OrderBy(x => x.ArticleDate);
    }

我只是想确保我没有建立这回事时,很多人用它死的东西吗?

I just want to make sure I'm not building something that's going to die when a lot of people use it?

推荐答案

这实际上取决于如何要公开你的资料库/数据存储。

It really depends on how to want to expose your repository/data store.

不知道你的意思是什么的背景下将被关闭,所以我不能做商业逻辑。里面做的using语句你的业务逻辑的。或者,如果你的业务逻辑是不同的类,然后让我们继续。 :)

Not sure what you mean by "the context will be closed, therefore i cannot do business logic". Do your business logic inside the using statement. Or if your business logic is in a different class, then let's continue. :)

有些人从他们的仓库返回具体的集合,在这种情况下,你可以用在using语句上下文:

Some people return concrete collections from their Repository, in which case you can wrap the context in the using statement:

public class ArticleRepository
{
   public List<Article> GetArticles()
   {
      List<Article> articles = null;

      using (var db = new ArticleNetEntities())
      {
         articles = db.Articles.Where(something).Take(some).ToList();
      }
   }
}

这种优势在满足与连接好习惯 - 打开迟就可以了,早可以关闭。

Advantage of that is satisfying the good practice with connections - open as late as you can, and close as early as you can.

您可以封装你所有的业务逻辑using语句中。

You can encapsulate all your business logic inside the using statement.

缺点 - 你的信息库意识到业务逻辑,我个人不喜欢,你最终为每个特定的场景不同的方法

The disadvantages - your Repository becomes aware of business-logic, which i personally do not like, and you end up with a different method for each particular scenario.

第二个选项 - 新出现一个上下文作为存储库的一部分,并使其实现IDisposable

The second option - new up a context as part of the Repository, and make it implement IDisposable.

public class ArticleRepository : IDisposable
{
   ArticleNetEntities db;

   public ArticleRepository()
   {
      db = new ArticleNetEntities();
   }

   public List<Article> GetArticles()
   {
      List<Article> articles = null;
      db.Articles.Where(something).Take(some).ToList();
   }

   public void Dispose()
   {
      db.Dispose();
   }

}

和则:

using (var repository = new ArticleRepository())
{
   var articles = repository.GetArticles();
}

第三个选项(我的最爱),使用依赖注入。从库去耦所有的上下文工作,让DI容器手柄支配的资源:

Or the third-option (my favourite), use dependency injection. Decouple all the context-work from your Repository, and let the DI container handle disposal of resources:

public class ArticleRepository
{
   private IObjectContext _ctx;

   public ArticleRepository(IObjectContext ctx)
   {
      _ctx = ctx;
   }

   public IQueryable<Article> Find()
   {
      return _ctx.Articles;
   }
}

您选择DI容器将注入混凝土的ObjectContext到存储库中的实例,用配置的生命周期(辛格尔顿,HttpContext的,ThreadLocal的,等等),并基于该配置处理它。

Your chosen DI container will inject the concrete ObjectContext into the instantiation of the Repository, with a configured lifetime (Singleton, HttpContext, ThreadLocal, etc), and dispose of it based on that configuration.

我有它设置,使每个HTTP请求被赋予了新的语境。当请求完成后,我的DI容器会自动处理的上下文。

I have it setup so each HTTP Request gets given a new Context. When the Request is finished, my DI container will automatically dispose of the context.

我也用工作模式的单位在这里允许多个库与一个对象上下文工作。

I also use the Unit of Work pattern here to allow multiple Repositories to work with one Object Context.

您可能也注意到我preFER从我的仓库返回的IQueryable(而不是一个具体的列表)。更强大的(但有风险的,如果你不理解的含义)。我的服务层执行对IQueryable的业务逻辑,然后具体的集合返回到用户界面。

You may have also noticed I prefer to return IQueryable from my Repository (as opposed to a concrete List). Much more powerful (yet risky, if you don't understand the implications). My service layer performs the business logic on the IQueryable and then returns the concrete collection to the UI.

这是我目前为止最强大的功能,因为它允许一个简单的赫克库的工作单元管理的背景下,服务层管理业务逻辑,而DI容器处理寿命/处置资源/对象

That is my far the most powerful option, as it allows a simple as heck Repository, the Unit Of Work manages the context, the Service Layer manages the Business Logic, and the DI container handles the lifetime/disposal of resources/objects.

让我知道,如果你想在更多的信息 - 因为有相当多的吧,比这令人惊讶的长的答案,甚至更多。 :)

Let me know if you want more info on that - as there is quite a lot to it, even more than this surprisingly long answer. :)

这篇关于实体框架最佳实践业务逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆