Unity:有条件的解决 [英] Unity: Conditional resolving

查看:84
本文介绍了Unity:有条件的解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用Unity在运行时根据收到的特定数据解析类型。我的代码(类似于下面的代码)当前在启动时将类型注册在bootstrapper类中,然后在主流程中确定所需的类型。

I'm looking to use Unity to resolve types at runtime based on specific data received. My code (similar to that shown below) currently registers the types in a bootstrapper class at start-up and then within the main flow a decision is made on what type is required.

我想要做的是用解析器替换使用'new'关键字的代码行,但是由于此代码已超出我的引导程序,所以我不是确定如何完成...我是Unity的新手,所以请放轻松。

What I'm looking to do is replace the code lines that use the 'new' keyword with a resolver, however as this code is outwith my bootstrapper I'm not sure how this can be done...I'm new to Unity so please go easy.

// In Bootstrapper class
resolver.RegisterType<IDataType1, DataType1>();
resolver.RegisterType<IDataType2, DataType2>();
resolver.RegisterType<IDataType3, DataType3>();


// Main flow...outwith bootstrapper
switch (dataRecordType)
{
    case DataRecordType.dataType1:
        DataType1 dt1 = new DataType1();
        dt1.ProcessData();
        break;

    case DataRecordType.dataType2:
        DataType2 dt2 = new DataType2();
        dt2.ProcessData();
        break;

    case DataRecordType.dataType3:
        DataType3 dt3 = new DataType3();
        dt3.ProcessData();
        break;

    default:
        break;
}


推荐答案

您缺少一些抽象这里。您缺少对数据类型的一般抽象,也缺少用于创建这些数据类型的实现的抽象:

You are missing a few abstractions here. You're missing an general abstraction over your data types and an abstraction for creating implementations of those data types:

// In your core layer
public interface IDataType {
    void ProcessData();
}

public interface IDataTypeFactory {
    IDataType Create(DataRecordType dataRecordType);
}

// In Bootstrapper class
resolver.RegisterInstance<IDataTypeFactory>(new DataTypeFactory(resolver));
resolver.RegisterType<IDataType1, DataType1>();
resolver.RegisterType<IDataType2, DataType2>();
resolver.RegisterType<IDataType3, DataType3>();

private sealed class DataTypeFactory : IDataTypeFactory {
    private readonly IUnityContainer container;
    public DataTypeFactory(IUnityContainer container) {
        this.container = container;
    }

    public IDataType Create(DataRecordType dataRecordType) {
        switch (dataRecordType) {
            case DataRecordType.dataType1:
                return this.container.Resolve<IDataType1>();
            case DataRecordType.dataType2:
                return this.container.Resolve<IDataType2>();
            case DataRecordType.dataType3:
                return this.container.Resolve<IDataType3>();
            default:
                throw new InvalidEnumArgumentException();
        }
    }
}

您所看到的是用于创建实现的代码已移至工厂。现在剩下的应用程序代码可以是这样的:

What you can see is that the code for creating implementations is moved to the factory. Now the remaining application code can be comes something like this:

// Main flow...outwith bootstrapper
IDataType dt = this.dataTypeFactory.Create(dataRecordType);
dt.ProcessData();

IDataType1 IDataType2 IDataType3 现在仅在引导程序中使用,并且已成为冗余(或至少与您提供的代码冗余),因此您甚至可以将它们全部删除,并将引导逻辑更改为以下内容:

The IDataType1, IDataType2 and IDataType3 are now only used in the bootstrapper and have become redundant (or at least, redundant with the code you presented), so you could even remove them all together and change the bootstrap logic to the following:

// In Bootstrapper class
resolver.RegisterInstance<IDataTypeFactory>(new DataTypeFactory(resolver));
resolver.RegisterType<DataType1>();
resolver.RegisterType<DataType2>();
resolver.RegisterType<DataType3>();

private sealed class DataTypeFactory : IDataTypeFactory {
    private readonly IUnityContainer container;
    public DataTypeFactory(IUnityContainer container) {
        this.container = container;
    }

    public IDataType Create(DataRecordType dataRecordType) {
        switch (dataRecordType) {
            case DataRecordType.dataType1:
                return this.container.Resolve<DataType1>();
            case DataRecordType.dataType2:
                return this.container.Resolve<DataType2>();
            case DataRecordType.dataType3:
                return this.container.Resolve<DataType3>();
            default:
                throw new InvalidEnumArgumentException();
        }
    }
}

这篇关于Unity:有条件的解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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