Are simple controllers without models a valid architecture?

I am trying to convert parts of an application to ASP.NET MVC from WebForms. The idea is that, whenever possible, I use MVC for new development. My problem is this:

Doing it this way means my models are not fully implemented in MVC. Let's say my WebForms application has a robust widget management page. I am adding new features to add color to the Widget. Click on the widget and it will take you to a new page to choose a color and that's it. One field. It seems silly to create a WidgetColorPreference model with two properties, WidgetID and Color, for the sole purpose of validation and persistence.

So, I went down the path of implementing the Service namespace with methods like "SetWidgetColor", accepting the WidgetID and Color parameter. My controller action is just calling this service on my model build.

My question is: Is this a valid stopgap architecture?

0


a source to share


2 answers


Sure. MVC exists to put a view on top of a model. If your application is everything and there is no model, you don't need to force the model there. As your application becomes more complex and the pattern becomes obvious, you can regroup.



In other words, you won't be using M, V and C for Hello World. This will be strictly "V". "C" adds more mental skill, and "M" adds business logic, persistence, and search.

+4


a source


I think you would use the Widget model and then include the Include / Exclude functions of the binder to easily update those properties in the Widget model in your action.

public ActionResult UpdateWidgetColor( [Bind(Include="Color,ID")]Widget widget )
{

}

      



or something similar with TryUpdateModel

public ActionResult UpdateWidgetColor( int id, string color )
{

   var whitelist = new string[] { "Color" };

   Widget widget = db.Widgets.SingleOrDefault( w => w.ID == id );

   if (widget == null) ...handle missing widget error

   if (!TryUpdateModel( widget, whitelist ))
   {
      ...
   }
   else
   {

   }
}

      

0


a source







All Articles