I'm struggling to find a graceful way to keep my DAL layer separate to my MVC/UI layer in ASP.NET 5 thanks to the new built in Dependency Injection which I want to use.
For example I have an ASP.NET 5 project, a Business Layer project and a Data Access Project where I have various Entity Framework code such as entities and contexts. in ASP.NET 5 to get the context set up and targeting a database the main documentation is suggesting I do something like this in my StartUp.cs class
services.AddEntityFramework() .AddSqlServer() .AddDbContext<BookContext>(options => { options.UseSqlServer(Configuration.Get("Data:ConnectionString")); });
This means that I now have to reference my DAL in what is basically my UI layer, something which for years has always been bad practice according to the various experts and blog posts around.
One way I have worked around this is to create two new projects, one is a CompositeRoot project which contains factory classes to generate my business classes which then access the DAL and also a Utilities project with a Configuration class in which has a ConnectionString
property which I can pass into my Context, I then use the built in DI to wire everything up and avoid referencing my DAL in my UI layer. But I've come across issues with the latest version of Entity Framework (beta 7) as it now doesn't seem possible to specify a connection string either in the constructor of the context or in the overrideable OnConfiguration
method. Plus, all the documentation so far doesn't seem to care about this mixing of concerns at all. Is this just the way we do things now? Trust that developers won't do 'bad' things like reference DAL classes in the UI directly? Or is there a pattern people are employing to keep things SOLID with this new built in DI/configuration for ASP.NET 5?
ASP.NET Core is a revised version of ASP.NET that includes architectural improvements to create a modular framework. With ASP.NET Core, you can do the following: Build web apps and services, IoT Apps, and mobile backends. Use development tools on Windows, macOS, and Linux. Deploy to the cloud or on-premises.
ASP.NET Core's built-in use of and support for dependency injection makes this architecture the most appropriate way to structure non-trivial monolithic applications. For monolithic applications, the Application Core, Infrastructure, and UI projects are all run as a single application.
The Onion architecture is a form of layered architecture and we can visualize these layers as concentric circles. Hence the name Onion architecture. The Onion architecture was first introduced by Jeffrey Palermo, to overcome the issues of the traditional N-layered architecture approach.
Clean architecture has a domain layer, Application Layer, Infrastructure Layer, and Framework Layer. The domain and application layer are always the center of the design and are known as the core of the system. The core will be independent of the data access and infrastructure concerns.
If you ask 10 civil architects to build you a bridge, you’ll end up having 10 different architectures. Not one will be the same and not one will be better than the other.
Regardless of how many best practices and design patterns they apply, each architect will justify their idea. Some will be overzealous while others will keep it simple and get the job done. Amongst other things, budget, delivery dates and craftsmanship will have a direct impact on type of architecture you decide.
The same rule applies with software architects.
I’ve seen my fair share of architects having the best intentions in the world only to realize that the UI layer has a dependency on the DAL. Perhaps the reasoning behind this is that:
Back in MVC 5, I had the following layers:
-Contoso.Core (Class Library) -Contoso.Data (Class Library) -Contoso.Service (Class Library) -Contoso.Web (asp.net MVC 5) -Contoso.Web.Configuration (Class Library)
The Web.Configuration
layer had a dependency on Core
, Data
and Service
. This layer is where I would configure my DI stuff.
The Web
layer did not have a dependency on the Data
layer and to get stuff started, I was using the Bootstrapper Nuget Package.
Perhaps you could achieve something similar with ASP.NET 5
I’d love for Microsoft (or anyone) to create a working project template that is more geared towards enterprise level samples using a decoupled approach or even something like an Onion Architecture sample.
In the end, whatever I consider like a reasonable decoupled architecture might be too much for some while not enough for others...
Feel free to share your findings as I’m curious to know how you’ve managed to make it work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With