14

I have a design idea for a large project at work and I think I have it figured out but would really love to get some feedback on a) the idea in general, and b) my proposed implementation.

The basic idea is simple: I want to create an ASP MVC application that can be extended in the future with additional controllers and views without having to recompile the code. The idea is to have one MVC application with a very basic set of features and then extend the functionality by adding another 'Application.dll" that contains controllers, data, and business logic that are specific to that application. The views will simply be copied into the same directory as the main MVC application during install.

The problem is that MVC does its routing on types within the same assembly so even if I move the routing definitions to the database, the MvcHttpHandler would not be able to route anything to the new Dll since it doesn't "know" the controller types in it. Looking at the MVC code, I found that to load the controllers they are simply calling Activator.CreateInstance which looks only in the current assembly.

My solution is simple but maybe I'm missing something: I will override the MvcHttpHandler by either replacing the ControllerFactory directly (not sure how to do that) or by duplicating that functionality in a derived class. The new code will read the request and try to load the controller first from the current assembly and then from the extended ones. Once the proper assembly is found, I will use CreateInstance and pass that assembly to it to get the controller I want.

4 Answers 4

7

The end of this article shows how to implement your own ControllerFactory. Basically, you derive from DefaultControllerFactory and then wire it up in Application_Start() in your global.asax.

Sign up to request clarification or add additional context in comments.

Comments

1

There is nothing wrong with the idea of a controller factory, as long as it is simply for flexibility later. the problem si when you load up the controllers with code that belongs in other layers, which is one reason you might "need" the flexibility of other controllers. As long as you are not mashing layers togehter, I see no real issue with the factory idea.

Comments

0
public IActionResult Register()
{
    return View();
}
[HttpPost]
public async Task<IActionResult> Register(RegisterVm vm)
{
    if (!ModelState.IsValid)
    {
        return View();
    }
    AppUser user = new AppUser
    {
        FullName = vm.Fullname,
        Email = vm.Email,
        UserName = vm.Username,
    };

    var result = await _userManager.CreateAsync(user);
    if (!result.Succeeded)
    {
        foreach (var err in result.Errors)
        {
            ModelState.AddModelError("", err.Description);
        }
    }
    var roleResult = await _userManager.AddToRoleAsync(user, Roles.User.GetRole());
    if (!roleResult.Succeeded)
    {
        foreach (var err in roleResult.Errors)
        {
            ModelState.AddModelError("", err.Description);
        }
        return View();
    }
    return View();
}
public async Task<IActionResult> MyRolesCreateMethod()
{
    foreach (Roles item in Enum.GetValues(typeof(Roles)))
    {
        await _roleManager.CreateAsync(new IdentityRole(item.GetRole()));
    }
    return Ok();
}

Comments

0
public async Task<IActionResult> Login(LoginVm vm, string? returnUrl = null)
{
    if (IsAuthenticated) RedirectToAction("Index", "Home");
    if (!ModelState.IsValid) return View();
    AppUser? user = null;
    if (vm.UsernameOrEmail.Contains("@"))
    {
        user = await _userManager.FindByEmailAsync(vm.UsernameOrEmail);
    }
    else
    {
        user = await _userManager.FindByEmailAsync(vm.UsernameOrEmail);
    }

    if (user is null)
    {
        ModelState.AddModelError("", "Username Or Password is wrong!!!");
        return View();
    }
    var password = await _signInManager.PasswordSignInAsync(user, vm.Password, vm.RememberMe, true);
    if (!password.Succeeded)
    {
        if (password.IsLockedOut)
        {
            ModelState.AddModelError("", "Wait untill" + user.LockoutEnd!.Value.ToString("yyyy-MM-dd HH:mm:ss"));
        }
        if (password.IsNotAllowed)
        {
            ModelState.AddModelError("", "Username or password is wrong");
        }
    }
    if (string.IsNullOrWhiteSpace(returnUrl))
    {
        if (await _userManager.IsInRoleAsync(user, "Admin"))
        {
            return RedirectToAction("Index", new { Controller = "Dashbard", Area = "Admin" });
        }
    }
    return RedirectToAction("Index", "Home");
}

3 Comments

public static class RoleExtension public static string GetRole(this Roles role) return role switct Roles.User => nameof(Roles.User), Roles.Admin => nameof(Roles.Admin), };
asp-items="@(new SelectList(Model.Departments, "Id", "Name")
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.