本文介绍了城堡温莎 - 多个实现的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Castle Windsor中注册组件时,如何将接口的特定实现绑定到与该接口有依赖关系的组件上。我提前知道组件需要使用哪个实现。



例如,我创建了一个基于几个博客和教程中的代码的示例控制台应用程序。

以下是代码。

  public interface IReport 
{
void LogReport();
}

public interface ILogger
{
string Log();


public class FileLogger:ILogger
{
public string Log()
{
return将数据记录到文件;
}
}

public class DatabaseLogger:ILogger
{
public string Log()
{
return记录的数据到数据库
}
}

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程序
{
private static IWindsorContainer container;

static void Main(string [] args)
{
//注册组件
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();
}
}

我希望NortonService始终使用Filelogger和McAfeeService使用数据库记录器。



在上述程序中,我无法将NortonService绑定到FileLogger。



如何要做到这一点?

解决方案

上面的答案引导我进行内联依赖和功能



以下是注册码:

  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();

输出:

  McAfee Scan已将记录的数据存储到数据库
Norton Scan将已记录数据记录到文件


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?

解决方案

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

这篇关于城堡温莎 - 多个实现的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:45