1

我正在尝试用装饰器包装我的通用接口,但它根本不起作用,在我看来,从其他问题来看,唯一的方法是为每个装饰器明确地这样做,我的问题是它是否可能用ninject中的特定装饰器包装实现某个接口的所有类型。

代码:

 static void BindMediatr(IKernel kernel) {
  kernel.Components.Add < IBindingResolver, ContravariantBindingResolver > ();

  kernel.Bind(scan => scan.FromAssemblyContaining < IMediator > ()
   .SelectAllClasses()
   .BindDefaultInterface());

  kernel.Bind < SingleInstanceFactory > ().ToMethod(ctx => t => ctx.Kernel.Get(t));
  kernel.Bind < MultiInstanceFactory > ().ToMethod(ctx => t => ctx.Kernel.GetAll(t));
  kernel.Bind(
   x => x.FromThisAssembly()
   .SelectAllClasses()
   .InheritedFromAny(typeof(IAsyncRequestHandler < , > ))
   .BindAllInterfaces());

  kernel.Bind(typeof(IAsyncRequestHandler < , > ))
   .To(typeof(Decorater < , > ))
   .WhenInjectedInto < ApiController > ();
 }

 public class Decorater < TRequest, TResponse >
  : IAsyncRequestHandler < TRequest, TResponse >
  where TRequest: IAsyncRequest < TResponse > {
   IAsyncRequestHandler < TRequest,
   TResponse > _decoratee;

   public Decorater(IAsyncRequestHandler < TRequest, TResponse > decoratee) {
    _decoratee = decoratee;
   }

   public Task < TResponse > Handle(TRequest message) {
    // do something here
   }
  }
4

2 回答 2

0

我发现这个扩展方法可以解决问题:

public static class KernelExtensions
    {
        /// <summary>
        /// Binds an open generic type to its implementation and adds all its defined decorators
        /// </summary>
        /// <param name="kernel">Ninject Container</param>
        /// <param name="openGenericType">Open generic Type</param>
        /// <param name="assembly">Assembly to scan for the open generic type implementation</param>
        /// <param name="decoratorTypes">Types of the decorators. Order matters. Order is from the most outer decorator to the inner decorator</param>
        public static void BindManyOpenGenericsWithDecorators(this IKernel kernel, Type openGenericType, Assembly assembly, params Type[] decoratorTypes)
        {
            var allImplementations = GetAllTypesImplementingOpenGenericType(openGenericType, assembly);

            foreach (var type in allImplementations.Where(type => !decoratorTypes.Contains(type)))
            {
                var genericInterface = type.GetInterfaces().FirstOrDefault(x => openGenericType.IsAssignableFrom(x.GetGenericTypeDefinition()));

                // real implementation
                var parentType = decoratorTypes.Last();
                kernel.Bind(genericInterface).To(type)
                .WhenInjectedInto(parentType);
            }

            for (var i = 0; i <= decoratorTypes.Count() - 1; i++)
            {
                var decoratorType = decoratorTypes[i];

                if (i == 0)
                {
                    // most outer decorator
                    kernel.Bind(openGenericType).To(decoratorType);
                }
                else
                {
                    // inner decorators
                    var parentType = decoratorTypes[i - 1];
                    kernel.Bind(openGenericType).To(decoratorType)
                        .WhenInjectedInto(parentType);
                }
            }
        }

        private static IEnumerable<Type> GetAllTypesImplementingOpenGenericType(Type openGenericType, Assembly assembly)
        {
            return (from type in assembly.GetTypes()
                    from interfaceType in type.GetInterfaces()
                    let baseType = type.BaseType
                    where
                    (baseType != null && baseType.IsGenericType &&
                    openGenericType.IsAssignableFrom(baseType.GetGenericTypeDefinition())) ||
                    (interfaceType.IsGenericType &&
                    openGenericType.IsAssignableFrom(interfaceType.GetGenericTypeDefinition()))
                    select type);
        }
    }
于 2016-04-29T16:51:09.660 回答
0

如果您需要在所有实现该接口的类中装饰对某些方法的调用,您可以尝试代理您的所有绑定。

Ninject 有一个名为 - Interception的扩展,它允许您拦截对您的方法的特定调用并用一些逻辑包装它。

是一篇关于此的好文章。

希望能帮助到你。

于 2016-04-29T15:22:06.017 回答