简体   繁体   English

SharpRepository EF5,如何共享dbcontext

[英]SharpRepository EF5, how to share dbcontext

Using the EF5 implementation of SharpRepository , how can I share the DBContext amongst different instatiations of the IRepository when using the RepositoryFactory ? 使用SharpRepositoryEF5实现,在使用RepositoryFactory时,如何在IRepository的不同实例之间共享DBContext?

Code snippet: 代码段:

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 file: 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>

Thanks 谢谢

The RepositoryFactory and configuration bits are relatively new and unfortunately right now don't contain a way to share a single DbContext, but I'll be adding that as a feature request and just need to think of the best way to make it happen. RepositoryFactory和配置位相对较新,不幸的是现在不包含共享单个DbContext的方法,但我将添加它作为功能请求,只需要考虑实现它的最佳方法。

With that being said, here is how I would handle it right now. 话虽如此,现在我将如何处理它。 Instead of using the RepositoryFactory you can just hard-code using a Ef5Repository until we implement this new feature. 在我们实现这个新功能之前,您可以使用Ef5Repository进行硬编码,而不是使用RepositoryFactory。 The first parameter in the constructor for Ef5Repository is a DbContext so you would pass your the same one into both repositories. Ef5Repository的构造函数中的第一个参数是DbContext,因此您可以将同一个参数传递到两个存储库中。

Not sure if you are using an IOC container like StructureMap, but if so you can set that up to handle creating a single DbContext for each thread or .NET request if it's a web application. 不确定您是否正在使用像StructureMap这样的IOC容器,但如果是这样,您可以将其设置为处理为每个线程或.NET请求创建单个DbContext(如果它是Web应用程序)。

The StructureMap config would look like this: 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);

Then your PersonManager would look like: 然后你的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);

    }

}

Unfortunately at this point you are hard-coding the type of the repository and don't get the benefits of the configuration file, but we'll get that feature in there soon. 不幸的是,此时您正在对存储库的类型进行硬编码,并且没有获得配置文件的好处,但我们很快就会在那里获得该功能。 Thanks. 谢谢。

Update for SharpRepository version 1.2 SharpRepository版本1.2的更新

Version 1.2 of SharpRepository (released on 3/14) fixes this issue. SharpRepository 1.2版(3/14发布)修复了这个问题。 Now you can tell SharpRepository what IoC container you are using and it will use that to create the DbContext. 现在您可以告诉SharpRepository您正在使用的IoC容器,它将使用它来创建DbContext。 This allows you to control the life-cycle of your DbContext and share it across multiple repositories. 这允许您控制DbContext的生命周期并在多个存储库之间共享它。

First step is to get the NuGet package for the IoC container you are using. 第一步是获取您正在使用的IoC容器的NuGet包。 Search NuGet for SharpRepository.Ioc and you'll see the 5 IoC packages that we've created and are handled out of the box (Autofac, Ninject, StructureMap, Unity and Windsor), you can always create your own if you are using a different IoC that we don't cover right now. 搜索NuGet for SharpRepository.Ioc,您将看到我们创建并开箱即用的5个IoC软件包(Autofac,Ninject,StructureMap,Unity和Windsor),如果您使用的话,您可以随时创建自己的软件包。我们现在没有涉及的不同IoC。

Once that is installed you will need to set the RepositoryDependencyResolver in your App_Start code, Global.asax or bootstrapper code so it runs on application start. 安装完成后,您需要在App_Start代码,Global.asax或bootstrapper代码中设置RepositoryDe​​pendencyResolver,以便它在应用程序启动时运行。 Here is how you would do it if using StructureMap. 如果使用StructureMap,您将如何操作。

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

I am telling it to use StructureMap and pass in the Container. 我告诉它使用StructureMap并传入Container。 Then you just have to make sure the IoC is setup and knows how to handle a request for a DbContext. 然后,您只需确保IoC已设置并知道如何处理DbContext的请求。 (see above for an example of that using the ASP.NET request life-cycle in StructureMap) (有关在StructureMap中使用ASP.NET请求生命周期的示例,请参见上文)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM