Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
Programming Blogs - Blog Catalog Blog Directory
 
 
 

Links

Social

Unity and singletons

I just putted up Unity behind my test project and had some troubles with singletons. There is some misleading information in documentation you should be aware of. I created one class and one interface, both in same project to save some time.


namespace mytest
{
    public interface ITest
    {
        string Name { get; set; }
    }
    public class Test : ITest 
    {
        public string Name { get; set; }  
    }
}

So, as we can see, it's nothing special. Now let's take application configuration file. You can see tag called lifetime there. Example in manual uses "singleton" as type attribute of this tag. Instead of it, to avoid errors, you should use correct name of the LifetimeManager.


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configsections>
    <section name="unity"
 
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                  Microsoft.Practices.Unity.Configuration"
 />

  </configsections>

 

  <unity>
    <containers>
      <container>
        <types>
          <type type="csharptest.ITest,csharptest"
                mapTo="csharptest.Test,csharptest"
         >

            <lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,
    Microsoft.Practices.Unity"
 />

          </type>
        </types>
      </container>
    </containers>
  </unity>
</configuration>


And now, let's see my little test code to test Unity and singleton. Point of this code is easy - we are creating Unity container and ask tqo ITest type objects from it. To make sure they are the same we will compare their hash codes.


namespace mytest
{
    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section;
            section =(UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers.Default.Configure(container);
                      
            ITest test1 = container.Resolve<ITest>();
            ITest test2 = container.Resolve<ITest>();
            Console.WriteLine(test1.GetHashCode().ToString());
            Console.WriteLine(test2.GetHashCode().ToString());
            Console.ReadLine();
        }
    }
}

I hope it helps you!

Posted: Apr 13 2008, 10:17 PM by DigiMortal | with 3 comment(s)
Filed under: ,

Comments

uTILLIty said:

yes, I thought I had it working with "singleton" and suddenly I got errors. Thanks for the quick "point in the right direction"

# July 24, 2008 9:40 AM

uTILLIty said:

BTW: if you wanted to use the alias, just register it in the unity config-section:

<typeAliases>

 <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />

</typeAliases>

# July 24, 2008 9:44 AM

Gunnar Peipman's ASP.NET blog said:

Loggers are one of most popular examples about interfaces for sure. And there are a lot of implementations

# March 26, 2009 5:04 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)