Castle Windsor - multiple implementation of an interface

前端 未结 4 1555
小鲜肉
小鲜肉 2020-12-09 16:02

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 adva

4条回答
  •  孤街浪徒
    2020-12-09 16:30

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

    Here is the registration code:

    container.Register(Component.For().ImplementedBy().Named("nortonService"));
    
    container.Register(Component.For().ImplementedBy());
    container.Register(Component.For().ImplementedBy());
    
    container.Register(
        Component.For().ImplementedBy().Named("mcafeeService")
            .DependsOn(Dependency.OnComponent())
    );
    
    IReport mcafeescan = container.Resolve("mcafeeService");
    mcafeescan.LogReport();
    
    IReport nortonscan = container.Resolve("nortonService");
    nortonscan.LogReport();
    

    Output:

    McAfee Scan has Logged data to a database
    Norton Scan has Logged data to a file
    

提交回复
热议问题