Implementing OnePerSessionBehavior in NInject

前端 未结 2 656
执笔经年
执笔经年 2021-01-21 14:54

I\'d like to create a OnePerSessionBehavior for NInject (v1.0) and I\'ve mostly got it working. The only issue that remains is how to pass in fresh arguments using .WithArgumen

2条回答
  •  囚心锁ツ
    2021-01-21 15:33

    Can you pass a lamda as your argument? For instance, if you have a class like this:

    public class Something : ISomething
    {
        public Something(Action initializer)
        {
            var now = initializer();
        }
    }
    

    You can bind it as follows:

    Bind()
        .To()
        .Using()
        .WithArgument("initializer", () => { return DateTime.Now; });
    

    Though I don't know you exact situation, another idea would be to create your object without worrying about argument injection, and then set your properties:

    kernel.Bind().To().Using();    
    var mySomething = kernel.Get();
    mySomething.DateCreated = DateTime.Now;
    

    or:

    mySomething.Initialize(DateTime.Now);
    

    Would either of those ideas work?

提交回复
热议问题