简体   繁体   中英

How to update already registered service Castle Windsor

I'm a beginner to the DI containers.

I'm using third party framework and it has a class like below

public class PurchaseOrderAppService : IPurchaseOrderAppService, IAutoRegister
    {
        private readonly IEmail _email;

        public PurchaseOrderAppService(IEmail email)
        {
            _email = email;
        }
    }

All classes that inherits IAutoRegister interface are automatically registered by the third party framework. Email class is like below

public class Email : IEmail, IAutoRegister
{
    public Send(string message)
    {
        //Send a message
    }
}

What I want is that I want PurchaseOrderAppService class to use another class called MyEmail instead of Email.

public class MyEmail : IEmail
{
    public Send(string message)
    {
        //Send a message another way
    }
}

How can I update already registered service by framework?

Thanks.

Thanks @Thuan,

In the second link, there is method named ServiceOverrides which is obsoleted, and I could use DependsOn instead,

IocContainer.Register(
    Component.For<IEmail>()
    .ImplementedBy<MyEmail>()
    .Named("Email.MyEmail"),
    Component.For<IPurchaseOrderAppService>()
    .ImplementedBy<PurchaseOrderAppService>()
    .Named("PurchaseOrderAppServiceWithMyEmail").IsDefault()
    .DependsOn(Dependency.OnComponent(typeof(IEmail), "Email.MyEmail")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM