Should the dependence on many "levels" depend on what you need?

I am writing a C # ASP.NET MVC web application using SOLID principles.

I wrote a ViewModelService, which depends on AccountServiceand RepositoryService, so I introduced these two services in ViewModelServer.

PermissionServicedepends on HttpContextBaseto use GetOwinContext()to get the instance UserManager. The controller has an instance HttpContextBaseto use, so it seems to me that I should insert the instance HttpContextBaseinto ViewModelService, which then introduces it into PermissionService.

So, in terms of code, I have:

public ViewModelService

public CategoryRepository(ApplicationDbContext context, IPermissionService permissionservice)

public AccountService(HttpContextBase httpcontext, IPrincipal securityprincipal)

to create an instance ViewModelService, I will then do the following:

new ViewModelService(
    new CategoryRepository(
            new ApplicationDbContext(), 
            new PermissionService(
                new AccountService(HttpContext, Thread.CurrentPrincipal),
                new UserPasswordRepository(new ApplicationDbContext()),
                new ApplicationSettingsService())),
    new PasswordRepository(
            new ApplicationDbContext(), 
            new PermissionService(
                new AccountService(HttpContext, Thread.CurrentPrincipal), 
                new UserPasswordRepository(new ApplicationDbContext()),
                new ApplicationSettingsService())),
    new ModelValidatorService());

Should a dependency from this many "levels" be introduced, or is there a better way?

+4
3

.

, , , , "" . ( , - Service Locator -.) , , . , , , , .

, . , DI , .

, , Locator, . , , , , - . ( , , , -, .)

( , DI , , ) .

. , . , , , . , , , DI , .

+4

, Injection Dependency , . Root of Composition, , Single Responsibility . - , , .

, , ( , , , ).

+3

, Service Locators , Injection Dependency. - , - , .

, Service Locators , . .

, :

public ViewModelService(ICategoryRepository categoryRepository, IPasswordRepository passwordRepository, IModelValidatorService modelValidator) { ... }

, . , , , , . , , , .

, , :

public ViewModelService() 
{ 
    var categoryRepository = CategoryRepositoryServiceLocator.Instance;
    var passwordRepository = PasswordRepositoryServiceLocator.Instance;   
    var modelValidator  FModelValidatorServiceLocator.Instance;

    ...
}

, , , ( ). , .

, ViewModelService . (ICategoryRepository ..) , . , ViewModelService, , Inversion of Control (, Castle Windsor, StructureMap ..), .

Castle Windsor - :

container.Register(Classes.FromAssemblyNamed("Repositories").Pick().WithServiceAllInterfaces());
container.Register(Component.For<IAccountService>().ImplementedBy<AccountService>()); 
container.Register(Component.For<IApplicationDBContext>().ImplementedBy<IApplicationDBContext>()); 
container.Register(Component.For<IApplicationSettingsService>().ImplementedBy<IApplicationSettingsService>()); 
var viewModelService = _container.Resolve<ViewModelService>();

, ", , " " ".

!

0

All Articles