Dependency AspNetCore.Http in Infra or other project?
See original GitHub issueI have a situation where I need to have ICurrentUser on both projects
1 - ApplicationCore for any specific rule with user or tenant 2 - Infra - For global filter in dbContext or repository
The implementation of ICurrentUser have a dependency the Microsoft.AspNetCore.Http
For example:
public class CurrentUser: ICurrentUser
{
private readonly IHttpContextAccessor _accessor; //HERE
public UserSession (IHttpContextAccessor accessor)
{
_accessor = accessor;
}
public string Name => _accessor.HttpContext.User.Identity.Name;
public int? TenantId => int.Parse (_accessor.HttpContext.User.Claims.FirstOrDefault (x => x.Type == "TenantId"). ToString ());
public bool IsAuthenticated => _accessor.HttpContext.User.Identity.IsAuthenticated;
}
I have as dependency Microsoft.AspNetCore.Http, and need to be used in ApplicationCore and Infra (Data / DbContext)
Would it be correct to leave this implementation and dependency on Infra? Or maybe in another layer (eShopOnWeb.Shared for example)? Where would be the best approach?
Suggestions?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top Results From Across the Web
DotNet Core Dependency Injection Multiple Projects
The problem I am having is that the App.Data project has a dependency on the App.Server project to implement the interfaces it requires,...
Read more >Understanding Dependency Injection in .NET Core
As a best practice, you should share one single instance of HttpClient as much as possible. You can achieve this goal by leveraging...
Read more >Clean Architecture with .NET and .NET Core — Overview
It is dependent on the domain layer, but has no dependencies on any other layer or project. This layer defines interfaces that are...
Read more >Clean Architecture - Should I Move the Startup Class to ...
In this post, I look at whether it is worth moving the dependency registrations in the Startup.cs class into its own assembly to...
Read more >Implementing the infrastructure persistence layer with ...
The EF DbContext comes through the constructor through Dependency Injection. It is shared between multiple repositories within the same HTTP ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Place the
CurrentUser
in the Web project since that is the only project that should know and care aboutMicrosoft.AspNetCore.Http
. KeepICurrentUser
in the Core project. Use dependency injection to inject theICurrentUser
into the other projects that need it.It is ok to have multiple infrastructure projects as long as only the Interfaces/Contracts are the ones defined in the Core project. Then use dependency injection from the application layers (Web project or any other application layer)