【问题标题】:Unity Interception Configuration - how to make less verbose?Unity拦截配置 - 如何减少冗长?
【发布时间】:2011-09-02 10:19:33
【问题描述】:

要在 Unity 2.0 中设置拦截,您可以在配置中添加类似这样的内容(取自 Unity 文档)...

      <policy name="addDataAccessTypes">
        <matchingRule name="DataLayerMatch" type="NamespaceMatchingRule">
          <constructor>
            <param name="namespaceName" value="MyApp.DataAccess" />
          </constructor>
        </matchingRule>
        <callHandler name="LogHandler" type="LoggingCallHandler" />
        <callHandler name="SecurityHandler"
            type="DatabaseSecurityCheckHandler" />
      </policy>

有没有办法为同一个处理类设置多个接口?

例如像这样...

<constructor>
    < interface to intercept 1 />
    < interface to intercept 2 />
</construtor>

使用统一示例中给出的方法,如果你有很多接口要拦截,你的配置文件会变得非常冗长。

【问题讨论】:

    标签: .net configuration unity-container app-config configuration-files


    【解决方案1】:

    如果您使用Unity.Interception 程序集,您可以以更流畅的方式对属性进行拦截。这样做的缺点是被拦截的类确实(以某种方式)知道方面:

    一个非常简单的设置示例如下所示:

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine();
                Console.WriteLine("Starting test...");
    
                var container = new UnityContainer();
                container.AddNewExtension<Interception>();
                container.Configure<Interception>()
                    .SetDefaultInterceptorFor<IGadget>(new InterfaceInterceptor());
    
                container.RegisterType<IGadget, Gadget>();
    
                var gadget = container.Resolve<IGadget>();
                gadget.DoSomething();
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine("An error has occurred: {0}", ex);
            }
            finally
            {
                Console.WriteLine();
                Console.WriteLine("Test complete.");
                Console.ReadKey();
            }
        }
    }
    
    
    
    public class LogAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return container.Resolve<LogHandler>();
        }
    }
    
    public class LogHandler : ICallHandler
    {
        public int Order { get; set; }
    
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            Console.WriteLine("*** Logging the action! -{0}-", input.MethodBase.Name);
            return getNext()(input, getNext);
        }
    }
    
    public interface IGadget
    {
        void DoSomething();
    }
    
    [Log]
    public class Gadget : IGadget
    {
        public void DoSomething()
        {
            Console.WriteLine("\tI did something!");
        }
    }
    

    【讨论】:

    • 好主意,但需要在配置中进行。
    • 我不确定你在说什么。您的意思是由于某种原因您只能在配置中执行此操作吗?我提供我的解决方案来替代必须在配置文件中进行任何设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    相关资源
    最近更新 更多