使用 Unity 为工作单元/存储库模式创建实体框架对象

发布于 2024-08-24 14:07:37 字数 1670 浏览 3 评论 0原文

我正在尝试实现工作单元/存储库模式,如下所述:

这要求每个存储库接受 IUnitOfWork 实现,例如使用分部类扩展的 EF 数据上下文以添加 IUnitOfWork 接口。我实际上使用的是.net 3.5,而不是4.0。我的基本数据访问构造函数如下所示:

public DataAccessLayer(IUnitOfWork unitOfWork,
                       IRealtimeRepository realTimeRepository)
{
    this.unitOfWork = unitOfWork;
    this.realTimeRepository = realTimeRepository;
}

到目前为止,一切顺利。

我想做的是使用 Unity 框架添加依赖注入。

使用 Unity 创建 EF 数据上下文是一次冒险,因为它在解析构造函数时遇到了麻烦 - 最后我所做的是使用新的重载构造函数在我的部分类中创建另一个构造函数,并用 标记它[注入构造函数]

[InjectionConstructor]
public communergyEntities(string connectionString, string containerName)
    : this()
{
    // ...
}

(我知道我需要将连接字符串传递给基本对象,可以等到所有对象正确初始化后)

因此,使用这种技术,我可以愉快地解析我的实体框架对象作为 IUnitOfWork 实例,因此:

using (IUnityContainer container = new UnityContainer())
{
    container.RegisterType<IUnitOfWork, communergyEntities>();

    container.Configure<InjectedMembers>()
        .ConfigureInjectionFor<communergyEntities>
            (new InjectionConstructor("a", "b"))

    DataAccessLayer target = container.Resolve<DataAccessLayer>();

    // ...
}

太棒了。我现在需要做的是为 DataAccessLayer 创建对存储库对象的引用 - DAL 只需要知道接口,所以我猜测我需要将其实例化为 Unity Resolve 语句的一部分,并将其传递给适当的对象IUnitOfWork 接口。

在过去,我只是将数据库连接字符串传递给存储库构造函数,它就会消失,创建一个本地实体框架对象并仅在存储库方法的生命周期中使用它。这是不同的,因为我在 Unity Resolve 语句期间创建一个实体框架实例作为 IUnitOfWork 实现,并且我需要将该实例传递到存储库的构造函数中 - 这是否可能,如果可以,如何实现?

我想知道是否可以将存储库设为属性并将其标记为依赖项,但这仍然无法解决如何使用 DAL 正在解析的 IUnitOfWork 对象创建存储库的问题,

我不确定如果我正确理解了这种模式,并且很乐意接受有关实现它的最佳方法的建议 - 实体框架将保留,但如果不是最佳方法,则可以更换 Unity。如果我把整个事情搞颠倒了,请告诉我

I'm trying to implement the Unit of Work/Repository pattern, as described here:
http://blogs.msdn.com/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx

This requires each Repository to accept an IUnitOfWork implementation, eg an EF datacontext extended with a partial class to add an IUnitOfWork interface. I'm actually using .net 3.5, not 4.0. My basic Data Access constructor looks like this:

public DataAccessLayer(IUnitOfWork unitOfWork,
                       IRealtimeRepository realTimeRepository)
{
    this.unitOfWork = unitOfWork;
    this.realTimeRepository = realTimeRepository;
}

So far, so good.

What I'm trying to do is add Dependency Injection using the Unity Framework.

Getting the EF data context to be created with Unity was an adventure, as it had trouble resolving the constructor - what I did in the end was to create another constructor in my partial class with a new overloaded constructor, and marked that with [InjectionConstructor].

[InjectionConstructor]
public communergyEntities(string connectionString, string containerName)
    : this()
{
    // ...
}

(I know I need to pass the connection string to the base object, that can wait until once I've got all the objects initialising correctly)

So, using this technique, I can happily resolve my entity framework object as an IUnitOfWork instance thus:

using (IUnityContainer container = new UnityContainer())
{
    container.RegisterType<IUnitOfWork, communergyEntities>();

    container.Configure<InjectedMembers>()
        .ConfigureInjectionFor<communergyEntities>
            (new InjectionConstructor("a", "b"))

    DataAccessLayer target = container.Resolve<DataAccessLayer>();

    // ...
}

Great. What I need to do now is create the reference to the repository object for the DataAccessLayer - the DAL only needs to know the interface, so I'm guessing that I need to instantiate it as part of the Unity Resolve statement, passing it the appropriate IUnitOfWork interface.

In the past, I would have just passed the Repository constructor the db connection string, and it would have gone away, created a local Entity Framework object and used that just for the lifetime of the Repository method. This is different, in that I create an Entity Framework instance as an IUnitOfWork implementation during the Unity Resolve statement, and it's that instance I need to pass into the constructor of the Repository - is that possible, and if so, how?

I'm wondering if I could make the Repository a property and mark it as a Dependency, but that still wouldn't solve the problem of how to create the Repository with the IUnitOfWork object that the DAL is being Resolved with

I'm not sure if I've understood this pattern correctly, and will happily take advice on the best way to implement it - Entity Framework is staying, but Unity can be swapped out if not the best approach. If I've got the whole thing upside down, please tell me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2024-08-31 14:07:37

这在这里被重新表述和回答: Unity 框架 - 重用实例

解决方案是使用 ContainerControlledLifetimeManager -又名单例:
http://msdn.microsoft.com/en-us/library/dd203242。 ASPX

This was rephrased and answered here: Unity framework - reusing instance

The solution is to use a ContainerControlledLifetimeManager - aka Singleton:
http://msdn.microsoft.com/en-us/library/dd203242.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文