jeudi 29 novembre 2018

Injecting a dependency that is a tree of dependencies

Is it a pattern or antipattern to inject a dependency that is a tree of dependencies?

For example:

public class ClassExample
{
    private IContainer _container;

    public ClassExample(IContainer container)
    {
        _container = container;
    }
}

public interface IContainer
{
    IDependencyA DependencyA { get; set; }
    IDependencyB DependencyB { get; set; }
    IDependencyC DependencyC { get; set; }
}

public interface IDependencyA
{
}

public interface IDependencyB
{
}

public interface IDependencyC
{
}

The above example could be made even deeper (a tree) - for example IDependencyA could contain even more dependencies IDependencyAA, IDependencyAB, etc.

Someone might use above method to minimize code duplication when having multiple places where a same set of services need to be injected. Code will end up looking like: container.DependencyA.DependencyAA.Method(...) in many cases which makes it more verbose and less DRY.

Is using above method a good or bad idea (pattern/antipattern)? Or when is it a good idea, and when is it a bad one?

Aucun commentaire:

Enregistrer un commentaire