none
Unity: What is the differents between two way of declaring singletone. RRS feed

  • Question

  • I use Unity. I want to declare singletone value

    Method 1.

    myContainer.RegisterType<MySingletonObject>(new ContainerControlledLifetimeManager());

    Method 2.

    container.RegisterInstance<IUserProvider>(new UserProvider());

    In both cases i will got instance wich exist only one for all application.

    Which is correct ? And what is the difference between them ?


    My .NET Blog with projects and feedback.

    Since May 30, 2014 I am waiting for Microsoft fix these 2 bug. If you know how to speed them up, please help

    Friday, February 13, 2015 10:10 AM

Answers

  • >>Which is correct ?

    Both.

    Both methods will return a singleton object when you resolve the type but Method 1 is a bit more explicit since you explicitly specify the ContainerControlledLifetimeManager when you register the type.

    But since Unity uses this lifetime manager (ContainerControlledLifetimeManager) by default for the RegisterInstance method if you do not specify a different lifetime manager, the results are effectively the same.

    I personally prefer this one though but from a technical perspective it doesn't really matter which one you use:

    myContainer.RegisterType<MySingletonObject>(new ContainerControlledLifetimeManager());

    Please rememeber to close your threads by marking helpful posts as answer.

     

     

    • Marked as answer by SmartWhy Friday, February 13, 2015 4:15 PM
    Friday, February 13, 2015 12:08 PM

All replies

  • the link can assist you:>

    http://www.yoda.arachsys.com/csharp/singleton.html

    https://msdn.microsoft.com/en-us/library/ee817670.aspx


    Mark as answer or vote as helpful if you find it useful | Ammar Zaied [MCP]

    Friday, February 13, 2015 10:24 AM
  • >>Which is correct ?

    Both.

    Both methods will return a singleton object when you resolve the type but Method 1 is a bit more explicit since you explicitly specify the ContainerControlledLifetimeManager when you register the type.

    But since Unity uses this lifetime manager (ContainerControlledLifetimeManager) by default for the RegisterInstance method if you do not specify a different lifetime manager, the results are effectively the same.

    I personally prefer this one though but from a technical perspective it doesn't really matter which one you use:

    myContainer.RegisterType<MySingletonObject>(new ContainerControlledLifetimeManager());

    Please rememeber to close your threads by marking helpful posts as answer.

     

     

    • Marked as answer by SmartWhy Friday, February 13, 2015 4:15 PM
    Friday, February 13, 2015 12:08 PM