简体   繁体   中英

Simple Injector Parameter appended to Factory

For brevity, I'll use some short and generalized naming conventions. If I have:

public class Factory : IFactory
{
     private readonly string dbConnection;
     public Factory(string dbConnection)
     {
          this.dbConnection= dbConnection;
     }

     public IDataContext Create() => new DataContext(dbConnection);
}

The Factory class has a field, which is being set by the Constructor. How does Simple Injector support the passing of that parameter? If you implement:

public class Service
{
     private IFactory Factory { get; }
     public Service(IFactory factory)
     {
         Factory = factory;
     }

     public void Save()
     {
          using(var context = Factory.Create())
              context.Insert(...);
     }
}

The parameter is never actually passed to or through the Factory when it is initialized or instantiated. When you register the dependencies, it doesn't look it is possible:

ContainerFactory.Register<IFactory, Factory>(Lifestyle.Transient);

I was looking into the IEventHandler interface, that is apart of Simple Injector which looks like it would address this issue, but is that how you're supposed to pass a parameter in this instance?

container.RegisterSingleton<IFactory>(new Factory("constr"));

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