Software Architecture – C#- Unity-2.0-Creating Instance Registrations

Previous Related posts:
Software Architecture- C# – Unity 2.0 – First lession
Software Architecture C-Sharp Unity-2.0 Second Lession

In the sequence, consider following Unity Code for analysis:

// Create container
UnityContainer container3 = new UnityContainer();
ILogger elogger = new EmailLogger();
// Step 1
Console.WriteLine("Step 1");
// Register an instance
container3.RegisterInstance(elogger);
// Resolve on basis of instance
ILogger logger3 = container3.Resolve<ILogger>();
// call a method
logger3.Write("Hello World....!!!");

// Step 2
Console.WriteLine("Step 2");
// Register named instance
container3.RegisterInstance("Email",elogger);
// Resolve on basis of named istance
ILogger logger33 = container3.Resolve<ILogger>("Email");
// call a method
logger33.Write("Hello World....!!!");

//Step 3
Console.WriteLine("Step 3");
//Register named intance with ContainerControlledLifetimeManager as life time manager
container3.RegisterInstance("Email",elogger , new ContainerControlledLifetimeManager());
// Resolve
ILogger logger333 = container3.Resolve<ILogger>("Email");
// call a method
logger333.Write("Hello World....!!!");

//Register named intance with HierarchicalLifetimeManager as life time manager
container3.RegisterInstance("Email",elogger , new HierarchicalLifetimeManager());
// Resolve
ILogger logger3333 = container3.Resolve<ILogger>("Email");
// call a method
logger3333.Write("Hello World....!!!");                                  
 

The output of code is as:

In Step 1, a instance is registered,then resolved and method is called.
UnityContainerExtensions static class has extension method for UnityContainer which registers a default instance mapping with the container so that it returns the specified instance when a Resolve method requests an instance of the type TInterface (which can be an implemented interface instead of the actual type). The container takes over the lifetime of the instance.
Explained as below:

public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, TInterface instance);    
//
// Summary:
//     Register an instance with the container.
//
// Parameters:
//   container:
//     Container to configure.
//
//   instance:
//     Object to returned.
//
// Type parameters:
//   TInterface:
//     Type of instance to register (may be an implemented interface instead of
//     the full type).
//
// Returns:
//     The Microsoft.Practices.Unity.UnityContainer object that this method was
//     called on (this in C#, Me in Visual Basic).
//
// Remarks:
//      Instance registration is much like setting a type as a singleton, except
//     that instead of the container creating the instance the first time it is
//     requested, the user creates the instance ahead of type and adds that instance
//     to the container.
//     This overload does a default registration and has the container take over
//     the lifetime of the instance.
 

In step 2, a named intance is created and named resolve is called.
UnityContainerExtensions static class has extension method for UnityContainer which registers a named instance mapping with the container so that it returns the specified instance when a Resolve method requests an instance of the type TInterface (which can be an implemented interface instead of the actual type) and the specified name. The container takes over the lifetime of the instance. Names are case sensitive.
Explained as below:

public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, string name, TInterface instance);
//
// Summary:
//     Register an instance with the container.
//
// Parameters:
//   instance:
//     Object to returned.
//
//   container:
//     Container to configure.
//
//   name:
//     Name for registration.
//
// Type parameters:
//   TInterface:
//     Type of instance to register (may be an implemented interface instead of
//     the full type).
//
// Returns:
//     The Microsoft.Practices.Unity.UnityContainer object that this method was
//     called on (this in C#, Me in Visual Basic).
//
// Remarks:
//      Instance registration is much like setting a type as a singleton, except
//     that instead of the container creating the instance the first time it is
//     requested, the user creates the instance ahead of type and adds that instance
//     to the container.
//     This overload automatically has the container take ownership of the instance.

In step 3, a named intance is created and named resolve is called with container life manager specified.
UnityContainerExtensions static class has extension method for UnityContainer which registers a named instance mapping with the container so that it returns the specified instance when a Resolve method requests an instance of the type TInterface (which can be an implemented interface instead of the actual type) and the specified name. Names are case sensitive. Also registers the specified LifetimeManager instance with the container to manage the lifetime of the returned object.

Explained as below:

public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, string name, TInterface instance, LifetimeManager lifetimeManager);   
//
// Summary:
//     Register an instance with the container.
//
// Parameters:
//   instance:
//     Object to returned.
//
//   container:
//     Container to configure.
//
//   name:
//     Name for registration.
//
//   lifetimeManager:
//     Microsoft.Practices.Unity.LifetimeManager object that controls how this instance
//     will be managed by the container.
//
// Type parameters:
//   TInterface:
//     Type of instance to register (may be an implemented interface instead of
//     the full type).
//
// Returns:
//     The Microsoft.Practices.Unity.UnityContainer object that this method was
//     called on (this in C#, Me in Visual Basic).
//
// Remarks:
//      Instance registration is much like setting a type as a singleton, except
//     that instead of the container creating the instance the first time it is
//     requested, the user creates the instance ahead of type and adds that instance
//     to the container.

Download complete source code here.
[After downloading the file, rename “unitytestapp-zip.doc” to “unitytestapp.zip” and extract the files]

Enjoy coding Unity 2.0….

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: