@GSerjo, एकता अवरोधन दृष्टिकोण है जो अच्छी तरह से काम करता है को रेखांकित किया गया है। यदि आप अवरोध की कॉन्फ़िगरेशन को स्वचालित करना चाहते हैं तो आप सभी इंटरफ़ेस अवरोधों के साथ-साथ व्यवहार को स्वचालित रूप से वायरस करने के लिए एक यूनिटीकॉन्टेनर एक्सटेंशन का उपयोग कर सकते हैं। यदि आप अधिक विशिष्ट अवरोध (विधि नाम, हस्ताक्षर, वापसी मूल्य इत्यादि) में जाना चाहते हैं तो आपको शायद पॉलिसी इंजेक्शन (कॉलहैंडलर के साथ मिलान नियमों का उपयोग करके) देखना होगा।
public class UnityInterfaceInterceptionRegisterer : UnityContainerExtension
{
private List<Type> interfaces = new List<Type>();
private List<IInterceptionBehavior> behaviors =
new List<IInterceptionBehavior>();
public UnityInterfaceInterceptionRegisterer(Type interfaceType,
IInterceptionBehavior interceptionBehavior)
{
interfaces.Add(interfaceType);
behaviors.Add(interceptionBehavior);
}
public UnityInterfaceInterceptionRegisterer(Type[] interfaces,
IInterceptionBehavior[] interceptionBehaviors)
{
this.interfaces.AddRange(interfaces);
this.behaviors.AddRange(interceptionBehaviors);
ValidateInterfaces(this.interfaces);
}
protected override void Initialize()
{
base.Container.AddNewExtension<Interception>();
base.Context.Registering +=
new EventHandler<RegisterEventArgs>(this.OnRegister);
}
private void ValidateInterfaces(List<Type> interfaces)
{
interfaces.ForEach((i) =>
{
if (!i.IsInterface)
throw new ArgumentException("Only interface types may be configured for interface interceptors");
}
);
}
private bool ShouldIntercept(RegisterEventArgs e)
{
return e != null && e.TypeFrom != null &&
e.TypeFrom.IsInterface && interfaces.Contains(e.TypeFrom);
}
private void OnRegister(object sender, RegisterEventArgs e)
{
if (ShouldIntercept(e))
{
IUnityContainer container = sender as IUnityContainer;
var i = new Interceptor<InterfaceInterceptor>();
i.AddPolicies(e.TypeFrom, e.TypeTo, e.Name, Context.Policies);
behaviors.ForEach((b) =>
{
var ib = new InterceptionBehavior(b);
ib.AddPolicies(e.TypeFrom, e.TypeTo, e.Name, Context.Policies);
}
);
}
}
}
तो फिर तुम यह इतना की तरह इस्तेमाल कर सकते हैं:
तो इस मामले में कंटेनर विस्तार कैसा दिखेगा
IUnityContainer container = new UnityContainer()
.AddExtension(new UnityInterfaceInterceptionRegisterer(
new Type[] { typeof(IMyInterface),
typeof(IMyOtherInterface) },
new IInterceptionBehavior[] { new MyInterceptionBehavior(),
new AnotherInterceptionBehavior() }
));
container.RegisterType<IMyInterface, SpecificClass1>();
var myInterface = container.Resolve<IMyInterface>();
myInterface.SomeMethod();
अब जब इंटरफ़ेस पंजीकृत किया गया है उचित अवरोधन नीतियों भी हो जाएगा कंटेनर में जोड़ा गया। तो इस मामले में यदि पंजीकृत इंटरफ़ेस IMYInterface या IMyOtherInterface प्रकार है तो नीतियों को इंटरफ़ेस अवरोध के लिए सेटअप किया जाएगा और इंटरसेप्शन व्यवहार MyInterceptionBehavior और AnotherInterceptionBehavior भी जोड़ा जाएगा।
ध्यान दें कि एकता 3 (इस प्रश्न/उत्तर के बाद जारी) ने Registration by Convention सुविधा जोड़ा जो यह एक्सटेंशन कर सकता है (कोई कस्टम कोड लिखने के बिना)। Developer's Guide to Dependency Injection Using Unity:
var container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterTypes(
AllClasses.FromLoadedAssemblies().Where(
t => t.Namespace == "OtherUnitySamples"),
WithMappings.MatchingInterface,
getInjectionMembers: t => new InjectionMember[]
{
new Interceptor<VirtualMethodInterceptor>(),
new InterceptionBehavior<LoggingInterceptionBehavior>()
});
से एक उदाहरण