c# - Register a default instance in StructureMap -
i have class (myservice) has static property (myservice.context) represents current context (which specific logged in user, changes).
what i'm trying achieve i
objectfactory.initialize(x => { x.for<imyservice>().use<myinstance>(c => c.use(myservice.context)); }); i.e. every objectfactory.getinstance<imyservice>() reference myservice.context
is doable?
update
i can't use singleton pattern since myservice.context changes depending on user making request (via httpcontext).
in pseudo-code above lambda parameter c represents sm context, can return custom result each request. i'm aware of sm's intercept() it's fired after object constructed - not instead.
if can work property there possibility add oncreation method. action provided executed against instance after creation:
objectfactory.initialize(x => { x.for<imyservice>() .use<myinstance>() .oncreation(x => x.context = myservice.context; }); or can use lazy initialization , provide func use method executed whenever new instance needed. should execute in right context:
objectfactory.initialize(x => { x.for<imyservice>() .use<myinstance>(() => new myinstance(myservice.context); }); i hope 1 of methods works you.
Comments
Post a Comment