Use the scene?
Many times, we define a function, when we want to extend this function, in accordance with conventional thinking, we generally use the idea of OOP, in the original function of the expansion.
So is there a thing that can be implemented when we need to extend this functionality without modifying the original function code, which is what we want to say to unity.
1. Preparatory work
Add a NuGet package for your project, search for unity and install it.
Add a related reference to unity in the project you are using
using Microsoft.Practices.Unity.InterceptionExtension; using Microsoft.Practices.Unity;
2. Hypothetical scenario
as stated above, unity can be implemented without modifying the original functionality to add additional extension features. in our actual development, we can also give a simple example.
When we go to do a user registration function, the initial version is to complete the basic registration function, later we need to expand, and give him a few features of registration verification, log processing, and exception capture, then the next demonstration, how to use unity to extend the function.
3. How to use
First, we define a primitive registration function.
Public Static void Show () { new User () { "Eleven", " 123123123123 " };
Iuserprocessor porcessor = new Userprocessor ();
Porcessor. Reguser (user); Simple user Registration
}
public interface Iuserprocessor { void Reguser (user user); public class userprocessor:iuser Processor// marshalbyrefobject, { public void Reguser (user user) {Console.WriteLine ( "register " }
Next, we're going to extend this registration by adding a few features for registration checking, log processing, and exception snapping.
1. First define 3 attributes and corresponding characteristic behavior implementations, namely registration, log, and exception.
Public classuserhandlerattribute:handlerattribute//register Check { Public Overrideicallhandler Createhandler (Iunitycontainer container) {Icallhandler handler=NewUserhandler () {Order = This. Order}; returnhandler; } } Public classLoghandlerattribute:handlerattribute//log processing { Public Overrideicallhandler Createhandler (Iunitycontainer container) {return NewLoghandler () {Order = This. Order}; } } Public classExceptionhandlerattribute:handlerattribute//exception handling { Public Overrideicallhandler Createhandler (Iunitycontainer container) {return NewExceptionhandler () {Order = This. Order}; } }
corresponding to each of the characteristics of the implementation of the behavior, respectively, to implement registration check, logging, and exception handling
Public classUserhandler:icallhandler//Registration check behavior { Public intOrder {Get;Set; } PublicImethodreturn Invoke (imethodinvocation input, getnexthandlerdelegate getNext) {User user /c5>= input. inputs[0] asUser; if(User. Password.length <Ten) { returnInput. Createexceptionmethodreturn (NewException ("password length cannot be less than 10 bits")); } Console.WriteLine ("Correct parameter detection"); Imethodreturn Methodreturn=Getnext.invoke (). Invoke (input, getNext); returnMethodreturn; } } Public classLoghandler:icallhandler//log processing behavior { Public intOrder {Get;Set; } PublicImethodreturn Invoke (imethodinvocation input, getnexthandlerdelegate getNext) {User user /c8>= input. inputs[0] asUser; stringMessage =string. Format ("Reguser:username:{0},password:{1}", user. Name, user. Password); Console.WriteLine ("log logged, message:{0},ctime:{1}", message, DateTime.Now); returngetNext () (input, getNext); } } Public classExceptionhandler:icallhandler//behavior of exception handling { Public intOrder {Get;Set; } PublicImethodreturn Invoke (imethodinvocation input, getnexthandlerdelegate getNext) {Imethodretu RN Methodreturn=getNext () (input, getNext); if(Methodreturn.exception = =NULL) {Console.WriteLine ("no exception"); } Else{Console.WriteLine ("exception: {0}", MethodReturn.Exception.Message); } returnMethodreturn; } }
According to the current thinking, we have to add a few of the above-written functions on the original registration function, first, we go back to the beginning of the definition of the interface, to the interface to add our definition of the 3 features
1 )]//Registration check 2)]/ /log processing 3)]//Legacy processing Public Interface Iuserprocessor { void reguser (user user); }
PS: In the above feature declaration, each corresponding order is ordered, relative to the order of execution of a behavior, this internal is an implementation of unity, so we use the process only need to declare a good scalar.
Then, in a well-defined registration method, we first declare a unity container unitycontainer, then register its interface iuserprocessor, and finally call the registration method of its interface.
// declaring a container New UnityContainer (); // declare unitycontainer and register Iuserprocessor Container. Registertype<iuserprocessor, userprocessor>(); Container. Addnewextension<Interception> (). Configure<interception>() . Setinterceptorfor<IUserProcessor> (new interfaceinterceptor ()); = Container. Resolve<iuserprocessor>(); Userprocessor. Reguser (user); Call the registration method.
Finally, let's take a look at the actual effect, and it's easy to implement more behavior when registering.
Summary:
It is not difficult to discover that unity's implementation is primarily based on the AOP functionality defined on the interface and implemented with the implementation behavior in conjunction with its internal unity container.
Because the above is a static writing, easy to learn, the real implementation of AOP can be dynamically configured, in the IOC will be described in detail.
C # Unity Dependency Injection leverages attribute for AOP functionality