Adding Other Unity Registrations. How?

42 views
Skip to first unread message

Virtual

unread,
Oct 18, 2012, 10:08:05 PM10/18/12
to agath...@googlegroups.com
I'm new to Agatha but the Request/Response pattern as presented is overwhelmingly compelling and very well done.
 
I have a web app global configuration file as follows:
 
namespace RequestResponse
{
    using System;
    using System.Reflection;
    using System.Web;
    using Agatha.Common;
    using Agatha.ServiceLayer;
    using Agatha.Unity;
    using Services.Requests;
 
    public class Global : HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            InitializeAgatha();
        }
 
        private static void InitializeAgatha()
        {
            var containerImplementation = typeof(Container);
 
            var requestsAndResponsesAssembly = typeof(HelloWorldRequest).Assembly;
 
            var requestHandlersAssembly = Assembly.GetExecutingAssembly();
 
            new ClientConfiguration(requestsAndResponsesAssembly, containerImplementation).Initialize();
 
            new ServiceLayerConfiguration(
                requestHandlersAssembly,
                requestsAndResponsesAssembly,
                containerImplementation).Initialize();
        }
    }
}
 
That's all great and I got the HelloWorld sample running no worries.
 
But I have registrations in an existing application that uses Entity Framework, Repositories, UnitOfWork (the usual). That currently gets injected like this:
 
namespace RequestResponse
{
    using System.Data.Entity;
    using Contracts;
    using Data;
    using Microsoft.Practices.Unity;
    using Repository;
 
    public static class CompositionRoot
    {
        private static readonly IUnityContainer UnityContainer = new UnityContainer();
 
        public static IUnityContainer Container
        {
            get
            {
                return UnityContainer;
            }
        }
 
        public static void RegisterServices()
        {
            UnityContainer
                .RegisterType(typeof(IRepository<>), typeof(GenericRepository<>))
                .RegisterType(typeof(IUnitOfWork), typeof(UnitOfWork))
                .RegisterType(typeof(DbContext), typeof(DataContext))
                .RegisterInstance(new DbContextAdapter(UnityContainer.Resolve<DbContext>()), new PerRequestLifetimeManager())
                .RegisterType<IObjectSetFactory>(new InjectionFactory(p => p.Resolve<DbContextAdapter>()))
                .RegisterType<IObjectContext>(new InjectionFactory(p => p.Resolve<DbContextAdapter>()));
        }
    }
}
 
I need to use the Agatha Unity container but how can I get it to play nice with my above registrations? I think the existing container methods won't work and I can't work out how to get a direct reference through to the UnityContainer (hope that makes sense).
 
Any ideas?
 
Thank you,
Richard

Davy Brion

unread,
Oct 19, 2012, 3:44:55 AM10/19/12
to agath...@googlegroups.com
Hi Richard,

you can initialize Agatha using your own instance of your container instead of letting Agatha create it. You just need to use a different constructor of the ServiceLayerConfiguration class which takes an instance of IContainer instead of a Type instance.

Simply put, you need to create an instance of Agatha.Unity.Container and pass in CompositionRoot.Container to the Agatha.Unity.Container constructor.  Then you need to pass that Agatha.Unity.Container instance to the ServiceLayerConfiguration constructor which takes an IContainer instance (https://github.com/davybrion/Agatha/blob/master/Agatha.ServiceLayer/ServiceLayerConfiguration.cs#L46)

After doing that, Agatha will use the same container instance as the rest of your application, and you'll be able to inject all of the components you've registered into your request handlers.

hope that helps,
Davy
Reply all
Reply to author
Forward
0 new messages