質問

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!!

役に立ちましたか?

解決

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.

他のヒント

Extract the common implementation into a separate project (a class library for instance). Your business logic must be the same no matter how you access it. After all, the web service and the site are only a view of the same information and the same control logic. In the future you might be required to write a fat client in WPF or a service in WCF and you do not want to rewrite everything, do you?

I think you are asking about layering application. basically the choice depends on requirements.If you are following data centric design check this layering

enter image description here

Research about DI,ORM,Repository Pattern, SOLID Principlese

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top