vendredi 16 avril 2021

Configuring Services in a Layered ASP.NET MVC Application

I'm following this course on asp.net core mvc on Pluralsight, and building the application in parallell. However, I have changed the project structure a bit:

solution
-- Common
-- Data
-- Model
-- View

Here Common contains all interfaces, Data contains EF Core stuff, Model contains some repositories and view models, and View is the mvc project.

The reason I did it this way was that I felt that the author was doing some strange things, like putting his database code in the UI (the mvc project, or the View). I have managed to get an almost "clean" dependecy graph where View depends on Model, Model depends on Data and all depend on Common, but there are no dependencies in the other direction.

The Problem

The problem I'm facing is the configuration of the application. The tutorial wants me to add a call:

services.AddDbContext<XXX>(YYY);

into the Startup.ConfigureServices() method. The problem here is that in order to do this, I would have to import EF Core stuff into the UI, which is the very thing I was trying to avoid.

Do you have any ideas or suggestions for how to avoid this, if it is at all possible?

I have looked at other tutorials, but most either seem to avoid layering and put everything in the UI (also my own teacher does that), whereas others such as this one layer the application, but don't seem to care about the direction of the dependencies (in that case I don't see the point of bothering with layering).

I have also scanned the forum and found some questions about layering mvc projects (e.g., this question) but not really anything that answered my question. Apologies in advance if the answer was there and I missed it!

I'm not looking for ready-made code to plug in, but would appreciate any ideas that might achieve what I want. Would the asp.net conventions allow me to move the Startup class, for instance? But that would pollute the Model project with some UI dependencies. Or perhaps it is okay to have a dependency like this if it is only in one place?

Aucun commentaire:

Enregistrer un commentaire