简体   繁体   English

ASP.NET自定义服务器控件中的依赖项注入

[英]Dependency Injection in a ASP.NET Custom Server Control

I am writing some asp.net custom server controls and looking for the best practice as to how to inject a dependency that the control needs. 我正在编写一些asp.net自定义服务器控件,并正在寻找有关如何注入控件所需依赖项的最佳实践。

When deciding on how to do this, there are a couple of factors which I am taking into consideration: 在决定如何执行此操作时,我考虑了两个因素:

1) How easy would it be to inject this dependency through markup. 1)通过标记注入此依赖关系有多么容易。
2) How easy would it be to inject this dependency through code-behind. 2)通过后台代码注入此依赖关系有多么容易。
3) This injection has to be as early as possible in the control life cycle, preferably the control should have all of its dependencies available in the OnInit(). 3)这种注入必须在控件的生命周期中尽早进行,最好是控件应在OnInit()中拥有所有可用的依赖项。

Bases on these factors, the only way I can think of doing this it so have a string property on the control which will have the fully qualities type to the dependency. 基于这些因素,我可以想到的唯一方法是在控件上具有字符串属性,该属性将具有依赖项的完全品质类型。 Once the control initializes it can load that type and do what it needs to do. 控件初始化后,就可以加载该类型并执行所需的操作。

Example : 范例

public class MyControl : CompositeControl
{     

     public string RepositoryType { get; set; }     
     protected IRepository Repository { get; set; }

     protected override void OnInit()
     {
          EnsureChildControls();
     }

     protected override void CreateChildControls()
     {
          if (!ChildControlsCreated)
          {
              Repository = ComponentFactory.Instanciate(RepositoryType);
          }
     }
}

I run into this kind of situation all the time and was wondering if anyone else figured out another/better/different to inject the dependency. 我一直都遇到这种情况,想知道是否还有其他人想出另一个/更好/不同之处来注入依赖关系。
Thanks :) 谢谢 :)

What I've done is on init of the control, query the DI container and populate those references. 我所做的是在控件的初始化上,查询DI容器并填充这些引用。 You could nest this logic in a specialized base class that your custom server control can inherit from, so then it's reused. 您可以将此逻辑嵌套在您的自定义服务器控件可以继承的专用基类中,然后将其重用。 OR: There is a special AddedControl method that is called everytime a control is added to the control tree. 或:每次将控件添加到控件树时,都会调用一种特殊的AddedControl方法。 The caveat here is that it probably only calls the method on the parent control, and does not propogate up. 需要注意的是,它可能仅调用父控件上的方法,而没有提升。 It may work depending on your scenario. 根据您的情况,它可能会起作用。

There isn't much in the lifecycle that lets you know when a control is created (other than the AddedControl method or the Init method that runs at the beginning of the process) to be able to tap into, and you can't customize the constructor of the control. 在生命周期中,没有什么可以让您知道何时创建控件(除了AddControl方法或在过程开始时运行的Init方法),您无法对其进行定制。控件的构造函数。

In regards to #1, to use the markup approach would require tapping into the design-time aspect of controls, and leveraging the design time attributes to the fullest. 关于#1,要使用标记方法,需要充分利用控件的设计时方面,并充分利用设计时属性。 But #1 means defining the mappings in markup, which is not quite the point of a DI container. 但是#1意味着在标记中定义映射,这与DI容器的意义不尽相同。 The designer still needs the property or something to assign the reference to, so it would essentially be double work of defining the property, then defining something in the designer, when the DI container takes care of that for you. 设计人员仍然需要该属性或将引用分配给它的东西,因此本质上来说,定义该属性,然后在DI容器为您解决这个问题时,在设计者中定义一些东西是一件双重的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM