Creating interfaces for data models in MVC

14,603

Solution 1

Inversion of Control (IoC) and Dependency Injection (DI) doesn't mean that you need to use interfaces for all models and classes.

For "view" models, concrete classes are fine, because

  1. They are inputs into the controller (and possibly outputs to the view)
  2. They are effectively DTOs
  3. They should be simple and have discrete purposes
  4. Model validation with data annotation attributes work on the type on which they are defined, so they should go on the concrete type used in model binding.

So, I would continue as you have been by using simple classes for "view" models. Your Domain / Core / Business Layer model (the M in MVC) on the other hand, will be as simple or complex as the domain that you are modelling.

Solution 2

Take into consideration that IoC works(when developing a MVC application) as a way to ease:

  • dependency management
  • unit testing

That's why it is good idea to inject all dependencies in some way(usually by injecting into constructor). When you work with controller's actions, you want to pass your model as a parameter cause it doesn't affect internal state of whole controller.

What is more - it is not controller's responsibility to initialize and manage some action specific dependencies.

What it more models(or view models as called by some in that particular case) are more like DTOs which are used only as form of proxy which facilitates mapping data from one class to another.

According to controllers and models testing - usually you want test either controller action behavior(in that case passing some new instance of model is not a problem - you can initialize it with any possible data combination) or a model itself(but to be honest - when it contains almost no logic, it is hardly a test case)

Share:
14,603
Sulphy
Author by

Sulphy

Updated on June 04, 2022

Comments

  • Sulphy
    Sulphy almost 2 years

    I have an MVC project that will accept data via the HTTP 'Post' verb. I'm keen to follow best practice and wanted to ask a quick question around how best to set up my models.

    Generally when working with posted data I would design my controller to use the concrete model. However as I'm looking to use IoC I wanted to ascertain if I should carry on as I have been OR if I should be creating interfaces.

    My gut feel is that I should be using interfaces for all of my models and classes throughout my web app to effectively implement IoC. And I just wanted to get validation that I'm on the right path! :o)

    example:

    My concrete model
    
    public class PhoneBook : IPhoneBook
    {
       public string Firstname {get; set;}
       public string LastName {get; set;}
       public string PhoneNumber {get; set;}
    }
    
    
    My interface
    
    public interface IPhoneBook 
    {
       string Firstname {get;set;}
       string Lastname {get;set;}
       string Phonenumber {get;set;}
    }
    
    My controller
    
    //Accept posted data from web form
    public void Post(IPhoneBook passInDetails)
    
    {
       ... 
    }
    

    Thanks in advance.

  • Mardoxx
    Mardoxx almost 8 years
    They are effectively DTOs I still am not convinced anyone is certain what the VM is in MVVMCP..