ApplicationDbContext - where it belongs to the project

I want to use a single (EF) context file in my mvc 5 application, and I want to use the asp identifier.

I have several projects in DAL, GUI and WebAPI solution.

I want to just move ApplicationDbContext to the DAL assembly and completely remove EF from my user interface project.

What do you do with ApplicationDbContext and asp-identifier when starting new projects?

Do you leave it in the user interface layer or move it to the data layer?

I really do not have experienced developers to ask, I hope this will not be canceled.

+6
source share
3 answers

Separation Identification:

Separating an identifier from a user interface is much more difficult. Here Microsoft has confused them so much that I would recommend leaving it until you understand Identity, EF, and the expert-level repository template.

But if you are interested in separating Identity from the user interface level, here is a great resource from Dino Esposito


  • Pay attention to the link to ApplicationDbContext in your account controller constructor:

     public AccountController() : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser> (new ApplicationDbContext()))) 

This could easily be a context reference in your DAL.

  • You will have difficulty using Repo in context. For this you will need to rewrite UserManager , which I doubt very much what you want to do.

  • You can create a child element that inherits from ApplicationDbContext and abstracts through the interface

:

 public class UserIdentityContext : ApplicationDbContext, IUserIdentityContext 

Now you can use Ninject to bind your account controller to an abstract template

 public AccountController() : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser> (IUserIdentityContext identityContext))) 

But none of these steps allow you to remove EF assemblies from your user interface project. Here, Microsoft tied data elements and user interfaces too tightly. You will have to overwrite the account controller.

This is the problem they are dealing with, and is likely to improve significantly in MVC 6 / Identity 3.

+6
source

I assume you are talking about Entity Framework DbContext? If so, you are right to save this in your DAL assembly.

Moving and changing the namespace is all you need to do.

+7
source

It may not be that difficult to remove ApplicationDbContext from your ui project. The most complete way is to create a webapi project that will expose the IUserStore functions, or at least only the functions you need, and you won’t need all of them. The web api methods simply delegate the underlying entity used in the DAL or not. How (with repository)

  [HttpGet] [Route("finduserbyid/{userId}")] public async Task<IHttpActionResult> FindByIdAsync(string userId) { var verifiedUser = await this.UnitOfWork.ApplicationUserRepository.FindByIdAsync(userId); if (verifiedUser == null) { return NotFound(); } return Ok(verifiedUser); } 

Then create a new class in your ui project or utility project that implements IUserStore and calls webapi for the data. I was able to do this in a day or so, not very difficult. Like this:

 public class RemoteUserStore<T> : IUserStore<ApplicationUser> , IUserPasswordStore<ApplicationUser> , IUserLoginStore<ApplicationUser> , IUserRoleStore<ApplicationUser> , IUserEmailStore<ApplicationUser> { public RemoteUserStore(string identityServerUrl) { _appServerUrl = identityServerUrl; } private readonly string _appServerUrl; private string IdentityServerUrl { get { return _appServerUrl; } } public async Task CreateAsync(ApplicationUser user) { var client = new HttpClient(); string url = string.Format("{0}/api/identity/createuser/{1}", IdentityServerUrl, user.Id); var userContent = MapApplicationUserToFormContent(user); var result = await client.PostAsync(url, userContent); } //snip } 

However, you cannot remove the reference to the entity infrastructure, since the user interface actually uses ApplicationUser with IdentityUser as the base class and is defined in microsoft.aspnet.identity.entityframework

+1
source

Source: https://habr.com/ru/post/975242/


All Articles