Unity Container: Overrides
Back to: Software Development Principles
Unity Container allows you to provide overrides when resolving dependencies. Overrides provide a way to modify or replace the default behavior of the container for a specific resolution.
There are several types of overrides supported by Unity Container:
- Parameter overrides: Allows you to specify specific parameter values for a method or constructor during the resolution process.
var customer = new Customer { Id = 1 }; var order = new Order { Customer = customer }; var overrides = new ParameterOverride("order", order); var orderService = container.Resolve<IOrderService>(overrides); orderService.ProcessOrder(customer);
In this example, we are creating an instance of the Customer
and Order
classes and specifying the Order
instance as a parameter override. When we resolve the IOrderService
dependency using the container, Unity will inject the Order
instance into the ProcessOrder
method.
- Property overrides: Allows you to specify specific property values for a type during the resolution process.
var customer = new Customer { Id = 1 }; var overrides = new PropertyOverride("Customer", customer); var orderService = container.Resolve<IOrderService>(overrides); orderService.ProcessOrder();
In this example, we are creating an instance of the Customer
class and specifying it as a property override. When we resolve the IOrderService
dependency using the container, Unity will inject the Customer
instance into the Customer
property.
- Dependency overrides: Allows you to specify specific dependencies for a type during the resolution process.
var customerRepository = new CustomerRepository(); var overrides = new DependencyOverride<ICustomerRepository>(customerRepository); var orderService = container.Resolve<IOrderService>(overrides); orderService.ProcessOrder();
In this example, we are creating an instance of the CustomerRepository
class and specifying it as a dependency override. When we resolve the IOrderService
dependency using the container, Unity will inject the CustomerRepository
instance into the OrderService
constructor.
- Injection factory overrides: Allows you to specify an injection factory that will be used to create a new instance of a type during the resolution process.
var overrides = new InjectionFactoryOverride<IOrderService>(c => new SpecialOrderService()); var orderService = container.Resolve<IOrderService>(overrides); orderService.ProcessOrder();
In this example, we are creating an injection factory override that will create a new instance of the SpecialOrderService
class during the resolution process. When we resolve the IOrderService
dependency using the container, Unity will use the injection factory to create the SpecialOrderService
instance.
In summary, Unity Container provides several types of overrides that allow you to modify or replace the default behavior of the container during the resolution process. Overrides can be useful in cases where you need to provide specific values or behavior for a particular resolution.