Unity

From no name for this wiki
Jump to: navigation, search

The Unity Framework

Interceptor durch Vererbung und virt. Methoden

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;
 
namespace UnityFramework
{
    class Program
    {
        static void Main(string[] args)
        {
 
            IUnityContainer container = new UnityContainer();            
            container.AddNewExtension<Interception>();
 
 
            container.RegisterType<Service>();
            container.Configure<Interception>().SetInterceptorFor<Service>(new VirtualMethodInterceptor());
 
            var service = container.Resolve<Service>();
            service.DoIt("Hello");
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
 
namespace UnityFramework
{
 
    public interface IService
    {
         [Watch]
         string DoIt(string param);
    }
 
    public class Service : IService
    {
        [Transaction]
        public virtual string DoIt(string param)
        {
            Console.WriteLine("Hello World");
            return "HelloWorld";
        }
    }
 
 
    public class WatchAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new WatchHandler();
        }
    }
 
    public class WatchHandler : ICallHandler
    {
        public int Order { get; set; }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            Console.WriteLine("WatchHandler");
            var arg0 = input.Arguments[0];
            var result =  getNext()(input, getNext);
            Console.WriteLine("NextOne");
            return result;
        }
 
    }
 
    public class TransactionAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new TransactionHandler();
        }
    }
 
    public class TransactionHandler : ICallHandler
    {
        public int Order { get; set; }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            Console.WriteLine("TransactionHandler");
            var arg0 = input.Arguments[0];
            var result = getNext()(input, getNext);
            Console.WriteLine("NextOne");
            return result;
        }
 
    }
}

Interface interceptor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;
 
namespace UnityFramework
{
    class Program
    {
        static void Main(string[] args)
        {
 
            IUnityContainer container = new UnityContainer();            
            container.AddNewExtension<Interception>();
            //container.RegisterType<IService, Service>();
 
            container.RegisterType<IService, Service>();
 
            //container.Configure<Interception>().SetInterceptorFor<IService>(new InterfaceInterceptor());
            container.Configure<Interception>().SetInterceptorFor<Service>(new VirtualMethodInterceptor());
 
 
            var service = container.Resolve<IService>();
            service.DoIt("Hello");
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
 
namespace UnityFramework
{
 
    public interface IService
    {
         [Watch]
         string DoIt(string param);
    }
 
    public class Service : IService
    {
        [Transaction]
        public virtual string DoIt(string param)
        {
            Console.WriteLine("Hello World");
            return "HelloWorld";
        }
    }
 
 
    public class WatchAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new WatchHandler();
        }
    }
 
    public class WatchHandler : ICallHandler
    {
        public int Order { get; set; }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            Console.WriteLine("WatchHandler");
            var arg0 = input.Arguments[0];
            var result =  getNext()(input, getNext);
            Console.WriteLine("NextOne");
            return result;
        }
 
    }
 
    public class TransactionAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new TransactionHandler();
        }
    }
 
    public class TransactionHandler : ICallHandler
    {
        public int Order { get; set; }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            Console.WriteLine("TransactionHandler");
            var arg0 = input.Arguments[0];
            var result = getNext()(input, getNext);
            Console.WriteLine("NextOne");
            return result;
        }
 
    }
 
    public class BinaryBehavior : IInterceptionBehavior
    {
 
        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }
 
 
        public bool WillExecute
        {
            get { return true; }
        }
 
 
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
 
            // Perform the operation
            var methodReturn = getNext().Invoke(input, getNext);
 
            // Grab the output
            var result = methodReturn.ReturnValue;
 
            return methodReturn;
        }
 
    }
}

registering the unity2 way

Die Attribute werden durch PolicyInjectionBehavior gelesen:

IUnityContainer container = new UnityContainer();            
container.AddNewExtension<Interception>();
 
container.RegisterType<IService, Service>(
    new Interceptor<InterfaceInterceptor>(),
    new InterceptionBehavior<PolicyInjectionBehavior>(), 
    new InterceptionBehavior<BinaryBehavior>());          
 
 
 
var service = container.Resolve<IService>();
service.DoIt("Hello");