Aug 012013
 
Share...Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on LinkedInPin on PinterestShare on TumblrShare on RedditDigg this

ServiceStack is a high-performance .NET web services framework (including a number of high-performance sub-components) that simplifies the development of XML, JSON, JSV and SOAP Web Services. ServiceStack has a large number of components, and it has its own “ecosystem”. One of the functionality ServiceStack exposes is the dependency injection with it’s built-in Funq IoC framework.

pnp_logo

logo-servicestack

Wile Funq is an excellent framework, for a number of reasons you might want to use the framework you are already familiar with like: StructureMap, Ninject, or others. In this post I will show you how to integrate the Microsoft Unity framework with ServiceStack and use it as an IoC.

Implementing the IContainerAdapter

ServiceStack exposes the IContainerAdapter interface for external containers and therefore the first thing that has to be done is to create our own adapter that would use Microsoft Unity.

Implementing the adapter is really trivial. We have to ensure that two methods Resolve<T> and TryResolve<T> are implemented.

/// 
/// IContainerAdapter for the unity framework.
/// 
public class UnityContainerAdapter : IContainerAdapter
{
    private readonly IUnityContainer _unityContainer;

    public UnityContainerAdapter(IUnityContainer container)
    {
        _unityContainer = container;
    }

    public T Resolve()
    {
        return _unityContainer.Resolve();
    }

    public T TryResolve()
    {
        if (_unityContainer.IsRegistered(typeof(T)))
        {
            return (T)_unityContainer.Resolve(typeof(T));
        }

        return default(T);
    }
}

Adapter integration with ServiceStack

We have to accomplish three steps in order to start using the new just created adapter:

  1. Create an instance of the IUnityContainer, as we would always and normally do in any other application and register all the objects we need to.
  2. Create an instance of the UnityContainerAdapter that accepts the instance IUnityContainer in the constructor.
  3. Tell ServiceStack to use the UnityContainerAdapter as the implementation of the IoC container

The full example of the implementation of the AppHost is as follows:

public class Global : HttpApplication
{
    public class FirstServiceStackAppHost : AppHostBase
    {
        public FirstServiceStackAppHost(): 
            base("ServiceStack", typeof(ExampleService).Assembly)
        {                
        }

        public override void Configure(Container container)
        {
            //1. Create the IUnityContainer and register objects
            IUnityContainer unityContainer = new UnityContainer();
            
            //register your objects to the IUnityContainer...
            unityContainer.RegisterType();
            unityContainer.RegisterType();
            unityContainer.RegisterType();
                
            //2. expose the UnityContainer to ServiceStack
            var unityAdapter = new UnityContainerAdapter(unityContainer);

            //3. Tell ServiceStack to use our adapter.
            container.Adapter = unityAdapter;
        }
    }

    protected void Application_Start(object sender, EventArgs e)
    {
        new FirstServiceStackAppHost().Init();
    }
}

…and start using the application.

NuGet

This code is now available as a library in NuGet:

PM> Install-Package ServiceStack.ContainerAdapter.Unity

Source Code

Full source code and an example application is available on CodePlex.

Conclusion

In this post I’ve shown how easy is to integrate Microsoft Unity dependency injection framework with ServiceStack. All This is made possibly by the the framework itself as it offers a very versatile integration interfaces that are clear and clean to use.

For further information aboout IoC and ServiceStack please visit this post.

    Share...Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on LinkedInPin on PinterestShare on TumblrShare on RedditDigg this

    I'm a Software Developer and Solution Architect interested in Software Development, Object-Oriented Design and Software Architecture all this especially bound to the Microsoft.NET platform.Feel free to contact me or know more in the about section

      One Response to “ServiceStack: IoC with Microsoft Unity”

    1. Thanks, exactly what I needed!!!

    Leave a Reply

    coriell@mailxu.com