2

I am starting with MVC 3 and am planning in separating the model and the controllers into their own separate projects. I'll follow the suggestions made from this post for this:

asp.net mvc put controllers into a separate project

The purpose of separating them into separate projects is that there are chances that I may have to add a web service project to the solution and I’d like it to reuse the same functionality exposed by the controller project. So the solution will be formed of two view projects, WebServices and WebSite, the controller project and the model project.

I’d like to know if this is possible and if it’s a common scenario with MVC.

Update 1:

With your suggestions I agree and think it’s best to keep the view and the controllers together.

Would it be possible to have a hybrid of MVC and MVP? I have a feeling I am really overdoing things here so please let me know what you think.

So I would have:
1 – Web project with controllers.
2 – WebServices project
3 – Presenters/Interfaces.
4 – Model.

The controllers would then become the views in an MVP model. Also each web service would become a view in an MVP model.

For instance we could have the following, interface, presenter , controller.

public interface ICustomers {
    string[] Customers{set;}
}

public class CustomerPresenter {
    ICustomers view = null;
    public CustomerPresenter(ICustomers view) {
        this.view = view;
    }

    public void GetCustomers() {
        view.Customers = new string[]{"Customer1","Customer2"};
    }
}

public class CustomerController:ICustomers {

    CustomerPresenter presenter = null;
    public CustomerController() {
        presenter = new CustomerPresenter(this);
    }

    private string[] customers = null;
    public string[] Customers {
        set { throw new NotImplementedException(); }
    }

    public void GetCustomers() {
        presenter.GetCustomers();
        //Return view.
    }
}

The WebService would be a view in an MVP model.

    public class CustomerWebService:ICustomers {

    CustomerPresenter presenter = null;
    public CustomerController() {
        presenter = new CustomerPresenter(this);
    }

   [WebMethod]
    public void GetCustomers() { 
     presenter.GetCustomers(); 
    //Return response. 
} 
Community
  • 1
  • 1
Rauland
  • 2,944
  • 5
  • 34
  • 44

2 Answers2

2

My projects are built specifically for the reason you stated, you want to implement a web service. I would not recommend separating the controllers because this is an actual part of the web project. What you actually want is around 3-4 different projects.

  1. Repository/data layer (may contain your domain level models)
  2. Domain Layer (optional)
  3. Service layer. (Now this is where you can point your web service to very easily, all your re-usable logic should be here, not in the controller)
  4. Web Layer. (Contains View Models, Views and Controllers)

I placed them in levels. Basically, the repository, domain and service layer are completely de-coupled, meaning you can use these libraries without a server or asp.net. A wpf application can just call to the service layer because the web layer is just for presentation purposes.

Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
1

I am not sure it's common to separate the views and the controllers on their own projects. It might be but I haven't seen it personally.

The way I would split it initially is:

  • One project for views and controllers
  • One project for models

If and when you have the need to support different views you could update your controller to return something different depending on the type of request. It's common to have controllers that return different things for different clients (for example HTML vs JSON.)

Hector Correa
  • 26,290
  • 8
  • 57
  • 73