Castle Windsor: How to specify a constructor parameter from code?

前端 未结 6 483
轻奢々
轻奢々 2020-12-30 08:34

Say I have the following class

MyComponent : IMyComponent {
  public MyComponent(int start_at) {...}
}

I can register an instance of it wit

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 09:19

    You could use a configuration class to read the app.config. Then register that and get windsor to use it for its dependency. Ideally my MyConfiguration would use an interface.

    public class MyConfiguration
    {
        public long CacheSize { get; }
    
        public MyConfiguration()
        {
            CacheSize = ConfigurationManager.AppSettings["cachesize"].ToLong();
        }
    }
    
    
    
    container.Register(Component.For().ImplementedBy());
    
    container.Register(Component.For>()
    .ImplementedBy>().
    DependsOn(Dependency.OnValue("size", container.Resolve().CacheSize))
    .LifestyleSingleton());
    

提交回复
热议问题