The analysis of WCF Technology VII: How to realize the integration between WCF and enterlib PIAB and unity

Source: Internet
Author: User
Tags abstract definition static class

Before that, I wrote an in-depth introduction to Ms Enterlib PIAB (see "Ms Enterprise Library Policy Injection Application Block in-depth resolution [summary]"), Also wrote WCF integration with PIAB (see: WCF Follow-up Tour (8): implementation of integration with MS Enterprise Library Policy injection block via WCF Extension), Integration of WCF with unity (see WCF Follow-up (7): Integration through WCF extension implementation and Enterprise Library Unity Container) and unity and PIAB integration (see Enterprise Library in-depth analysis and flexible application (1): Through Unity extension implementation and policy injection Application Block integration, "Enterprise Library in-depth analysis and flexible application (7): Again on the integration between PIAB and unity "). As part of the implementation is based on enterlib, unity, the previous version, in the new version (Enterlib V4.1 and Unity 1.2), MS through unity Piab to the redesign and implementation, so we need to regain this topic, Talk about the new enterlib and unity, how to integrate PIAB and unity into WCF.

A brief introduction to the principle of design

In Enterlib, PIAB and unity are positioned respectively as lightweight IOC Container (or di Container) and AOP frameworks. Piab adopts the method of call interception to implement the dynamic injection of the strategy, which relies on the creation of Interceptable objects; UnityContainer based on ObjectBuilder2, is essentially a container for object creation. Therefore, we can realize the integration between unity and PIAB by creating interceptable objects according to the PIAB requirements of UnityContainer (see Enterprise Library in-depth analysis and flexible application (7): Again on the integration between PIAB and unity ").

The integration between unity and WCF is essentially about having WCF use UnityContainer for the creation of service instances. Within the WCF framework, the creation of a service instance is a special object--instanceprovider. So we can realize the integration between unity and WCF by customizing the Instanceprovider and providing the service instance with the help of UnityContainer. Therefore, creating a instanceprovider based on UnityContainer is the key.

Second, create a instanceprovider:unityinstanceprovider based on UnityContainer

Within the WCF framework, Instanceprovider users to provide service instances. All Instanceprovider implement the interface System.ServiceModel.Dispatcher.IInstanceProvider, and the following is the definition of iinstanceprovider. Service instances provide implementations in getinstance, while ReleaseInstance is used to implement the release of service instances and resource reclamation.

1:public Interface Iinstanceprovider
2: {
4: Object getinstance (InstanceContext instancecontext);
5: Object getinstance (instancecontext instancecontext, message message);
6: void ReleaseInstance (InstanceContext InstanceContext, object instance);
7:}

Our goal now is to create a unity based Instanceprovider, with UnityContainer to provide the implementation of the GetInstance method, Let me call this custom Instanceprovider the Unityinstanceprovider. Before I formally introduce the specific implementation of Unityinstanceprovider, let me introduce the definition of a secondary type: unitytypemapping.

We know that UnityContainer uses a dynamically registered interface or abstract class to match a specific type of relationship, allowing us to use Unitycontaner to implement interfaces or abstract classes to create objects of the specific classes we want. Unitytypemapping is used to describe the matching of types, which are defined as follows. Type and mapto represent the corresponding type (interface or abstract class) to the matched type (the concrete Class), and name indicates the name of the mapping entry (uniquely identified).

1:public class Unitytypemapping
2: {
3: Public Type Type
4: {get; set;}

6: Public Type Mapto
7: {get; set;}

9: Public string Name
0: {get; set;}
1:}

Unity can be programmed and configured to achieve the type of matching, in real system development, the latter is preferred. To implement the transformation between type matching configuration (unitytypeelementcollection) to our defined unitytypemapping list (ilist<unitytypemapping>), I defined one of the following extension methods (Extension method): Copy.

 1:public Static Class Extension 
2: {
3:public static ilist<unitytypemapping> Copy (This Unitytypeel Ementcollection unitytypeelements)
4: {
5:ilist<unitytypemapping> mappings = new List<unitytyp Emapping> ();
6:foreach (unitytypeelement type in unitytypeelements)
7: {
8:mappings. ADD (new unitytypemapping {type = type. Type, Mapto = type. Mapto, Name = type. Name});
9:}
0:
1:return mappings
2:}
3:}

The specific definitions of Unityinstanceprovider are listed below. The property Contracttype and container represent the service contract and the UnityContainer object that is used to create the service instance, and the field _registeredtypemapping represents the current UnityContainer-related type matching collection. For performance reasons, in order to avoid frequent unitycontainer creation and frequent parsing of type-matching relationships, I save them through two static properties | fields (Containers and registeredtypemappings, Key is the name of the container). Accept two input parameters in the constructor: Contracttype and ContainerName, respectively, representing the service contract type and the corresponding UnityContainer name. Based on ContainerName, determine if the corresponding UnityContainer has been created, and if so, extract the corresponding UnityContainer and type matching list directly from the two static variables described above. Otherwise, recreate the unitycontainer and load the appropriate configuration information to configure it. In particular, a special Unitycontainerextension:extendedintercepiton is added when initializing the UnityContainer that is created, The Unitycontainerextension users realize unity and PIAB integration, in the "Enterprise Library in-depth analysis and flexible application (7): Talk about the integration between PIAB and unity" The realization principle of Extendedintercepiton is introduced in detail.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.