Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC processing database actions model vs controller class

I am creating my first MVC project, although not new to coding. According to Microsoft:

An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. For example, if you are using the Microsoft Entity Framework to access your database, then you would create your Entity Framework classes (your .edmx file) in the Models folder. A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained in the model. In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.

At first I wrote all database logic in the controller class. I have successfully re-written a majority of it in the model class but there are major problems with this approach that I would like to get cleared up.

First of all is all the UrlHelper / HttpRequestBase / HttpContext / ModelStateDictionary, all of these functions are part of you controller class. It was written as though you are supposed to do most of this processing in the controller class.

For example my controller class once you register a new account:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        if (accountModel.register(model, Url, Request, ModelState) == true)
            return RedirectToAction("Login", "Account");
    }
     return View(model);
}

And my model class:

public bool register(RegisterViewModel model, UrlHelper url, HttpRequestBase request, ModelStateDictionary modelState)
    {
        if (userManager.FindByEmail(model.Email) != null)
        {
            modelState.AddModelError("Email", "Error, already have this email registered!");
            return false;
        }
        else
        {
            MyIdentityUser user = new MyIdentityUser();

            user.Email = model.Email;
            user.FullName = model.FullName;
            user.UserName = model.Email;

            IdentityResult result = userManager.Create(user, model.Password);

            if (result.Succeeded)
            {
                userManager.AddToRole(user.Id, "ReadOnly");

                string code = userManager.GenerateEmailConfirmationToken(user.Id);
                var callbackUrl = url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: request.Url.Scheme);
                userManager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking" + System.Environment.NewLine + "<a style=\"border:15px solid #00FF00;background-color:#00FF00;color:#000;font-size:14px; font-family:Arial;text-decoration:none\" href =\"" + callbackUrl + "\">here</a>");

                return true;
            }
            else
            {
                modelState.AddModelError("Email", "Error while creating the user!");
                return false;
            }
        }
    }

In this model class, I have no access to the ModelState, UrlHelper, HttpRequestBase since they are all part of the controller class.

Am I supposed to be passing all these variables into my model class to have access to them? Seems like Microsoft wrote this to do all the logic in the controller even though they state not to. Just dont really see why not to do it in the controller to not have to pass all these variables to the model class.

like image 201
user1779362 Avatar asked Feb 14 '26 02:02

user1779362


1 Answers

MVC has nothing to do with Microsoft - it is a concept that was around 20 years ago. Seriously.

Yes, Models are that - Models. VERY little logic happens in them (MOSTLY: NONE) and when I see your view, I see way too much code. It is not a model.

The reason you have no access to all those variables in the model is that you do not need them. You should not do any processing that is not about presenting data in the Model. Which you do.

like image 199
TomTom Avatar answered Feb 15 '26 15:02

TomTom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!