Welcome to MSDN Blogs Sign in | Join | Help

News


  • All information or content in this site is provided "AS IS" with no warranties, and confer no rights. The views expressed by me do not necessarily reflect the official policy, position, or opinions of the company where I work.





Unity 1.2 and PIAB: Creating a Unity Extension that auto configures the container with PIAB policies

Following my last post around Unity and PIAB, and as already mentioned, PIAB policies have to be explicitly applied to a Unity Container.

But what about if you want to apply always the PIAB policies to your container? Easy, apply an extension that does that for you:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;

namespace MyExtensions
{
    public class PolicyInjectionInterceptionExtension: Microsoft.Practices.Unity.InterceptionExtension.Interception
    {
        protected override void Initialize()
        {
            base.Initialize();

            // Get the PIAB config out of entlib config
            var configSource = ConfigurationSourceFactory.Create();
            var section = (PolicyInjectionSettings)configSource.GetSection(PolicyInjectionSettings.SectionName);
            // Apply PIAB settings, if any, to the container
            if (section != null)
            {
                section.ConfigureContainer(this.Container, configSource);
            }

        }

    }
}

 

So, instead of adding the standard Microsoft.Practices.Unity.InterceptionExtension.Interception extension, you can add MyExtensions.PolicyInjectionInterceptionExtension which auto configures the container for you.

Pretty easy, right?

Comments

No Comments

Anonymous comments are disabled
Page view tracker