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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -