Aspnetcore: Razor Components project - Controllers in same project or separate project?

Created on 20 Mar 2019  路  5Comments  路  Source: dotnet/aspnetcore

With a Razor Components project that will need to make use of a SQL Server database, and perform typical CRUD, is it best to:

Option A:
Create Web API controllers inside of the existing Razor Components project, and set it up to serve Web API requests too? So this way you would have your Razor Components project, and your Web API all as one project.

Option B:
Leave the Razor Components project to behave like a client only, and create a separate Web API project to house any Web API endpoints?

Or is there another option other than the above which I haven't considered?

question

All 5 comments

Option C:

If you're writing a server-side components application you don't need to communicate with your own code through an HTTP API - you're already running on the server.

Option C:

If you're writing a server-side components application you don't need to communicate with your own code through an HTTP API - you're already running on the server.

If I go this route, would this then make life difficult to transition the project over to client-size Blazor if that becomes stable?

I think that's up to you to judge. Personally, I think the work to make an app run on the client is a super-set (adding HTTP apis).

One option would be to build repository abstractions for all of your data access from components - then implement them in a really simple way for now (just a wrapper around your DB). Then when it's time to move to the client, change the implementation to use HTTP.

If your goal is to eventually run on the client with Blazor, then I'd suggest using two projects for now. This will enforce a separation between the code that's going to stay on the server, and the code that's going to move to the client eventually.

Thanks very much - that's exactly what I needed to know. Thank you for taking the time to look at it.

Was this page helpful?
0 / 5 - 0 ratings