question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dependency AspNetCore.Http in Infra or other project?

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
trevor-hackettcommented, Mar 3, 2018

Place the CurrentUser in the Web project since that is the only project that should know and care about Microsoft.AspNetCore.Http. Keep ICurrentUser in the Core project. Use dependency injection to inject the ICurrentUser into the other projects that need it.

2reactions
CESARDELATORREcommented, Mar 4, 2018

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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found