0

I have already seen a lot of different MVC architectures. All of them are different. I would like to know a standard way to send a model data to a view using a controller. Is it suitable to send the model object to the view and set view fields using one setter? or set the view fields one by one using several setters with one parameter or other way?

public class View{
    public void SetField1(int value){
      //...
    }
    public void SetField2(string text){
      //...
    }
    public void SetField3(Time time){
      //...
    }
    //or
    public void SetContent(Model model){
       //...
    }
}

public class Model{
   public int _value;
   public string _text;
   public Time _time;

   public Model(int value,string text,Time time){
     //...
   }
}

public class Controller{
   Model _model;
   View _view;

   public Controller(){
      _model=new Model();
      _view=new View();
   }
   public void SetModel(Model model){
      _model=model;
      SetContentView();
   }
   public void SetView(View view){
      _view=view;
   }
   public void SetContentView(){
      _view.SetField1(_model._value);
      _view.SetField2(_model._text);
      _view.SetField3(_model._time);
      //or
      _view.SetContent(_model); // the view can not be decoupled with model!
   }
} 
Mahdi GB
  • 65
  • 2
  • 9
  • A fail to see any link between your above posted code and the [tag:asp.net-mvc] architecture. It seems your question is not related at all to [tag:asp.net-mvc], was that the point? If so maybe remove those tags? If that was not the point then you should first read up on how [tag:asp.net-mvc] actually works. – Igor Mar 27 '18 at 21:28
  • Your code samples have very little to do with how ASP.NET MVC actually works. May I suggest you search YouTube / MS Virtual Academy / etc for `ASP.NET MVC tutorial`. This site is not the place for extensive tutorials. – Peter B Mar 27 '18 at 21:31

1 Answers1

1

You could create a ModelView object that explicitly defines the properties needed by your view. Then you could define a Mapper class responsible for mapping the server side model into the client side model, thus decoupling the view from the server's model.

From your example:

public class Controller{
   Model _model;
   View _view;
   Mapper _mapper;

   public Controller(){
      _model=new Model();
      _view=new View();
      _mapper = new ViewModelMapper();

   }

   public void SetContentView(){
      _view.SetContent(_mapper.map(_model));
   }
} 

I don't have much experience with them, but there do exist frameworks intended to make this less tedious: http://automapper.org/

zaq
  • 2,571
  • 3
  • 31
  • 39
  • I want to understand the design and architecture and clean code with decoupling, not framework – Mahdi GB Mar 27 '18 at 21:36
  • _mapper.map(_model), it returns a client model? so the view depends on the client model. it is not better to set fields individually without passing the model based on maintainability? – Mahdi GB Mar 27 '18 at 21:39
  • The architecture would be the same with or without a mapping framework. The difference is that without the framework, you will need to define a class to handle the mapping step.You might have one mapping class per Model->ViewModel transformation that you need to perform. – zaq Mar 27 '18 at 21:40
  • Yes, the _mapper.map(_model) would return a view model. The View will always be bound to something, be it a single object or a bunch of individual fields, the key is that you want to be able to decouple the domain model from the view. This is what the mapping does for you. For example, if you add a field to the model that the view doesn't need, there is no need to change anything in the view. – zaq Mar 27 '18 at 21:44
  • I have updated the example so as to not use generics. The ViewModelMapper is an explicit implementation intended to map objects of type Model to objects of type ViewModel. If you have other models and views, they will need similar mapping objects to handle them. – zaq Mar 27 '18 at 21:48