Question about Unity AOP

Topics: Policy Injection Application Block
Aug 16, 2012 at 7:01 AM

I'm using Unity for AOP, register the mapping in web.config like below:

<register type="IEmployeeBiz" mapTo="EmployeeBiz" name="" >
        <interceptor isDefaultForType="true" type="InterfaceInterceptor"/>
        <policyInjection />
      </register>

in EmployeeBiz, I have a method which has the custom attribute for a Cache handler:

 [Cache(Name="AllInsSet",Type=CacheType.Get)]
        public List<INS_SET> ListAllInsSet()
        {
           var repo = new PolicyRepository<INS_SET>();
           return repo.FindAllInsSet();
        }

When I call the method using the following code, it works. The Cache handler code can be excuted.

var employBiz=UnityController.Container.Resolve<IEmployeeBiz>();
employBiz.ListAllInsSet();

 

But the problem is , when I have another method in employbiz class and calls the method inside the class, the Cache handler is not get excuted:

 [Cache(Name="AllInsSet",Type=CacheType.Get)]
public List<INS_SET> ListAllInsSet()
{
var repo = new PolicyRepository<INS_SET>();
return repo.FindAllInsSet();
}

public List<INS_SET> Test()
{
var temp=ListAllInsSet(); //calling the method from another method within same class

do some other business
}

 

var employBiz=UnityController.Container.Resolve<>(IEmployeeBiz);
employBiz.Test();

 

I tried to add another interceptor "Viutual method interceptor"  and changed the method to virtual but no use.

Who can tell me how to solve this issue?

 

Aug 16, 2012 at 8:23 AM

You should be able to do what you want with a VirtualMethodInterceptor.

So the config would look like this:

<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name="Cache"/>
    <namespace name="Cache"/>

    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>

    <container>

      <extension type="Interception"/>

      <register type="IEmployeeBiz" mapTo="EmployeeBiz" >
        <interceptor isDefaultForType="true" type="VirtualMethodInterceptor"/>
        <policyInjection />
      </register>
      
    </container>

  </unity>

</configuration>

And the code would look something like this (I've simplified your code to return a Guid string):

    public interface IEmployeeBiz
    {
        string ListAllInSet();
        string ListAllInSetFacade();
    }

    public class EmployeeBiz : IEmployeeBiz
    {
        [MyCache(Name = "AllInsSet")]
        public virtual string ListAllInSet()
        {
            return Guid.NewGuid().ToString();
        }

        public string ListAllInSetFacade()
        {
            return ListAllInSet();
        }
    }

Now, because of the caching, the following should all return the same value:

    IUnityContainer container = new UnityContainer().LoadConfiguration();
    var empBiz = container.Resolve<IEmployeeBiz>();
            
    string a = empBiz.ListAllInSet();
    string b = empBiz.ListAllInSet();
    string c = empBiz.ListAllInSetFacade();

    Debug.Assert((a == b) && (a == c));

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com