Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Castle Windsor - multiple implementation of an interface

While registering components in Castle Windsor, how do we bind specific implementation of an interface to a component that has a dependency on that interface. I know in advance which implementation needs to be used by the component.

For example i created a sample console application based on code from several blogs and tutorials.

Following is the code.

public interface IReport {     void LogReport(); }  public interface ILogger {     string Log(); }  public class FileLogger : ILogger {     public string Log()     {         return "Logged data to a file";     } }  public class DatabaseLogger : ILogger {     public string Log()     {         return "Logged data to a database";     } }  public class McAfeeService : IReport {     private readonly ILogger _logger;      public McAfeeService(ILogger logger)     {         this._logger = logger;     }      public void LogReport()     {         string getLogResult = this._logger.Log();          Console.WriteLine("McAfee Scan has " + getLogResult);     }         }  public class NortonService : IReport {     private readonly ILogger _logger;      public NortonService(ILogger logger)     {         this._logger = logger;     }      public void LogReport()     {         string getLogResult = this._logger.Log();          Console.WriteLine("Norton Scan has " + getLogResult);     } }  class Program {     private static IWindsorContainer container;      static void Main(string[] args)     {         // Register components         container = new WindsorContainer();          container.Register(Component.For<IReport>().ImplementedBy<NortonService>());         container.Register(Component.For<ILogger>().ImplementedBy<FileLogger>());          IReport service = container.Resolve<IReport>();         service.LogReport();          Console.ReadLine();     } } 

I would like NortonService to always use a Filelogger and McAfeeService to use a Database Logger.

In the above program i am unable to bind NortonService to FileLogger.

How to do it?

like image 232
Chinmay Lokesh Avatar asked Aug 02 '13 03:08

Chinmay Lokesh


1 Answers

The above answers lead me to inline dependencies and the feature service override

Here is the registration code:

container.Register(Component.For<IReport>().ImplementedBy<NortonService>().Named("nortonService"));  container.Register(Component.For<ILogger>().ImplementedBy<FileLogger>()); container.Register(Component.For<ILogger>().ImplementedBy<DatabaseLogger>());  container.Register(     Component.For<IReport>().ImplementedBy<McAfeeService>().Named("mcafeeService")         .DependsOn(Dependency.OnComponent<ILogger, DatabaseLogger>()) );  IReport mcafeescan = container.Resolve<IReport>("mcafeeService"); mcafeescan.LogReport();  IReport nortonscan = container.Resolve<IReport>("nortonService"); nortonscan.LogReport(); 

Output:

McAfee Scan has Logged data to a database Norton Scan has Logged data to a file 
like image 52
Chinmay Lokesh Avatar answered Sep 21 '22 15:09

Chinmay Lokesh