c# - WebAPI DI Framework that understands HttpContext.User -
i looking di framework can satisfy scenario:
every controller has constructor this
public thiscontroller( thatrepository repo)
every repository has controller this:
public thatrepository (datasource ds)
there 1 master datasource, never passed repository. instead needs:
masterdatasource.withuser ( httpcontext?.user?.identity?.name )
are there di frameworks webapi support out of box?
i believe use every di container such purpose, registering current httpcontext
(or current user identity
) inside container , injecting constructing datasource
instance.
here simple solution using httpcontext
in simpleinjector (you may port code di framework like):
container.register<httpcontextbase>(() => new httpcontextwrapper(httpcontext.current), lifestyle.scoped); container.register<datasource>(() => { var httpcontext = container.getinstance<httpcontextbase>(); return masterdatasource.withuser(httpcontext?.user?.identity?.name); }, lifestyle.scoped);
Comments
Post a Comment