Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
I have implemented Dependency Injection,everything works fine.I am just confused where should i
place my Registertype<>() functions in the application?
Posted
Updated 18-Dec-13 19:06pm
v3
Comments
SarveshShukla 17-Dec-13 8:23am    
What kind of application your are developing and in which technology?
Sumit Bhargav 17-Dec-13 8:34am    
I am developing a web application in .Net technology.

Normally web.config is used to register all your types with the Unity container and then in Global.asax.cs you can configure your unity container using web config section. Let me know if you need any other detail.
 
Share this answer
 
Comments
Sumit Bhargav 17-Dec-13 8:56am    
Thanks for replying,
I know that global.asax file will be used to implement Unity Container.I am confused in the Pattern of implementing it.I don't know how to configure it.
Please find the code for configuring Unity container. Also please ensure that you have <unity> section in your web config where you would register your types

Ensure that you take a static container variable so that it is available for all request

C#
public static IUnityContainer container;


Below code should be present in Global.asax.cs Application_Start method
C#
container = new Microsoft.Practices.Unity.UnityContainer();
         
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers.Default.Configure(container);


or if you are not using config use below code to register type

C#
container.RegisterType<interface,class>();


Also add below code in prerequest handler so that your types are build by the container

C#
protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    IHttpHandler httpHandler = HttpContext.Current.Handler as System.Web.UI.Page;

    if (httpHandler != null)
    {
        if (unityContainer != null)
        {
            unityContainer.BuildUp(httpHandler.GetType(), httpHandler);
        }
    }
}



Web.config changes
Add a new section for unity
XML
<configsections>

		<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
	</configsections>


and then defines the unity section
XML
<unity>
<containers>
	<container>
		<types>
			<type type="Full Interface Name including Namespace, Assembly Name" mapto="Full class Name including Namespace, Assembly Name">
			</type>
		</types>
	</container>
</containers>
</unity>
 
Share this answer
 
v2
Comments
Sumit Bhargav 18-Dec-13 0:52am    
hi SarveshShukla,
I appreciate your views over registering the classes,i haven't tried it yet but i will try it soon.Can you please explain me a way for registering using classes instead of web.config and then call the class in the Application_Start method?
Sumit Bhargav 18-Dec-13 1:55am    
hi SarveshShukla,
Your code shows an error
"Unrecognised attribute "types""
SarveshShukla 18-Dec-13 4:48am    
I have updated the solution to include all details, it works fine on my machine let me know if you face issues
Sumit Bhargav 18-Dec-13 5:43am    
thanks sarvesh,
i have added
in config section
and
xmlns="http://schemas.microsoft.com/practices/2010/unity" in unity tag
but it says


Resolution of the dependency failed, type = "IBLPotential", name = "". Exception message is: The current build operation (build key Build Key[IBLPotential, null]) failed: The current type, IBLPotential, is an interface and cannot be constructed. Are you missing a type mapping? (Strategy type Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, index 2)
Sumit Bhargav 18-Dec-13 6:31am    
hi sarvesh,
your code is working but i dont know where is the error coming out please have a glance at it.

Could not load type 'IBLPotential' from assembly 'Microsoft.Practices.Unity.Configuration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

This error is coming at
Section.Containers.Default.Configure(Container);
XML
<configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>




XML
<unity>
    <containers>
      <container>
        <types>
          <type type="IBLPotential" mapTo="Website1.App_Code.Potentials.BusinessLogic.BLPotential"></type>
          <type type="IPotentialBinding" mapTo="Website1.App_Code.Potentials.DataAccess.PotentialBinding"></type>
          <type type="IPotentialInfo" mapTo="Website1.App_Code.Potentials.DataAccess.PotentialInfo"></type>
          <type type="IPotentialNew" mapTo="Website1.App_Code.Potentials.DataAccess.PotentialNew"></type>
        </types>
      </container>
    </containers>
  </unity>


This is my web.config unity section.
 
Share this answer
 
v3
This is my web.config file

XML
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>         
  </configSections>
  <unity>
    <containers>
      <container>
        <types>
          <type type="CRMLibrary.IBLPotential,CRMLibrary" mapTo="CRMLibrary.BLPotential,CRMLibrary"></type>
          <type type="CRMLibrary.IPotentialBinding,CRMLibrary" mapTo="CRMLibrary.PotentialBinding,CRMLibrary"></type>
          <type type="CRMLibrary.IPotentialInfo,CRMLibrary" mapTo="CRMLibrary.PotentialInfo,CRMLibrary"></type>
          <type type="CRMLibrary.IPotentialNew,CRMLibrary" mapTo="CRMLibrary.PotentialNew,CRMLibrary"></type>
        </types>
      </container>
    </containers>
  </unity>


This is my .asax file
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
IUnityContainer Container = new UnityContainer();
UnityConfigurationSection Section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
Section.Containers.Default.Configure(Container);
}

This is what i am doing in a class
UnityContainer IoC = new UnityContainer();
IBLPotential BPotential = IoC.Resolve<iblpotential>();
 
Share this answer
 
v3
Comments
Sumit Bhargav 19-Dec-13 1:00am    
Dependency injection has been done and i had implemented interface iblpotential in class blpotential
Sumit Bhargav 19-Dec-13 1:20am    
This is what i am doing in a class
UnityContainer IoC = new UnityContainer();
IBLPotential BPotential = IoC.Resolve<iblpotential>();

but it still gives the error
SoMad 19-Dec-13 1:39am    
Have you solved your problem? From what you wrote here, it sounds like you are still getting an error. If that is the case, you should not post this as a solution and then accept it as the answer.

Soren Madsen
Sumit Bhargav 19-Dec-13 1:40am    
i havn't solved it yet,It's by mistake.sorry
SarveshShukla 19-Dec-13 1:51am    
Sumit, This will not work, you have created a container in application_start method but you are not using it for resolving types, instead you are creating a new IOC variable and using it to resolve the dependency.

Just use the code i gave in Solution 2 and then where ever you want your dependency to be injected for any property do this

[Dependency]
IBLPotential potential {get;set;}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900