Enterprise Library In-depth analysis and flexible application (7): Further discussion on the integration between PIAB and unity

Source: Internet
Author: User
Tags datetime

In Entelib, PIAB (Policy injection Application Block) and unity are positioned as lightweight AOP frameworks and IOC containers (Container). Through PIAB, we can define some business-independent crosscutting concern in the corresponding Callhandler, through attribute declaration or configuration application to the target method that carries the business logic. The IOC container (or DI container), which is provided by unity, is a good way to implement the dynamic injection of dependence, which enables loose coupling between components, between modules, or between services. UnityContainer

Unity is entirely based on ObjectBuilder2, as the name suggests, a base component for creating objects. OBJECTBUILDER2 provides a highly scalable, policy-based (strategy Based) object creation framework that is not just a unity based component, but also the cornerstone of the entire enterlib and software factory. And PIAB implements the policy injection (Policy injection) through the mechanism of method invocation hijacking (methods call interception). PIAB provides different methods of hijacking mechanisms, most typically based on transparentproxy (reference to my PIAB series) and code generation (such as dynamically generating a subclass that inherits from the target type, Implement policy injection by override the corresponding virtual method, or dynamically generate a type that implements the target interface and implement the corresponding method to implement the policy injection. PIAB needs to create a hijacked (interceptable) object through a special mechanism, and unitycontainer is essentially a container for creating objects, and if UnityContainer can be created in accordance with the requirements of PIAB Hijacked (interceptable) object, then the integration between the two can be achieved. (Source Code downloads from here)

One, Unity 1.2 and Enterlib 4.1 How to achieve integration of both

In my first article in this series, I talked about the integration between PIAB and unity, when we adopted a custom unitycontainerextension implementation, at which point the version was Unity 1.1 and Enterlib 3.1. To the Unity 1.2 and Enterlib 4.1,unity has been widely used throughout the enterlib, Microsoft even through unity to piab a thorough transformation. So, in the newest unity and Piab, the native integration of both is already provided.

Unity and Piab the integration between the two is through a special UNITYCONTAINEREXTENSION--MICROSOFT.PRACTICES.UNITY.INTERCEP Tionextension.interception to achieve. To demonstrate the use of interception, let's create a simple example. This example defines a service synctimeprovisionservice for the delivery of synchronization time, Synctimeprovisionservice implements the interface isynctimeprovision. Synctimeprovisionservice itself does not provide a specific implementation, but through another component Synctimeprovider implementation of the specific synchronization time return, Synctimeprovider implementation interface Isynctimeprovider.

You can view Synctimeprovisionservice and Synctimeprovider as a dependency in the application of the two modules, in order to achieve decoupling between two modules, the adoption of interface based dependencies is the recommended design pattern. Therefore, Synctimeprovisionservice does not depend on Synctimeprovider, but relies on Synctimeprovider interface Isynctimeprovider. We implement dependency injection through constructor injection. To give the reader an intuitive impression of the unity and PIAB integration, I applied a cachingcallhandlerattribute on Synctimeprovider, and if the Callhandler came into effect, The result of the method execution will be cached, and the time will be the same until the cache expires. The corresponding code looks like this:

Using System;
Namespace Artech.interceptableunity
{
  
public interface Isynctimeserviceprovision
{
DateTime GetCurrentTime ();
}
public class Synctimeserviceprovisionservice:isynctimeserviceprovision
{
Public Isynctimeprovider Synctimeprovider
{get; private set;}
Public Synctimeserviceprovisionservice ([Dependency]isynctimeserviceprovider Synctimeserviceprovider)
{
This. Synctimeserviceprovider = Synctimeserviceprovider;
}
#region Isynctimeserviceprovision Members
Public DateTime GetCurrentTime ()
{
return this. Synctimeprovider.getcurrenttime ();
}
#endregion
}
public interface Isynctimeprovider
{
DateTime GetCurrentTime ();
}
[Cachingcallhandler]
public class Synctimeprovider:isynctimeprovider
{
#region Isynctimeserviceprovider Members
Public DateTime GetCurrentTime ()
{
return datetime.now;
}
#endregion
}
}

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.