Difference between revisions of "Unity"
(→= Interceptor durch Vererbung und virtuelle Methoden) |
(→Interceptor durch Vererbung und virtuelle Methoden) |
||
Line 28: | Line 28: | ||
service.DoIt("Hello"); | 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; | ||
+ | } | ||
+ | |||
} | } | ||
} | } | ||
</source> | </source> |
Revision as of 10:08, 9 September 2011
The Unity Framework
Interceptor durch Vererbung und virtuelle 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;
}
}
}