0

I'm using the Identity with UserManager Pattern. When I need to create a new user, my Register action of Account controller has this code

var user = new WebUser() { UserName = model.Email, Email = model.Email, CreatedOn = DateTime.Now, FullName = model.Email, EmailConfirmed = true };
IdentityResult result = await RegisterService.CreateAsync(user, model.Password);

if (!result.Succeeded && ((string[])result.Errors).Length > 0)
{
    foreach (var erro in result.Errors)
    {
        ModelState.AddModelError("", erro);
    }
    return BadRequest(ModelState);

}
else if (!result.Succeeded)
    return InternalServerError();

For now it works, but my rule needs something specific. Every user in my database need to have a record in other table. How can I implement it? I feel like wrong putting into the controller the rule like "if succeeded create a record into another table" because I'll need a transaction scope in the controller. It's sounds dirty to me. Can someone help me?

Dawid Rutkowski
  • 2,658
  • 1
  • 29
  • 36
  • What? This is what a controller is supposed to handle. – Steve Jan 30 '17 at 11:14
  • I don't know... All my controllers handle only the rules about the form data like if everything required came (ModelState), after that I convert the viewmodel comming into a model and send it to a Service Layer (business logic) which have to check if the data is ok (like if its unique and follow the rules). After that my service layer do the logic to keep the data right in database (chain of inserts/updates/deletes). Each service has it owns logic to keep data, not the controller. If I have an API and MVC controller, the logic will be duplicated and I dont think its correct. – Leandro De Mello Fagundes Jan 30 '17 at 11:26
  • Simply override RegisterService.CreateAsync() method and add custom logic there. – Dawid Rutkowski Jan 30 '17 at 13:32

0 Answers0