Partial Components
Partial components is a flexible way of changing components in the IOC container without having to re-register them all from scratch. This technique was mainly introduced to be able to patch pipelines by either adding or removing tasks in a flexible way. But partial components can be used to patch any component in the platform. Mainly you want to patch the component to change what is injected into the component (like a list of tasks for a pipeline for example). Let's take a closer look.
Partial Components basics
A partial component
is registered under the components
element in your configuration files just like regular components are.
They look like this
<partial-component id="id of the component you want to patch"></partial-component>'
For a partial component the prerequisite is that the ID must match an existing component in the configuration files and that gives you the ability to then patch it.
Simple property patching
Let's take a look at how to patch a property injected into a component. In this example we'll want to change a component that determine how many decimals we want in our price calculation.
Out of the box the configuration for this is set as configuration object you can constructor inject into your components, and Ucommerce use this out of the box to figure that out when rounding prices and numbers.
The component for that looks like this:
<component id="PriceCalculationSettings" service="Ucommerce.Settings.PriceCalculationSettings, Ucommerce" type="Ucommerce.Settings.PriceCalculationSettings, Ucommerce"> <parameters> <NumberOfDigitsPrecision>2</NumberOfDigitsPrecision> </parameters> </component>
Instead of re-registering the entire component and change the value we can use a partial component to change the NumberOfDigitsPrecision from 2 to 4.
With a partial component it would look like this:
<partial-component id="PriceCalculationSettings"> <parameters> <NumberOfDigitsPrecision>4</NumberOfDigitsPrecision> </parameters> </partial-component>'
The IOC container will automatically change this value and this everywhere you constructor inject the type Ucommerce.Settings.PriceCalculationSettings, Ucommerce
will now yield 4 instead of 2 on the property called NumberOfDigitsPrecision
This does not only work for simple types like ints. You can also change which services are injected into your components by explicitely specifying the id of the component. Like the example below.
Imagine our ProductPriceCalculationService
which are calculating prices for products. This component makes use of another injected component TaxService
. We can make sure that it uses a special version of that service instead of just the first component the container finds. Like so:
<partial-component id="ProductPriceCalculationService"> <parameters> <TaxService>${SpecificTaxService}</TaxService> </parameters> </partial-component>'
In the example above we patch the property TaxService that are injected into the service by ensuring it will always use the component with the id SpecificTaxService
. This is particular useful if you have multiple implementations of the same service.
Patch Pipelines
If you are interested in how to patch pipelines we encourage you to read how to create pipeline tasks