5

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.

David Klempfner
  • 8,700
  • 20
  • 73
  • 153
Sulphy
  • 766
  • 2
  • 9
  • 29
  • 1
    There are times when "best practices" are a bit overkill. In this particular case for your `PhoneBook`model I'd say you don't need an interface. – Padraic May 26 '15 at 12:17

3 Answers3

6

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.

Community
  • 1
  • 1
Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • `They are effectively DTOs` I still am not convinced anyone is certain what the VM is in MVVMCP.. – Mardoxx Jun 28 '16 at 08:28
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)

kamil-mrzyglod
  • 4,948
  • 1
  • 20
  • 29
0
  • You cannot use Interface as a model in an API controller. Dot net framework cannot deserialize an Interface.
  • A method with an Interface type parameter could use an object created using a class that inherits the same interface.

Correct me if I am wrong.

Sunil Ram
  • 21
  • 2