2013-03-07 43 views
4

使用EF5 implementation of SharpRepository,我怎麼能使用RepositoryFactory時共享的DbContext躋身IRepository的不同instatiations?SharpRepository EF5,如何共享的DbContext

代碼片段:

using SharpRepository.Repository; 

public class PersonManager() 
{ 

    private IRepository<Domain.PersonIdentifier, int> personIdentifierRepository; 
    private IRepository<Domain.NextNumber, string> nextNumberRepository; 

    public PersonManager() 
    { 

     //HOW TO SHARE A SINGLE DBCONTEXT INSTANCE BETWEEN THESE TWO INSTANTIATIONS ?? 
     this.personIdentifierRepository = RepositoryFactory.GetInstance<Domain.PersonIdentifier, int>(); 
     this.nextNumberRepository = RepositoryFactory.GetInstance<Domain.NextNumber, string>(); 

    } 

} 

Web.config文件:

<sharpRepository> 
    <repositories default="EF5Repo"> 
     <repository name="EF5Repo" connectionString="MyContainer" factory="SharpRepository.Ef5Repository.Ef5ConfigRepositoryFactory, SharpRepository.Ef5Repository" /> 
    </repositories> 
    <cachingProviders default="inmemory"> 
     <cachingProvider name="inmemory" factory="SharpRepository.Repository.Caching.InMemoryConfigCachingProviderFactory, SharpRepository.Repository" /> 
    </cachingProviders> 
    <cachingStrategies default="noCaching"> 
     <cachingStrategy name="timeout" timeout="30" factory="SharpRepository.Repository.Caching.TimeoutConfigCachingStrategyFactory, SharpRepository.Repository" /> 
     <cachingStrategy name="standardCachingStrategy" generational="true" writeThrough="true" factory="SharpRepository.Repository.Caching.StandardConfigCachingStrategyFactory, SharpRepository.Repository" /> 
     <cachingStrategy name="noCaching" factory="SharpRepository.Repository.Caching.NoCachingConfigCachingStrategyFactory, SharpRepository.Repository" /> 
    </cachingStrategies> 
    </sharpRepository> 

感謝

回答

3

的RepositoryFactory和配置位是比較新的,不幸的是,現在不包含方式共享一個DbContext,但我會將其作爲功能請求添加,只需要考慮實現它的最佳方式即可。

有了這樣說,這裏是我將如何處理它現在。除了使用RepositoryFactory,您可以直接使用Ef5Repository進行硬編碼,直到我們實現這個新功能。 Ef5Repository的構造函數中的第一個參數是一個DbContext,因此您可以將同一個參數傳遞給兩個存儲庫。

不知道,如果你使用的是像StructureMap的IOC容器,但如果是這樣,你可以設置多達負責創建每個線程或.NET請求的單一的DbContext如果它是一個Web應用程序。

的StructureMap的配置是這樣的:

 // Hybrid (once per thread or ASP.NET request if you’re in a web application) 
     For<DbContext>() 
      .HybridHttpOrThreadLocalScoped() 
      .Use<MyEntities>() 
      .Ctor<string>("MyContainer").Is(entityConnectionString); 

那麼你的PersonManager會是什麼樣子:

using SharpRepository.Repository; 

public class PersonManager() 
{ 

    private IRepository<Domain.PersonIdentifier, int> personIdentifierRepository; 
    private IRepository<Domain.NextNumber, string> nextNumberRepository; 

    public PersonManager(DbContext dbContext) 
    { 
     this.personIdentifierRepository = new Ef5Repository<Domain.PersonIdentifier, int>(dbContext); 
     this.nextNumberRepository = new Ef5Repository<Domain.NextNumber, string>(dbContext); 

    } 

} 

不幸的是,在這一點上,你是被硬編碼庫和Don」的類型沒有獲得配置文件的好處,但我們很快就會在那裏獲得該功能。謝謝。

更新SharpRepository 1.2版

SharpRepository(3/14上公佈)的1.2版本修復了這個問題。現在你可以告訴SharpRepository你正在使用的IoC容器,它將使用它來創建DbContext。這使您可以控制DbContext的生命週期,並在多個存儲庫中共享它。

第一步是讓NuGet包爲您正在使用的IoC容器。在SharpRepository.Ioc中搜索NuGet,您將看到我們創建並開箱即用的5個IoC包(Autofac,Ninject,StructureMap,Unity和Windsor),如果您使用的是不同的IoC,我們現在不覆蓋。

一旦被安裝,您將需要設置RepositoryDe​​pendencyResolver在App_Start代碼,Global.asax中或引導程序代碼,以便在其上運行的應用程序的開始。這裏是你如何使用StructureMap來做到這一點。

RepositoryDependencyResolver.SetDependencyResolver(new StructureMapDependencyResolver(ObjectFactory.Container)); 

我告訴它使用StructureMap並傳入容器。那麼你只需要確保IoC已經建立並且知道如何處理一個DbContext的請求。 (參見以上的,使用ASP.NET請求生命週期中StructureMap一個例子)

+0

感謝。事實上,我認爲直接實例化Ef5Repository將是目前唯一的可能性,但感謝您確認這一點。還要感謝把StructureMap的例子放在那裏,實際上我打算使用它。保持在這個非常有用的圖書館良好的工作 – tjeuten 2013-03-07 17:04:44

+0

謝謝。我已經在這裏頭腦風暴,只需要找出最佳方法。這將是非常有用的,所以感謝您提出。 – 2013-03-07 17:09:36