1

I am creating an administration facility for an asp.net MVC application. I am currently trying to create a form that will allow admin users to create roles. I see plenty of code, which looks like this online:

public class RoleController {

 ApplicationDbContext context;

        public RoleController()
        {
            context = new ApplicationDbContext();
        }

        public ActionResult Index()
        {
            var Roles = context.Roles.ToList();
            return View(Roles);
        }

        public ActionResult Create()
        {
            var Role = new IdentityRole();
            return View(Role);
        }

        [HttpPost]
        public ActionResult Create(IdentityRole Role)
        {
            context.Roles.Add(Role);
            context.SaveChanges();
            return RedirectToAction("Index");
   }
 }

should I be putting code that connects to the database in the application service layer to be consistent or is there a reason not to do this?

6
  • Is this a c# specific question? Sep 4, 2017 at 14:16
  • @Constantin Galbenu, yes the code is written in C#. However, I guess it applies to any language, which uses ASP.NET Identity i.e. VB.NET as well.
    – w0051977
    Sep 4, 2017 at 14:21
  • In my php web applications I often use an application service in my presentation layer. I do like this, as opposed to injecting a database connection into http controllers, to call authorisation services and to have a well defined api. Sep 4, 2017 at 19:19
  • So, all that my apllication can do is in the api namespace and is protected by an authorisation layer and/or logging and/or cache and so on. Sep 4, 2017 at 19:22
  • @Constantin Galbenu, do you also have a service layer in a separate project?
    – w0051977
    Sep 13, 2017 at 13:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.