简体   繁体   中英

Simple Injector auto-wiring with an Initialize()-Method

I'm using Simple injector successfully with constructor injection and I love the auto-wiring feature for the ctr-Arguments. Is there a similar auto-wire way for a initialize method?

So like instead of providing arguments to the constructor where doing async stuff is often impossible, things are provided to an Initialize() method with auto-wiring

public class A
{
   // only std-ctor ()
   [Init] // maybe mark needed?
   void /*async*/ Initialize(IC c, IB b)
   {
      // get auto called by container and params auto-wired
      // maybe do async stuff with b
   }

}

// in Composition root (for simplicity):
container.RegisterSingleton<A>();

So the async possibility would be a plus feature.

[Update]

So async is reasonably not the feature you want in object graph creation. So let's not put the focus on that. But sometimes it may still be desireable to rather use init (ie if a ()-constructor is required). I realize that auto calling Initialize() by simple injector leads to nonsense. I just see the need sometimes to split object creation and initialization and auto-wiring is just way too cool. How would one then activate or implement that? So that simple Injector does auto-wire it for you? I hope I do not have to write an "initializer-class" where I have to 'pipe through' all dependencies. So that is the actual direction of the question (get ahold of Magic.AutoWire() ).

   public class SomeClassWithA
   {
      A _a;
      public SomeClassWithA(A a)
      {
        _a=a;
      }

      void CalledLater()
      {
         Magic.AutoWire(_a);
         // Initialize with auto-wire parameters was called
      }
    }

Although it is possible to extend Simple Injector to call an initialize method during object graph initialization, it is impossible to make this truly asynchronous, simply because Simple Injector lacks an asynchronous API. Without a " Task<T> GetInstanceAsync<T>() " method, the application would still have wait for I/O by blocking a thread, instead of using an I/O completion port, which would completely waste the benefits of using async.

But Simple Injector lacks such async feature and this is deliberate (and AFAIK most DI libraries lack such feature for the exact same reason). The construction of object graphs should be fast and should not consist of any I/O. Constructors (or initializers with dependencies) should do nothing more than store incoming dependencies, as Mark Seemann clearly stated here .

This basically means that you should delay any I/O till after object graph construction until the moment that an object is used for the first time. This not only keeps object construction simple, fast and reliable, it also makes it possible to verify the construction of your onject graph in isolation (without the existence of any I/O components such as a database or the file system).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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