Decorators with different constructor arguments

前端 未结 2 602
野的像风
野的像风 2021-01-16 04:05

Using Castle Windsor, I\'d like to create a class that records an integer. But I\'d like to decorate it several times with other classes. I can see how this works if all c

2条回答
  •  猫巷女王i
    2021-01-16 05:05

    You could wrap the seed in an interface (ISeedHolder ?) and register it with a singleton lifestyle. Then use the interface in your ModelUpdatingRecorder instead of the raw int. Unless your seeds may need to be parallelized it should allow you to set the seed and resolve it when constructing the ModelUpdatingRecorder

    public interface ISeedHolder
    {
        int Seed {get;set;}
    }
    
    public class ModelUpdatingRecorder : IRecorder
    {
        int seed;
    
        public ModelUpdatingRecorder(ISeedHolder seedHolder)
        {
            this.seed = seedHolder.Seed;
        }
    

    Would this solution achieve what you need?

提交回复
热议问题