Amy*_*Amy 5 c# dependency-injection autofac code-contracts
假设我们有以下内容:
[ContractClass(typeof(ContractClassForIFoo))]
public interface IFoo
{
int DoThing(string x);
}
public class Foo : IFoo { ... }
[ContractClassFor(typeof(IFoo))]
public class ContractClassForIFoo : IFoo
{
public int DoThing(string x)
{
Contract.Requires<ArgumentNullException>(x != null);
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Autofac来注册我实现的所有组件IFoo:
builder.RegisterAssemblyTypes(ThisAssembly).As<IFoo>();
Run Code Online (Sandbox Code Playgroud)
当我稍后解决我的依赖关系:
var dependencies = container.Resolve<IFoo[]>();
Run Code Online (Sandbox Code Playgroud)
IFoo 除了契约类之外,我应该获得所有实现的类.如何防止所有合同类在没有完全移动到单独的程序集的情况下解析?
我可以这样做:
builder.RegisterAssemblyTypes(ThisAssembly)
.Where(t=> t.GetCustomAttribute<ContractClassForAttribute>() == null)
.As<IFoo>();
Run Code Online (Sandbox Code Playgroud)
但我需要为每个组件注册执行此操作.影响所有注册的东西会更好.如果它们具有ContractClassForAttribute属性,是否可以对从Autofac解析的类型进行全局排除?
编辑正如史蒂文评论中所解释的,ContractClass和ContractClassFor标记为[Conditional("CONTRACTS_FULL")],此解决方案可能会引入这些属性的错误。请参阅史蒂文的评论以获得更好的解释。
我不知道有什么机制允许对RegisterAssemblyTypes方法注册的注册进行全局过滤。使用此方法过滤注册的唯一解决方案是使用Where代码示例中所示的方法。
当注册在 a 内部注册时,ComponentRegistry无法将其从注册表中删除。
如果您不想Where在每次注册时都使用该方法,则可以创建另一个方法。
public static class ContractClassRegistrationExtensions
{
public static IRegistrationBuilder<TLimit, TScanningActivatorData, TRegistrationStyle> NotContractClass<TLimit, TScanningActivatorData, TRegistrationStyle>(this IRegistrationBuilder<TLimit, TScanningActivatorData, TRegistrationStyle> registration) where TScanningActivatorData : ScanningActivatorData
{
if (registration == null)
{
throw new ArgumentNullException("registration");
}
return registration.Where(t => t.GetCustomAttribute<ContractClassForAttribute>() == null);
}
}
Run Code Online (Sandbox Code Playgroud)
使用这种方法,而不是
builder.RegisterAssemblyTypes(ThisAssembly)
.Where(t=> t.GetCustomAttribute<ContractClassForAttribute>() == null)
.As<IFoo>();
Run Code Online (Sandbox Code Playgroud)
你将能够写:
builder.RegisterAssemblyTypes(ThisAssembly)
.NotContractClass()
.As<IFoo>();
Run Code Online (Sandbox Code Playgroud)
这不是一个真正的解决方案,但它是我在类似情况下使用的解决方案。
顺便说一句,如果你真的想要使用Autofac一些魔法,你可以实现一个IRegistrationSource
public class FilterRegistrationSource : IRegistrationSource
{
private static MethodInfo _createFilteredRegistrationMethod = typeof(FilterRegistrationSource).GetMethod("CreateFilteredRegistration");
public Boolean IsAdapterForIndividualComponents
{
get
{
return false;
}
}
public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
{
IServiceWithType serviceWithType = service as IServiceWithType;
if (serviceWithType == null)
{
yield break;
}
Type serviceType = serviceWithType.ServiceType;
if (!serviceType.IsClosedTypeOf(typeof(IEnumerable<>)))
{
yield break;
}
Type elementType = new Type[] { serviceType }.Concat(serviceType.GetInterfaces())
.Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>))
.Select(t => t.GetGenericArguments()[0])
.First();
yield return (IComponentRegistration)FilterRegistrationSource._createFilteredRegistrationMethod.MakeGenericMethod(elementType)
.Invoke(this, new Object[] { serviceWithType });
}
public IComponentRegistration CreateFilteredRegistration<T>(IServiceWithType serviceWithType)
{
return RegistrationBuilder.ForDelegate((cc, p) => cc.ComponentRegistry
.RegistrationsFor(serviceWithType.ChangeType(typeof(T)))
.Where(r => !r.Activator.LimitType.GetCustomAttributes(typeof(ContractClassForAttribute), false).Any())
.Select(r => r.Activator.ActivateInstance(cc, p))
.Cast<T>())
.As((Service)serviceWithType)
.CreateRegistration();
}
}
Run Code Online (Sandbox Code Playgroud)
您可以这样注册:builder.RegisterSource(new FilterRegistrationSource())
我尚未测试该解决方案的性能损失,请谨慎使用。
另一个有趣的解决方案是使用 AOP 来自定义注册的方式。
| 归档时间: |
|
| 查看次数: |
252 次 |
| 最近记录: |