Unity [Depedency] Annotation不是依赖本身吗?

Ath*_*ras 9 c# annotations dependency-injection unity-container

我正在开发一个.Net Web Forms项目结构,我决定使用Unity作为应用程序的DI框架.

MSDN-Asp.Net中的解析说明为了在项目中注入依赖项,我需要构建DI容器外部创建的初始对象.话虽如此,我们来到这个问题.

  1. 属性注释,例如[Dependency]扩展Attribute类的类.为了使用它们,必须在声明类中包含另一个命名空间,从而使我们的类依赖于Microsoft.Practices.Unity.DependencyAttribute类.那么现在,即使我们的类可能不知道它使用的IMyInterface的实现,它还必须知道Dependency类的具体实现?我在这里失踪的是什么?如果我们要更改DI框架,我们需要删除整个项目中的所有注释.

  2. 有没有办法避免容器外的这种声明(配置文件或什么不是)?

编辑 - >代码在这里

/*This is the abstract base class where I want the dependency injection to occur*/
public abstract class BasePage : System.Web.UI.Page 
{
    private IMyService _dtService;   
    public IMyService DtService
    {
        get { return _dtService; }
        set { _dtService = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

后面的Default.aspx代码

public partial class _Default : BasePage
{

    public _Default( )
    {

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            DataClass dt = DtService.GetDataById(2);
            lblWhatever.Text = dt.Description;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

全球代码背后

public class Global : System.Web.HttpApplication
{
   void Application_Start(object sender, EventArgs e)
    {
        IUnityContainer myContainer = HttpContext.Current.Application.GetDIContainer();
        myContainer.RegisterType<IMyService,MyServiceClient>(new 
                     InjectionConstructor("MyServiceWsEndpoint"));
        // I have tried this with BasePage too
        myContainer.RegisterType<_Default>(new InjectionProperty("DtService"));

    }
}
Run Code Online (Sandbox Code Playgroud)

和模块

public class UnityHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
    }

    public void Dispose() { }

    private void OnPreRequestHandlerExecute(object sender, EventArgs e)
    {
        IHttpHandler currentHandler = HttpContext.Current.Handler;
        /*This does not work*/
HttpApplicationStateExtensions.GetDIContainer(HttpContext.Current.Application).BuildUp(
                          currentHandler.GetType(), currentHandler);
        /* While this works*/
HttpApplicationStateExtensions.GetDIContainer(HttpContext.Current.Application).BuildUp<_Default>((_Default)currentHandler);

        var currentPage = HttpContext.Current.Handler as Page;
        if (currentPage != null)
        {
            currentPage.InitComplete += OnPageInitComplete;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

每次都会到达模块内部的代码.使用[Dependency]Attribute 时,INDEED行是否有效.

Ran*_*ica 5

是的,使用Dependency属性的代码将实现紧密地耦合到容器.

有几种方法可以确定要注入的属性:

  • 属性
  • XML配置
  • 程序化配置
  • 自定义容器扩展

绕过属性使用的一种方法是注册所有页面并以编程方式指定要注入的属性以及根据需要提供覆盖BuildUp:

IUnityContainer container = new UnityContainer();

// Assume we have a logger we are injecting
container.RegisterType<ILogger, Logger>(new ContainerControlledLifetimeManager());

// Register the Dependency Properties for the Page 'MyPage'.
// Property names on MyPage are "Logger"
container.RegisterType<MyPage>(new InjectionProperty("Logger"));

// ...later

IHttpHandler currentHandler = HttpContext.Current.Handler;

// Perform Property Injection.  
// Logger will be injected from the existing container registration.  
container.BuildUp(currentHandler.GetType(), currentHandler);
Run Code Online (Sandbox Code Playgroud)