4

Refactored my MVC application using entity framework as ORM and Autofac as DiContainter.

I run into the next "problem. I have a controller where i do crud operations for a object that has lots of complex properties.

here I show a simplified example that has nothing to do with project.

public class Car { public int Id {get;set;} public int Doors { get; set; } public double Miles { get; set; } ..... public Brand Brand { get; set; } public Engine Engine { get; set; } public FuelType FuelType {get;set;} } public class Brand { public int Id { get; set; } public string Name { get; set; } ..... }// Same for Engine, FuelType,.... 

for all these Objects I have services for CRUD actions

for objects with no complex properties i inject 1 service into my controller like

public class BrandController : Controller { readonly IBrandService brandService; public KwaliteitController(IBrandService _BrandService) { brandService = _BrandService; } //here comes actionresults for CRUD operations and etcetera... } 

This works fine and looks clean for my. Now the problem is for my CarController I also need all the names "or other properties" for Fueltype, Brand, Engine,....

I can retrieve these values and Id's from there service but than i have to inject them all into my controller like

 public class CarController : Controller { readonly ICarService carService; readonly IBrandService brandService; readonly IFuelTypeService fuelTypeService; readonly IEngineService engineService; public KwaliteitController(ICarService _CarService IBrandService _BrandService,.....) { carService = _CarService; brandService = _BrandService; .... } } 

In reality I have objects with more than 10 property's like this so i wouldt have to implement more than X services into my controller. That looks to me like a serious overkill. is there a way I can accomplish this without having to use all my services for 1 controller.

4
  • to Create a new Car Commented Mar 30, 2017 at 13:13
  • Classic sign of too many responsibilities. (SRP violation) review your design. Commented Mar 30, 2017 at 13:14
  • You should make use of generics in your service class(es), which will allow you to have to inject far fewer services, perhaps only just one. I have an example of something like this on my blog if you're interested: cpratt.co/truly-generic-repository Commented Mar 30, 2017 at 13:14
  • Thej are Directly acting against a datasource. "Database" & csv files Commented Mar 30, 2017 at 13:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.