Entity Framework Best Practices In Business Logic?

前端 未结 5 532
北恋
北恋 2020-12-12 11:46

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

I have created a separate class in my business logic

5条回答
  •  借酒劲吻你
    2020-12-12 12:27

    What you can also do is store your context at a higher level.

    E.g., you can have a static class storing the current context:

    class ContextManager
    {
        [ThreadStatic]
        public static ArticleEntities CurrentContext;
    }
    

    Then, somewhere outside you do something like this:

    using (ContextManager.CurrentContext = new ArticleEntities())
    {
        IEnumerable
    article = articleService.GetLatestArticles(true); }

    Then, inside the GetLastestArticles, you just use the same ContextManager.CurrentContext.

    Of course, this is just the basic idea. You can make this a lot more workable by using service providers, IoC and such.

提交回复
热议问题