WPF MVVM Framework Stylet Development Document 14.3 StyletIoC Injection

14.3 StyletIoC Injection

This page covers constructor injection and property injection in detail.

constructor injection

Constructor injection happens automatically when StyletIoC creates a new instance of a type, provided the type is bound using a syntax Bind<...>().To<...>()other than Bind<...>().ToFactory(...).

When presented with a type to construct, StyletIoC first needs to determine which constructor to call (if there is more than one):

  1. If the constructor is [Inject]decorated with an attribute, StyletIoC will use it.
  2. Any constructors that contain parameter types that StyletIoC does not know how to parse and that do not have default values ​​will be discarded.
  3. Select the constructor with the most parameters that StyletIoC can provide, and use default values ​​for parameters that StyletIoC cannot provide values.

If the constructor type is IEnumerable<T>some T, and you don't explicitly register it IEnumerable<T>as a service with StyletIoC, but you do register one or more implementing types T, StyletIoC will construct an IEnumerable<T>instance containing those types.

attribute injection

After the type is constructed, StyletIoC injects values ​​into all [Inject]properties and fields decorated with that attribute. It will inject public, protected and private properties and fields - injecting values ​​into private members is not recommended (because setting types is impossible without reflection), but you may need it.

The main problem with property injection is that your properties should really be world-writable (so it can be set without reflection, e.g. in unit tests), even though they shouldn't be from an encapsulation perspective. This usually ends up with an attribute of { set; private get; }, which is really bad. For this reason, constructor injection is almost always preferred.

Because properties are injected after the type is constructed, nothing that happens in the constructor can rely on these properties being populated. This is another reason to prefer constructor injection. If you _really_ need to know when a property is injected, implement the interface IInjectionAware. ParametersInjectedAfter all the properties are injected, the interface methods will be called.

Because property injection requires decorating properties [Inject], your types cannot be agnostic to the IoC container as they can be injected using constructors. This will bite you if you want to change the IoC container.

IEnumerable<T>As with constructor injection, properties of certain types Twill be injected into collections of that type.

You can also perform property injection if StyletIoC has no constructor type calls IContainer.BuildUp. For example:

var car = new OldBanger():
ioc.BuildUp(car);

example:

class OldBanger
{
    
    
   // 不会被注入 - 没有 [Inject] 属性
   public IEngine Engine {
    
     get; set; }
}

class OldBanger
{
    
    
   // 将被注入,前提是为服务 IEngine 注册了一个类型
   [Inject]
   public IEngine Engine {
    
     get; set; }
}

class Garage
{
    
    
   // 将注入为服务 IVehicle 注册的所有类型
   [Inject]
   public IEnumerable<IVehicle> Vehicles {
    
     get; set; }
}

class OldBanger
{
    
    
   // 字段也被注入
   [Inject]
   public IEngine Engine;
}

class OldBanger
{
    
    
   // This works true, although you really shouldn't do it
   [Inject]
   private IEngine Engine;
}

class OldBanger : IInjectionAware
{
    
    
   [Inject]
   public IEngine Engine {
    
     get; set; }

   public PropertiesInjected()
   {
    
    
      // Do something with this.Engine
   }
}

Original project address: https://github.com/canton7/Stylet
Current document original address: https://github.com/canton7/Stylet/wiki/StyletIoC-Injection

Previous section: WPF MVVM Framework Stylet Development Document 14.2 StyletIoC Configuration
Next section: WPF MVVM Framework Stylet Development Document 14.4 StyletIoC Keys

Previous: WPF MVVM Framework Stylet Development Documentation 13. Validating Model Base Class ValidatingModelBaseNext
: WPF MVVM Framework Stylet Development Documentation 15. The ViewManager

Guess you like

Origin blog.csdn.net/qq_39427511/article/details/130362899
Recommended