c# - Passing dependency into Factory -


we using factory create instance of subscribers. each subscriber can have own dependency.

each subscriber use constructor injection.

should pass dependency subscribers through subscriber factory? every time adding new dependency in subscriber change subscriber factory?

public interface imessagesubscriber {     bool process(string message) }  public class messagesubscriber1 : imessagesubscriber {     public bool process(string message)     {         //some custom logic      } }  public class messagesubscriber2 : imessagesubscriber {     public bool process(string message)     {         //some custom logic      } }  public class messagesubscriberfactory {      //subscribertype enum     public imessagesubscriber get(subscribertype type)     {             if(type == 1)             {                 return new messagesubscriber1();             }             else if(type == 2)             {                 return new messagesubscriber2();                         }     } }  //main class public class process {     public static void main(string[] args)     {         messagesubscriberfactory fac = new messagesubscriberfactory();         foreach subscribertype         {             string  = "message";             imessagesubscriber subscriber = fac.get(type);             subscriber.process(message)         }     } } 

one approach use named registrations di/ioc container. involve using container in service locator fashion (which people oppose), think make sense in case. example below pretty crude, give approach handle subscribers different dependencies without passing them factory. used unity here , you'd want wrap container reference rather referencing directly, gets point across.

public interface ilowercasewriter {     void write(string message); } public class lowercasewriter : ilowercasewriter {     public void write(string message)     {         console.writeline(message.tolower());     } }  public interface iuppercasewriter {     void write(string message, int number); } public class uppercasewriter : iuppercasewriter {     public void write(string message, int number)     {         console.writeline("{0}:{1}", number, message.toupper());     } }  public interface isubscriber {     void write(); } public class subscriber1 : isubscriber {     private ilowercasewriter _writer;     public subscriber1(ilowercasewriter writer)     {         _writer = writer;     }     public void write()     {         _writer.write("using subscriber 1");     } } public class subscriber2 : isubscriber {     private iuppercasewriter _writer;     public subscriber2(iuppercasewriter writer)     {         _writer = writer;     }     public void write()     {         _writer.write("using subscriber 2", 2);     } }  public class subscriberfactory {     private unitycontainer _container;     public subscriberfactory()     {         _container = new unitycontainer();         _container.registertype<ilowercasewriter, lowercasewriter>();         _container.registertype<iuppercasewriter, uppercasewriter>();         _container.registertype<isubscriber, subscriber1>("subscriber1");         _container.registertype<isubscriber, subscriber2>("subscriber2");     }      public isubscriber getsubscriber(int type)     {         switch (type)         {             case 1:                 return _container.resolve<isubscriber>("subscriber1");             case 2:                 return _container.resolve<isubscriber>("subscriber2");             default:                 throw new exception();         }     } }  class program {     private static void main(string[] args)     {         var factory = new subscriberfactory();         var subscriber = factory.getsubscriber(1);         subscriber.write();         console.readline();     } } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -