Implementing OnePerSessionBehavior in NInject

爱⌒轻易说出口 提交于 2019-12-01 23:23:38

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

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

You can bind it as follows:

Bind<ISomething>()
    .To<Something>()
    .Using<OnePerSessionBehavior>()
    .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<ISomething>().To<Something>().Using<OnePerSessionBehavior>();    
var mySomething = kernel.Get<Something>();
mySomething.DateCreated = DateTime.Now;

or:

mySomething.Initialize(DateTime.Now);

Would either of those ideas work?

You're passing a value that's evaluated during the definition of the binding. That's why you get the same value over and over again. Actually from the top of my head I don't have an easy answer to that but I'll definitely think about it as it might be useful for testing purposes for me to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!