我在这里的一个问题中找到了解决方案。下面的代码按预期工作。
这有什么缺点,将来会产生任何问题吗?或任何注册所有存储库和服务的最佳方式,除了这个?
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
// container.RegisterType<IAbcRepository, AbcRepository>();
// container.RegisterType<IAbcService, AbcService>();
container.RegisterTypes(AllClasses.FromAssemblies(typeof(Repository).Assembly), WithMappings.FromAllInterfaces, GetName, GetLifetimeManager);
container.RegisterTypes(AllClasses.FromAssemblies(typeof(Service).Assembly), WithMappings.FromAllInterfaces, GetName, GetLifetimeManager);
}
static bool IsNotificationHandler(Type type)
{
return type.GetInterfaces().Any(x => x.IsGenericType);
}
static LifetimeManager GetLifetimeManager(Type type)
{
return IsNotificationHandler(type) ? new ContainerControlledLifetimeManager() : null;
}
static string GetName(Type type)
{
return IsNotificationHandler(type) ? string.Format("HandlerFor" + type.Name) : string.Empty;
}
}
在大型应用程序中使用多少安全?