Accessing HttpContext and User Identity from data layer

前端 未结 6 1954
Happy的楠姐
Happy的楠姐 2021-02-15 11:08

I need to implement AddedBy/ChangedBy type fields on my Base Entity that all other entities inherit from ( Fluent Nhibernate ).

Accessing HttpContext.User.Identity

6条回答
  •  难免孤独
    2021-02-15 11:44

    You're correct. Referencing your HttpContext.User.Identity class from within your repository is not a good idea. The HttpContext is a UI concern and as such, should go no further than the UI layer.

    What you should be doing is harnessing an IoC container (such as StructureMap) to inject your dependency (HttpContext.User.Identity) details into your repository, or any other layer such as the service layer via dependency injection.

    For an example of how this can be setup (in this instance it's the session object) see the latter part of this answer.

提交回复
热议问题