jeudi 8 janvier 2015

Should injected dependencies be publicly accessable or private?

Should dependencies be stored to private fields or properties with private setters and public getters? This applies to constructor DI.


To be clear, in the property example, I would not expect these to be added to an accompanying interface unless it made sense - i.e. they would only be visible in the implementation type:



interface IFoo {
void DoSomething();
}

class Foo : IFoo {
private readonly IService dependency;
public Foo(IService dependency) {
this.dependency = dependency;
}
}

class Bar : IFoo {
public Foo(IService dependency) {
this.Dependency = dependency;
}

public IService Dependency { get; private set; }
}

Aucun commentaire:

Enregistrer un commentaire