如何使用Unity解决静态类中的依赖关系? [英] How to resolve dependency in static class with Unity?

查看:96
本文介绍了如何使用Unity解决静态类中的依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下扩展方法,(自然地)存在于静态类中.

I have the following extension method, which exists (naturally) in a static class.

public static class MyExtensions
{
    [Dependency]
    private static IMyDataContext _myDataContext { get; set; }

    public static void MyExtensionMethod(this MyType myType)
    {
        // do stuff

        _myDataContext.DoAwesomeThing();
    }
}

_myDataContext对象为 null .

通常我会使用UnityContainer来注册类型,但是由于这是一个静态类,所以我不能.

Normally I'd use the UnityContainer to register the type, but as this is a static class, I can't.

我需要实例化_ myDataContext以便它在需要时不为空吗?

What do I need to instantiate _myDataContext so that it's not null when I need it?

推荐答案

正如您已经提到的那样,由于Unity是静态的,因此无法用于解析该类.有一些选择.我个人最喜欢的是使用抽象工厂"模式.我倾向于微调模式,以便与DI配合使用.

As you have already mentioned, Unity can't be used to resolve the class due to it being static. There are a few options for this. My personal favorite is using the Abstract Factory pattern. I tend to tweak the pattern just a tad to work well with DI.

工厂通常看起来像这样:

The factory typically kind of looks like this:

/// <summary>
/// Creates an IMyDataContext instance
/// </summary>
public static class MyDataContextFactory
{
    /// <summary>
    /// The factory used to create an instance
    /// </summary>
    static Func<IMyDataContext> factory;

    /// <summary>
    /// Initializes the specified creation factory.
    /// </summary>
    /// <param name="creationFactory">The creation factory.</param>
    public static void SetFactory(Func<IMyDataContext> creationFactory)
    {
        factory = creationFactory;
    }

    /// <summary>
    /// Creates a new IMyDataContext instance.
    /// </summary>
    /// <returns>Returns an instance of an IMyDataContext </returns>
    public static IMyDataContext CreateContext()
    {
        if (factory == null) throw new InvalidOperationException("You can not create a context without first building the factory.");

        return factory();
    }
}

在引导过程中(无论您在哪里设置服务注册),都可以初始化工厂来解决依赖关系.

In your bootstrap process (where ever you setup your service registrations), you can initialize the factory to resolve your dependency.

MyDataContextFactory.SetFactory(() => this.Container.Resolve<IMyDataContext>());

现在,在扩展方法中,您将获取上下文.

Now in your extension method, you fetch a context.

public static class MyExtensions
{
    public static void MyExtensionMethod(this MyType myType)
    {
        MyDataContextFactory.CreateContext().DoAwesomeThing();
    }
}

您的上下文注册可以在有条件的情况下处理服务的各种不同配置.如果可以通过Unity以外的其他方式设置上下文,则该所有者可以传入扩展方法将使用的新委托.

Your Unity registration of the context, can handle the various different configurations of the service if there are conditions to resolving it. If there is the possibility that the context can be set by something other than Unity, that owner can just pass in a new delegate that the extension method will use.

我倾向于避免将容器本身传递到我的工厂,因为这开始使容器与我的应用程序紧密耦合.如果我要将任何东西传递给工厂,我宁愿它是用来通过DI进行解析的工厂代表,而不是自己传递DI容器.

I tend to avoid passing the containers themselves in to my factories, as that starts creeping tight-coupling of the containers to my app. If I'm going to pass anything in to the factory, i'd rather it be a factory delegate used to resolve via DI, than to pass the DI container in itself.

这篇关于如何使用Unity解决静态类中的依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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