How much logic is in M, in MVC?

I have a service level mapping methods for my MVC layer. Let's say I have a method that:

Public List<StateObjects> GetStates() 

Is it a good or bad design to then call this method from the constructor of my model object, in order to possibly create a SelectList of States for my view? If the model refers to my service level and ALSO allow my controllers to refer to my service level? Or should it be that the set of model variables is executed in the controllers, so that all the logic in the controller?

+4
source share
4 answers

As you pointed out, there are 2 real options:

  • Fill in the model in the controller using the data returned from your business / domain level
  • Fill in the model data inside the model using its constructor, properties, or method.

There is also a third option, which should contain the MC part of the MVC in your domain layer. This simplifies the model set, but blurs the line a bit (if you're a purist) with what the business layer does. In practice, this does not really matter until you build a giant corporate model.

My particular preference is to have a static

public static MyModel FromDomainObject(DomainObject object)

on models. Then you can use this in your controller and pass to the domain object in action to get back the model that you pass into your view.

The important part is to make sure that the properties in the model are not populated on demand, and the methods are not available for presentation. In fact, you or the team will call these methods / properties and you will know the consequences, but it helps to keep problems separate and reduce grip.

0
source

This is probably not a clear answer, depending on the size and volume of your application, but in combination with the service layer, I try to make the "M" MVC a more View Model.

Like MVVM, the model becomes a flat object that contains only the properties that will be displayed in the view. The controller accesses the service layer and then uses AutoMapper to map the properties of the domain objects to the model.

As a result, you get thin controllers and thin models, and most of the logic remains at your level of service. The only time the model really has any logic is either matching lists (like the StateObjects list), or SelectLists, or other view-related functions / calculations. It also makes it easy to map a model to a JSON object if you are doing any kind of asynchronous JavaScript development because the models are relatively flat.

If your model directly refers to your level of service, you should pass all your services to the constructor if you are trying to use some type of dependency injection, which can make the Model instance an actual pain.

I don’t know if this violates the MVC paradigm, but it worked well in projects that I have done in the past.

+3
source

Answer:

It depends on your application and coding style.

Many people practice lean models and fat adjusters, but based on a heavy object-oriented background, it is more natural to have fat models and harsh controllers. Here's how I think about it:

Models must know about themselves. This means that model-specific functions belong to models.

Models must be agnostic for other models. Controllers must handle model interactions with the model (and other interactions that are not only associated with the instance).

That way you can have elegant instance.function() and controller.staticLikeFunction() .

IMO is the most beautiful way to do this if you have a highly object-oriented application.

If your application does not require object-oriented design, then it makes no sense. In this case, drop everything into the controller and keep your models in pure data.

+2
source

It is better to use the view model as a model and save it. It must have sufficient information to communicate with the view and the controller, and nothing more.

Other logic required for the controller may be contained within classes and created by the controller. This keeps your logic loosely coupled and therefore easier to maintain. You will do a favor.

0
source

All Articles