c# - registering 2 components which inherits same interface -


i new in autofac , trying understand concept. going through sample on website. playing around code , didn't understand following.

if register 2 similar components todaywriter , yesterdaywriter below , resolve container, writes out last registered one, in case, write out today's date , ignore yesterday's date. happening? can't register 2 components inherits same interface? if can how display both of them.

class program {     private static icontainer container { get; set; }      static void main(string[] args)     {         var builder = new containerbuilder();         builder.registertype<consoleoutput>().as<ioutput>();          builder.registertype<yesterdaywriter>().as<idatewriter>();         builder.registertype<todaywriter>().as<idatewriter>();         container = builder.build();          writedate();     }      public static void writedate()     {         // create scope, resolve idatewriter,         // use it, dispose of scope.         using (var scope = container.beginlifetimescope())         {             var writer = scope.resolve<idatewriter>();             writer.writedate();             console.readline();         }     } }    // implementation of ioutput interface // how write console. technically // implement ioutput write debug // or trace... or anywhere else. public class consoleoutput : ioutput {     public void write(string content)     {         console.writeline(content);     } }  // interface helps decouple concept of // "writing output" console class. // don't "care" how write operation // happens, can write. public interface ioutput {     void write(string content); }  // interface decouples notion of writing // date actual mechanism performs // writing. ioutput, process // abstracted behind interface. public interface idatewriter {     void writedate(); }  // todaywriter comes together. // notice takes constructor parameter of type // ioutput - lets writer write anywhere // based on implementation. further, implements // writedate such today's date written out; // have 1 writes in different format // or different date. public class todaywriter : idatewriter {     private ioutput _output;     public todaywriter(ioutput output)     {         this._output = output;     }      public void writedate()     {         this._output.write(datetime.today.toshortdatestring());     } }  // todaywriter comes together. // notice takes constructor parameter of type // ioutput - lets writer write anywhere // based on implementation. further, implements // writedate such today's date written out; // have 1 writes in different format // or different date. public class yesterdaywriter : idatewriter {     private ioutput _output;     public yesterdaywriter(ioutput output)     {         this._output = output;     }      public void writedate()     {         this._output.write(datetime.today.adddays(-1).toshortdatestring());     } } 

in code sample, container contains 2 registrations match idatewriter service. when resolve idatewriter service, autofac give latest registration, in case todaywriter.

if want resolve idatewriter can resolve ienumerable<idatewriter>.

foreach (var writer in scope.resolve<ienumerable<idatewriter>>()) {     writer.writedate(); } 

if want go further, may want aggregated idatewriter.

for example :

public class aggregateddatewriter : idatewriter {     public aggregateddatewriter(ienumerable<idatewriter> writers)     {         this._writers = writers;      }       private readonly ienumerable<idatewriter> _writers;       public void writedate()     {         foreach (idatewriter writer in this._writers)         {             writer.writedate();         }     } } 

if try register type have following error message

autofac.core.dependencyresolutionexception: circular component dependency detected: consoleapplication75.aggregateddatewriter

which normal because try resolve idatewriter when aggregateddatewriter activated.

to avoid can change way components registered.

builder.registertype<yesterdaywriter>()         .named<idatewriter>("concrete"); builder.registertype<todaywriter>()         .named<idatewriter>("concrete"); builder.registertype<aggregateddatewriter>()         .as<idatewriter>()         .withparameter((pi, c) => pi.name == "writers",                          (pi, c) => c.resolvenamed<ienumerable<idatewriter>>("concrete")); 

the withparameter method tell autofac how should handle arguments of component. if aggregateddatewriter constructor has value parameter of type string. can use .withparameter("value", "anystring") method let autofac use value in constructor. in case, first parameter parameter name writers , second parameter tell autofac use result of c.resolvenamed<ienumerable<idatewriter>>("concrete") value.

if still want go further, can generate proxy using castle.core automatically generated aggregateddatewriter , register proxy using custom iregistrationsource.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -