David Hayden [MVP C#]

Sponsors

The Lounge

News

  • CodeBetter.Com Home

Other Links

Teas

Patterns & Practices

Florida .NET Developer

Book Reviews

Tampa ASP.NET MVC Developer Group

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Dependency Injection and Policy Injection Application Block - Caching and StructureMap Example

On May 24th my children are out for the summer and my status of Part-Time Stay-at-Home Dad is happily elevated to more of a Full-Time Position. I so look forward to the summers where we can spend more time at the beach, kayaking on the bay, fishing, and hanging out at a number of places where we enjoy yearly passes.

Since it ain't summer yet, however, I wanted to continue my little progression on the Policy Injection Application Block that I talked about in the following 2 posts:

One of the things that had irked me is that we don't have a Dependency-Injection Tool from Microsoft and the Policy Injection Application Block doesn't provide any dependency injection facilities. I say "had", because I don't truly know that they could create a good dependency-injection tool or at least be as responsive in creating new versions and maintenance releases as the OSS Community.

We can use a third-party Dependency-Injection Tool, like StructureMap, and use it with the Policy Injection Application Block to get both dependency injection and Aspect-Oriented Programming capabilities.

 

Getting Object Instances

If you are using StructureMap for your Dependency Injection, you are probably calling ObjectFactory directly to get your object instances:

 

T instance = ObjectFactory.GetInstance<T>();

 

As cool as StructureMap is, I still like creating a wrapper for it just so that I can perhaps switch to another tool or strategy for getting my object instances. I tend to wrap it up in an IoC Class:

 

public static class IoC
{
    public static T Resolve<T>()
    {
        return ObjectFactory.GetInstance<T>();
    }
}

 

I could see where this may seem like a waste of time, except now that the Policy Injection Application Block has been released, I can now sneak it into my IoC Class and my client code is none the smarter:

 

public static class IoC
{
    public static T Resolve<T>()
    {
        // Get From StructureMap
        T instance = ObjectFactory.GetInstance<T>();

        // Wrap Using Policy Injection Application Block
        return PolicyInjection.Wrap<T>(instance);
    }
}

 

Back To Caching

So now if we go back to the IService as talked about in the last couple of posts where I want to simply cache the data for 30 minutes in this case:

 

public interface IService
{
    IList GetData();
}

public class Service : IService
{
    [CachingCallHandler(0,30,0)]
    public IList GetData()
    {
        return new ArrayList();
    }
}

 

Registering and obtaining the service is pretty simple:

 

class Program
{
    static void Main(string[] args)
    {
        // Register Service
        StructureMapConfiguration.UseDefaultStructureMapConfigFile = false;
        StructureMapConfiguration.BuildInstancesOf<IService>().
            TheDefaultIsConcreteType<Service>().AsSingletons();

        // Get Service
        IService service = IoC.Resolve<IService>();
    }
}

 

Here we get the best of both worlds - dependency injection via a 3rd party tool and the Policy Injection Application Block.

Cool Stuff!

 

Recent Enterprise Library Posts

 

Drinking: Gyokuro Green Tea

 


Posted Sun, Apr 29 2007 6:27 PM by David Hayden
Filed under:

[Advertisement]

Comments

Ayende Rahien wrote re: Dependency Injection and Policy Injection Application Block - Caching and StructureMap Example
on Sun, Apr 29 2007 10:57 PM

David,

Just to point out a flaw with that approach, the IoC is not aware of the policy block.

This means that if I IService directly, I get caching, but if FooService got an IService from the IoC, it doesn't have caching.

David Hayden wrote re: Dependency Injection and Policy Injection Application Block - Caching and StructureMap Example
on Mon, Apr 30 2007 12:03 AM

Ohhh..... Excellent point. That is a bad flaw :( and now I won't be able to sleep tonight because that will be on my mind.

Here is where I need a way to hook into the StructureMap pipeline so that I can slip in a call to the Policy Injection Application Block and have StructureMap return the result or add it as a dependency in another object. My guess is that Jeremy thought about this and I need to read up on the API a bit more.

Another way is to call the PIAB before registering the instance, but that won't work if I want to register services via configuration.

Jeremy D. Miller wrote re: Dependency Injection and Policy Injection Application Block - Caching and StructureMap Example
on Mon, Apr 30 2007 5:50 AM

David & Ayende,

The answer is yes, you can hook into the pipeline.  StructureMap has the idea of "Interceptor's" in the object construction pipeline.  It's what it uses to put caching in in the first place.  There is a way to write your own and plug it in.  You do have to revert to xml configuration because I left it out of the programmatic API -- but this is the 2nd time it's come up so I'm definitely adding it for 2.1 this summer.

I'll write it up sometime this week.

Interesting finding - 04/30/2007 « Breaking the bottleneck… wrote Interesting finding - 04/30/2007 &laquo; Breaking the bottleneck&#8230;
on Tue, Jan 22 2008 3:28 PM

Pingback from  Interesting finding - 04/30/2007 &laquo; Breaking the bottleneck&#8230;