注册单独的封闭类型

时间:2016-01-14 16:36:24

标签: unity-container registration

我有以下界面

public interface IViewModelFactory<out TViewModel>
{
    TViewModel Create();
}

我有几个类将在

中实现这个接口
public class LeaseCreateViewModelFactory : IViewModelFactory<LeaseCreateModel>
{

    public LeaseCreateModel Create()
    {
        return null;
    }
}

public class LeaseEditViewModelFactory : IViewModelFactory<LeaseEditModel>
    {

        public LeaseEditModel Create()
        {
            return null;
        }
    }

如何注册所有使用Unity实现IViewModelFactory的封闭类型,我看过但我能找到的只是封闭类型的个人注册。

在Microsoft文档中找到如何为单个注册执行此操作 喜欢:

container.RegisterType<IValidator<StockQuote>, RandomStockQuoteValidator>();

有没有办法自动执行此操作?

2 个答案:

答案 0 :(得分:0)

找到此链接

http://www.danderson00.com/2010/09/automatically-register-implementors-of.html

我希望我能在不诉诸扩展的情况下做到这一点,但它有效......

public static void RegisterAllTypesForOpenGeneric(this IUnityContainer container, Type openGenericType, Assembly targetAssembly)
{
      if (!openGenericType.IsGenericTypeDefinition) throw new ArgumentException("typeToRegister must be an open generic type", "typeToRegister");

         foreach (Type type in targetAssembly.GetExportedTypes())
         {
             if (openGenericType.IsInterface)
                  RegisterInterfaceTypes(container, openGenericType, type, type);
             else
                  RegisterBaseTypes(container, openGenericType, type, type);           
         }
}

private static void RegisterInterfaceTypes(IUnityContainer container, Type openGenericType, Type targetType, Type typeToRegister)
{
    foreach (Type interfaceType in targetType.GetInterfaces())
    if (interfaceType.IsGenericType && !interfaceType.ContainsGenericParameters && openGenericType.IsAssignableFrom(interfaceType.GetGenericTypeDefinition()))
         container.RegisterType(interfaceType, typeToRegister);
}

private static void RegisterBaseTypes(IUnityContainer container, Type openGenericType, Type targetType, Type typeToRegister)
{
     if (targetType.BaseType != null && targetType.BaseType != yypeof(object))
         if (targetType.BaseType.IsGenericType && openGenericType.IsAssignableFrom(targetType.BaseType.GetGenericTypeDefinition()))
              container.RegisterType(targetType.BaseType, typeToRegister);
    else
        RegisterBaseTypes(container, openGenericType, targetType.BaseType, typeToRegister);
 }

答案 1 :(得分:0)

您可以使用扩展方法RegisterTypes来使用Registration by Convention

首先添加以下方法来定位具体类型和实现的接口:

private static IEnumerable<Type> GetImplementedIViewModelFactoryTypes(Type type)
{
    return type.GetInterfaces().Where(iface => iface.IsGenericType &&
        iface.GetGenericTypeDefinition() == typeof(IViewModelFactory<>));
}

private static bool ImplementsIViewModelFactory(Type type)
{
    return GetImplementedIViewModelFactoryTypes(type).Any();
}

private IEnumerable<Type> GetConcreteFactoryTypes()
{
    // You may want to change which assemblies are scanned here
    return AllClasses.FromAssemblies(typeof(IViewModelFactory<>).Assembly)
        .Where(ImplementsIViewModelFactory);
}

然后你可以像这样使用它:

container.RegisterTypes(GetConcreteFactoryTypes(), GetImplementedIViewModelFactoryTypes);

这将自动连接所有IViewModelFactory<T>实施。然后可以正常解决它们:

var leaseEditViewModelFactory = container.Resolve<IViewModelFactory<LeaseEditViewModel>>();