Search code examples
c#asp.netdata-access-layerasp.net-mvc-5separation-of-concerns

Separate projects for MVC5 and Web API 2


I am new to MVC and Web API. I created two separate projects. One ASP.NET MVC 5 (MyUI) and other ASP.NET Web API 2 (MyApi). I would like to keep my API project separate from my UI layer.

The AccountController class in MVC project (MyUI) is essentially doing the same that the AccountController in the API project does (MyApi). I first thought about making the MyUI.AccountController a sub class of MyApi.AccountController but then I quickly realize that first inherits from Controller and second from ApiController type.

So my questions are:

  1. In order to remove data access logic from MVC 5 project, should I just convert the AccountController to a wrapper class which will essentially call the corresponding methods from the MyApi.AccountController?
  2. Is there a better approach?

Edit:

My solution structure

Edit 2: While trying to articulate the problem I realized that I was going about it incorrectly. My confusion came from the ASP.NET Identity implementations which were embedded within the API project. That needs to be moved to the Data Access layer and both controllers need to access them the same way which is a whole different can of worms :)

Thanks!!


Solution

  • Method 1 seems a plausible solution but what I would suggest is to create a new class library and there put your data logic. In that way, the MVC project and the Web Api project could connect to that class library. The reason is that you never know if you write another UI layer, Service layer or other connectivity layer. All those layers could then connect to the same data logic layer.