I am working on a web application that uses Angular 12 for the frontend and ASP.NET Core 6 for the backend. In our team we usually write just about anything as a 3-layer application (if possible) consisting of a presentation layer, a business logic layer and a data layer. In the case of this project, this would mean that when for example all entities of a certain type, let's call it Car
, are requested from the server, the following would happen (simplified):
The request arrives in the
CarController.cs
controller. ThecarManager
instance here belongs to the business layer and provides functions for interacting with car entities (CRUD for the most part). Dependency Injection is used to inject thecarManager
instance into the controller:public async Task<ActionResult<List<Car>>> GetCars() { List<Car> cars = await this.carManager.GetCarsAsync(); return this.Ok(cars); }
If there is anything business logic related, it will be done in the
GetCarsAsync()
function of thecarManager
. However, in many cases this is not necessary. Therefore, I often end up with "dumb" functions that just call the corresponding function of the data layer like this. ThecarAccessor
in this example belongs to the data layer. Dependency Injection is also used here to inject its instance into the manager:public async Task<ActionResult<List<Car>>> GetCarsAsync() { return this.carAccessor.GetCarsAsync(); }
The actual querying of the data is then done in the corresponding accessor for the entity. Example:
public async Task<List<Car>> GetCarsAsync() { List<Car> cars = new(); List<TblCar> dbCars = await this.Context.TblCars.ToListAsync(); foreach (TblCar dbCar in dbCars) { Car car = <AutoMapper instance>.Map<Car>(dbCar); cars.Add(car); } return cars; }
While this makes it easy to not mix up business logic and data access in the application, it really creates a whole lot of code that does nothing than just call other code. Therefore, in some cases the business layer is omitted simply because of laziness and business logic ends up just being implemented somewhere else, for example directly in the controller. Is there a simple solution for this?