c# - Dependency Injection and other constructor parameters - bad practice? -


at moment experimenting little bit dependency injection containers, time unity.

given following interface:

public interface ipodcastcommservice {     void download();      void upload(); } 

and following implementation:

public class podcastservice {      private ipodcastcommservice commservice;      private string url;       public podcastservice(string url, ipodcastcommservice commservice)      {          this.commservice = commservice;          this.url = url;      } } 

because of constructor, looking solution pass parameter , found it:

var p = container.resolve<ipodcastcommservice>(new parameteroverride("url", myurl)); 

so far good, @ same time read how bad , how bad design of class , yes, looks little bit ugly. how can pass parameter class in elegant way?

my first thought property, have check every time need url given.

update: 1 example, read bad design, this:

but there may cases have pass in custom constructor parameters resolve operation. may argue screams of bad architecture there’s situations bringing di-container legacy system may require these kind of actions.

source: http://mikaelkoskinen.net/unity-passing-constructor-parameters-to-resolve/

i don't why need podcastservice composition of ipodcastcommservice, instead of implemented ipodcastcommservice , has url injected string. don't understand why design bad. injecting url imho.

if think of better way, think can replaced injecting context / configuration instead of native data type.

public class podcastservice {      private ipodcastcommservice commservice;      private iconnectioncontext connection;       public podcastservice(iconnectioncontext connection, ipodcastcommservice commservice)      {          this.commservice = commservice;          this.connection= connection;      } }  public interface iconnectioncontext{     string podcastserviceurl{get;} } 

but again, don't find benefit (except can handle session / constants / static fields) regular approach.

update:

i have found similiar question bad design here. in summary, not native type parameter (string, etc) or custom constructor parameter bad. need put parameter class responsible parameter. , custom constructor parameter needed if handle if-else condition inside abstract factory pattern.


Comments