c# - Autofac: how to inject properties with dynamic values? -
by autofac, it's easy inject static value currentdate
property instances of classes in given assembly:
builder.registerapicontrollers(asm).withproperty("currentdate", new datetime(2012, 1, 13));
however, how inject dynamic values e.g. values returned lamda () => { return datetime.now; }
currentdate
property?
sounds use pretty standard property injection, this:
builder.registerapicontrollers(asm) .onactivating(e => { e.instance.currentdate = datetime.now; });
note may need cast e.instance
of type object.
see lifetime events in documentation more info.
on second thought, why not put initialization in base class constructor?
public datetime currentdate { get; private set; } protected apicontroller() { currentdate = datetime.now; }
the current date isn't dependency need di container provide.
Comments
Post a Comment