Unity:使用通用类型注册并解析类 [英] Unity: Register and resolve class with generic type

查看:94
本文介绍了Unity:使用通用类型注册并解析类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Unity,并尝试尽可能地遵循SOLID原理.因此,所有实现都只依赖于接口.

I'm using Unity and try to follow to SOLID-principles as far as possible. Therefore all implementations only have dependencies to interfaces.

我有一个看起来像这样的collectionwrapper:

I have a collectionwrapper which looks like this:

public interface ICollectionWrapper<TModel>
{
    int TotalCount { get; set; }
    IEnumerable<TModel> Items { get; set; }
}

现在,我想用工厂创建ICollectionFactory<T>的实例.这是我到目前为止所得到的:

Now I want to create the instance of ICollectionFactory<T> with a factory. This is what I got so far:

public interface ICollectionWrapperFactory
{
    ICollectionWrapper<T> CreateCollection<T>();
    ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items);
    ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items, int totalCount);
}

public class CollectionWrapperFactory : ICollectionWrapperFactory
{
    private readonly IUnityContainer _container;

    public CollectionWrapperFactory(IUnityContainer container)
    {
        _container = container;
    }

    public ICollectionWrapper<T> CreateCollection<T>()
    {
        var collectionWrapper = _container.Resolve<ICollectionWrapper<T>>();
        return collectionWrapper;
    }

    public ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items)
    {
        throw new System.NotImplementedException();
    }

    public ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items, int totalCount)
    {
        throw new System.NotImplementedException();
    }
}

我知道将容器用作服务定位器被认为是一种反模式,但是我不知道有什么更好的方法来解决此问题.如果有更好的方式可以做到这一点,请大家注意...一种替代方法是使用Activator,但是工厂需要了解ICollectionWrapper<T>的实际实现.

I know that using the container as a servicelocator is considered an anti-pattern, but I don't know any better way to solve this. If there's a better pattern for doing this I'm all ears... An alternative is using the Activator, but then the factory would need to know about the actual implementation of ICollectionWrapper<T>.

但是真正的问题是我无法正确注册ICollectionWrapper.

But the real problem is that I cannot register the ICollectionWrapper correctly.

container.RegisterType<ICollectionWrapper<T>, CollectionWrapper<T>>(new TransientLifetimeManager()); // Does not compile.

T可以是任何类型. 我希望能够创建ICollectionWrapper<T>的实例,而不必注册T的所有可能组合.

T may be any type. I want to be able to create instances of ICollectionWrapper<T> without having to register every possible combination of T.

目前,我只有ICollectionWrapper<T>的一种实现.但是关键是我真的希望Unity是了解实际实现的唯一部分.

Currently I only have one implementation of ICollectionWrapper<T>. But the point is that I really want Unity to be the only part that knows about the actual implementation.

[DataContract]
public class CollectionWrapper<TModel> : ICollectionWrapper<TModel>
{
    public CollectionWrapper(IEnumerable<TModel> items)
    {
        Items = items;
    }

    public CollectionWrapper(IEnumerable<TModel> items, int totalCount)
    {
        Items = items;
        TotalCount = totalCount;
    }

    public CollectionWrapper()
    {

    }

    [DataMember]
    public int TotalCount { get; set; }
    [DataMember]
    public IEnumerable<TModel> Items { get; set; }
}

推荐答案

T可以是任何类型.我希望能够创建ICollectionWrapper的实例而不必注册T的所有可能组合.

T may be any type. I want to be able to create instances of ICollectionWrapper without having to register every possible combination of T.

这就是注册泛型的用途.某些IOC将方法命名为RegisterGeneric以使其易于说明(例如autofac),但统一将其保留为

That's what register generics is for. Some IOC name the method as RegisterGeneric to make it self explanatory (autofac for example), but unity keep it just an overload of RegisterType.

container.RegisterType(typeof(ICollectionWrapper<>), typeof(CollectionWrapper<>), new TransientLifetimeManager());

还请注意,您的可注入对象具有多个构造函数. 它本身被视为反模式.

Also note that your injectable is having multiple constructors. That itself is considered as anti-pattern.

如果您修复了多个构造函数,则可以进行上述注册.

If you fix the multiple construtor thing, above registration will work.

这篇关于Unity:使用通用类型注册并解析类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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