views:

112

answers:

2

in my asp.net-mvc application I have a statis MvcApplication that calls a static CreateContainer() method.

In this method I create my unity ioc container:

private static IUnityContainer CreateContainer()
{
    var container = new UnityContainer();
    container.RegisterType<IConfigurationService, ConfigFile>();
    container.RegisterType<ILoggerService, NlogLoggerService>();

    container.RegisterInstance<ISearchService>(
        new LuceneSearchService(
            container.Resolve<IConfigurationService>(),
            container.Resolve<ILoggerService>()),
            new ContainerControlledLifetimeManager());
}

If I understood my sources well, this should give me a singleton LuceneSearchService instance. In my logging however, I can see that my constructor gets hit everytime this instance is requested.

What am I doing wrong?

+1  A: 

I would think that would work, assuming you're resolving an ISearchService and not a LuceneSearchService directly - in that case I think Unity will create a new instance each time as there will be no existing mapping.

Personally, I'd also register it as: container.RegisterType<ISearchService, LuceneSearchService>(new ContainerControlledLifetimeManager());

Lee
All my constructors accept an ISearchService. There is no mentioning of LuceneSearchService, yet the logs aren't making this up...
borisCallens
Thanks for pointing me in the right direction for instantiating my singleton types. Together with the answer of this question I got what I needed
borisCallens
+2  A: 

For a singleton you should move the definition of container outside of the function, and make it static. Set it to null by default.

Then in your CreateContainer function, check if container is null. If it is, create it and initialize it. otherwise, just return it.

private static IUnityContainer container = null;
private static IUnityContainer CreateContainer()
{
    if( container == null )
    {
        container = new UnityContainer();
        container.RegisterType<IConfigurationService, ConfigFile>();
        container.RegisterType<ILoggerService, NlogLoggerService>();

        container.RegisterInstance<ISearchService>(
            new LuceneSearchService(
                container.Resolve<IConfigurationService>(),
                container.Resolve<ILoggerService>()),
                new ContainerControlledLifetimeManager());
    }

    return container;
}
TJMonk15
P.S. Is it just me, or is 'var' the most overused, misused, and misunderstood keyword in c# now?
TJMonk15
+1. Note that it says "ContainerControlledLifetimeManager", not "Singleton". Since the OP is creating a new container every time, the lifetime is very short. Note also that UnityContainer is IDisposable.
TrueWill
I don't use ASP.Net MVC or Unity (if thats something different than MVC?). I just know how to do a singleton :-P
TJMonk15
Thanks for the answer. Could you explain why I shouldn't be using var? As far as I'm aware it's basically the same as writing the type in full, only now my compiler will fill in the gap at compile time and my code becomes less cluttered. Or is there a catch I'm forgetting?
borisCallens
It might just be a personal preference, but I believe that using var hurts readability. If you simply declare a variable as var, another person coming and reading through your code will have to waste time reading your code just to find out what type it is. Sometimes is acceptable to use (mainly for LINQ Queries.) Again, this is just personal preference (and learned from working with many people on projects) and in no way meant to be insulting... :)
TJMonk15
It is a personal preference.
Darren