Integration with mvc2 and unity, how to make the service singleton?

Apr 26, 2011 at 8:30 AM
Edited Apr 26, 2011 at 8:49 AM

We integrated mvc2 and unity by our own ControllerFactory in mvc2. In the factory we resolve the service reference from unity container when the mvc controller recreating.

So I made the service held in the unity container as  singleton, and it successed.

but when I want to intercept the service by unity'PolicyDefinition , the service will recreate(different hashcode) everytime when resolved. Unfortunately, the proccess take much more time than singleton.

Any idea?

 

The cotroller create method

 

public IController CreateController(RequestContext requestContext, string controllerName) {
            if (requestContext == null) {
                throw new ArgumentNullException("requestContext");
            }

            if (string.IsNullOrEmpty(controllerName))
            {
                return null;
            }
            Type type = base.GetControllerType(requestContext,controllerName);
            if (type == null)
            {
                return null;
            }
            IController icontroller = container.Resolve(type) as IController;
            return icontroller;
        }

 

the interceptor method

 

private static void RegisterTransactionInterceptor(IUnityContainer container)
        {
             
            List<string> transNameSpace = new List<string>();
            transNameSpace.Add("Relaship.Core");
             
            List<string> memberNameMatching = new List<string>();
            memberNameMatching.Add("Insert*");
            memberNameMatching.Add("Update*");
            memberNameMatching.Add("Delete*");
            memberNameMatching.Add("Add*");
            memberNameMatching.Add("Remove*");
            memberNameMatching.Add("Clear*");
            memberNameMatching.Add("Change*");
            memberNameMatching.Add("Save*");
            memberNameMatching.Add("Del*");
            memberNameMatching.Add("Init*");
            

            #region
            container.AddNewExtension<Interception>();
            container.RegisterInstance<ICallHandler>("TransactionCallHandler", new TransactionInterceptor());


            foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
            {
                if (!type.IsInterface)
                {
                    continue;
                }
                if (transNameSpace.Contains(type.Namespace))
                {
                    container.Configure<Interception>().SetDefaultInterceptorFor(type, new InterfaceInterceptor());
                }
            }

            PolicyDefinition pd = container.Configure<Interception>().AddPolicy("Transaction");
            pd.AddMatchingRule(new MemberNameMatchingRule(memberNameMatching, true));
            pd.AddCallHandler("TransactionCallHandler");
            #endregion
        }

the service register method

        private static void RegisterServices(IUnityContainer container)
        {
            Type serviceType = typeof(IServiceBase);
            Type[] types = Assembly.GetExecutingAssembly().GetTypes();
            foreach (Type type in types)
            {
                if (type.IsInterface && serviceType.IsAssignableFrom(type))
                {
                    Type implType = GetServiceImplementation(type);
                    if (implType != null)
                    {
                        if (implType.GetCustomAttributes(typeof(CircularReference), true).Length > 0)
                        {
                            container.RegisterType(type, implType, new PerResolveLifetimeManager());
                        }
                        else
                        {
                            container.RegisterType(type, implType, new ContainerControlledLifetimeManager());
                        }
                    }
                }
            }
        }

thanks in advance!!

Apr 28, 2011 at 10:44 AM

Hi,

Personally, I never had done any perf testing on Unity myself. Though as far as I know, from what I got from the other related threads regarding performance, there is an overhead in resolving from the container.

Here are some of the related thread I was saying:

http://unity.codeplex.com/discussions/26032

http://unity.codeplex.com/discussions/37992

http://unity.codeplex.com/discussions/231444

I think one thing to note is to make sure that the Container creation and type registration doesn't happen multiple times since this will also consume performance hit.

HTH.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

 

 

Apr 28, 2011 at 10:53 AM

thanks

Now I held a static var to store the service ref that resovled from unity container myself.

It works fine!!