C#/ EF和存储库模式:放在哪里ObjectContext中与多个存储库的解决方案? [英] C#/EF and the Repository Pattern: Where to put the ObjectContext in a solution with multiple repositories?

查看:241
本文介绍了C#/ EF和存储库模式:放在哪里ObjectContext中与多个存储库的解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的多个存储库。我应该在哪里放置的ObjectContext?现在,我有一个像引用的ObjectContext CTX; 在每一个存储库中。什么是去这是最聪明的,最安全的方法是什么?

I have multiple repositories in my application. Where should I put the ObjectContext? Right now, I have a reference like ObjectContext ctx; in every repository. What is the smartest and safest way to go about this?

推荐答案

多一个设计的ObjectContext 实例是唯一可以接受的,如果你的方法提交事务。否则,有可能是外部调用提交事务可能不会坚持你打算一切,因为你会抱到的ObjectContext

A design with multiple ObjectContext instances is only acceptable if your Repository methods commit the transaction. Otherwise, it is possible that external calls to commit the transaction may not persist everything you intend, because you will hold references to different instances of the ObjectContext.

如果您想限制的ObjectContext 来一个实例,那么你可以建立一个包含 RepositoryProvider 类在的ObjectContext ,并管理库行动,以提交数据的传播。这可以通过将最佳实现,
- 注射的的ObjectContext 引用到每个存储库中,或
- 订阅的资料库事件事件处理程序 S中的呼吁适当的方法的ObjectContext

If you want to restrict the ObjectContext to a single instance, then you can build a RepositoryProvider class that contains the ObjectContext, and manages the propagation of repository actions to data commits. This can be best accomplished by either, - Injecting the ObjectContext reference into each repository, or - Subscribing the repositories' events to EventHandlers that call the appropriate methods on the ObjectContext.

以下是我使用了一个高可插拔实现:

The following is a highly pluggable implementation that I have used:

public interface IRepositoryProvider
{
    IRepository this[Type repositoryType] { get; }
}

库工厂接口

的实施有上依赖的IEnumerable< IFilteredRepositoryFactory方式>

public interface IFilteredRepositoryFactory{
   bool CanCreateRepository(Type repositoryType);
   IRepository CreateRepository(Type repositoryType, ObjectContext context);
}

因此​​,执行如下:

So, the implementation looks like:

public class RepositoryProvider
{
    public RepositoryProvider(ObjectContext context, IEnumerable<IFilteredRepositoryFactory> repositoryFactories)
    {
        _context = context;
        _repositoryFactories = repositoryFactories;
    }

    private readonly ObjectContext _context;
    private readonly IEnumerable<IFilteredRepositoryFactory> _repositoryFactories;
    private readonly Dictionary<Type, IRepository> _loadedRepositories;

    IRepository this[Type repositoryType]
    {
        get
        {
            if(_loadedRepositories.ContainsKey(repositoryType))
            {
                return _loadedRepositories[repositoryType];
            }
            var repository = GetFactory(repositoryType).CreateRepository(repositoryType, _context);
            _loadedRepositories.Add(repositoryType,repository);
            return repository;
        }
    }

    IFilteredRepositoryFactory GetFactory(Type repositoryType)
    {
        //throws an exception if no repository factory is found
        return _repositoryFactories.First(x => x.CanCreateRepository(repositoryType));
    }
}

应当注意的是,新的将由第一匹配工厂实现创建。所以,如果工厂的集合包含的多个工厂可以的创建一个为给定的资料库键入,在枚举的第一个 IFilteredRepositoryFactory 对象将被用来和任何后续的工厂将被忽略。此外,如果没有注册的工厂,会抛出异常。

It should be noted that a new Repository will be created by the first matching factory implementation. So, if the collection of factories contains multiple factories that can create a Repository for the given repository Type, the first IFilteredRepositoryFactory object in the enumerable will be used and any subsequent factories will be ignored. In addition, if there is no registered factory, and exception will be thrown.

这篇关于C#/ EF和存储库模式:放在哪里ObjectContext中与多个存储库的解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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