2013-09-23 75 views
0

谷歌搜索一段時間後,我想知道哪些是DBContext(EntityFramework或Linq-to-Sql)的最佳實踐。ASP DBContext最佳實踐

在實踐中我會騙知道下面「模式」一個有少缺點:

1)獲取代碼從Here

public class ContextFactory 
{ 
    [ThreadStatic] 
    private static DBDataContext context; 
    //Get connectionString from web.config 
    private static readonly string connString = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString; 

    public static DBDataContext Context() 
    { 
     if (context == null) 
      context = new DBDataContext(connString); 
     return context; 
    } 

    public static void FlushContext() 
    { 
     context = new DBSkillDataContext(connString); 
    } 
} 

這樣我使用FlushContext每次控制器是初始化。

2)這樣(從Here獲取代碼)

public class UnitOfWork : IUnitOfWork, IDisposable 
    { 
    DBContext context= null; 
    IUserRepository userRepo = null; 
    IAccountRepository accountRepo = null; 

    public UnitOfWork() 
    { 
     context= new DBContext(); 
     userRepo= new UserRepository(context); 
     accountRepo= new accountRepo(context); 
    } 

    public IUserRepository userRepo 
    { 
     get 
     { 
      return userRepo; 
     } 
    } 

    public IAccountRepository accountRepo 
    { 
     get 
     { 
      return accountRepo; 
     } 
    } 

    public void Dispose() 
    { 
     // If this function is being called the user wants to release the 
     // resources. lets call the Dispose which will do this for us. 
     Dispose(true); 

     // Now since we have done the cleanup already there is nothing left 
     // for the Finalizer to do. So lets tell the GC not to call it later. 
     GC.SuppressFinalize(this); 
    } 

    protected virtual void Dispose(bool disposing) 
    { 
     if (disposing == true) 
     { 
      //someone want the deterministic release of all resources 
      //Let us release all the managed resources 
      context= null; 
     } 
    } 

    ~UnitOfWork() 
    { 
     // The object went out of scope and finalized is called 
     // Lets call dispose in to release unmanaged resources 
     // the managed resources will anyways be released when GC 
     // runs the next time. 
     Dispose(false); 
    } 
} 

public abstract class AController : Controller 
{ 
    private IUnitOfWork IUnitOfWork; 

    protected IUnitOfWork UnitOfWork_ 
    { 
     get { return IUnitOfWork; } 
    } 

    public AController(IUnitOfWork uow) 
    { 
     this.IUnitOfWork = uow; 
    } 
} 

public class UserController : AController 
{ 
    // use our DbContext unit of work in case the page runs 
    public UserController() 
     : this(new UnitOfWork()) 
    { 

    } 

    // We will directly call this from the test projects 
    public UserController(UnitOfWork unitOfWork) 
     : base (unitOfWork) 
    { 

    } 

    public ActionResult Index() 
    { 
     List<User> users= UnitOfWork_.usersRepo.GetUsers(); 

     return View(users); 
    } 

}

所以我要問的是,它的上面一種是使用最好的做法?

回答

0

不要使DbContext靜態。 DbContext不是線程安全的並且使其變得靜態,這使得它易於在多個線程中使用 - 特別是在我們可能導致難以調試/解決錯誤甚至數據損壞的場景中。此外,保持上下文長時間運行並不是一個好主意 - 它會跟蹤越來越多的實體,因此會逐漸變慢和變慢。它還會阻止GC收集正在跟蹤但未被使用的對象。 另一個例子好多了。 DbContext本身就是一種UnitOfWork。我不確定我是否會從這裏開始使用Reposiory模式。我寧願直接使用DbContext,並在從我的代碼中出現時才轉向使用模式,並且只有在使用模式有益時(請注意,模式需要更多需要理解和維護的代碼)。