我应该在哪里注册依赖注入? [英] Where should i register by class in dependency injection?

查看:155
本文介绍了我应该在哪里注册依赖注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了依赖注入,一切正常。我只是困惑在哪里我应该在应用程序中放置我的Registertype<>()函数?

I have implemented Dependency Injection,everything works fine.I am just confused where should i
place my Registertype<>() functions in the application?

推荐答案

通常web.config用于向Unity容器注册所有类型,然后在Global.asax.cs中,您可以使用Web配置部分配置统一容器。如果您需要任何其他详细信息,请告诉我。
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.


请找到配置Unity容器的代码。另外请确保你有< unity>在您的网络配置中您将注册您的类型的部分



确保您采用静态容器变量,以便它可用于所有请求



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

public static IUnityContainer container;





以下代码应存在于Global.asax.cs中.Application_Start方法



Below code should be present in Global.asax.cs Application_Start method

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

container.RegisterType<interface,class>();





还在预处理处理程序中添加以下代码,以便您的类型由容器构建





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

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更改

为团结添加新部分




Web.config changes
Add a new section for unity

<configsections>

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





然后定义统一部分



and then defines the unity section

<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>


<configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>













<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>





这是我的web.config统一部分。



This is my web.config unity section.


这篇关于我应该在哪里注册依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆