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.
Updating registered service is not a right way to do with IoC container and is not something you need. I would suggest you have a look at https://github.com/castleproject/Windsor/blob/master/docs/inline-dependencies.md and https://github.com/castleproject/Windsor/blob/master/docs/registering-components-one-by-one.md#supplying-the-component-for-a-dependency-to-use-service-override
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.