Simple Injectors function to InjectProperties is marked deprecated and will be removed in a future release. How will I be able to use it in Caliburn.Micro's BuildUp override? The framework uses this internally via IoC.BuildUp many times!
The InjectProperties is marked with the [Obsolete] attribute and the compiler message points to the URL https://simpleinjector.org/depr1 which gives more detailed information. The referenced page explains why this method is deprecated and how to change your code or configuration for this.
That page also links to the Property Injection paragraph of the Extendibility Points page. This page describes how to configure the container to enable property injection. For instance:
class PropertySelectionBehavior<TAttribute> : IPropertySelectionBehavior
where TAttribute : Attribute
{
public bool SelectProperty(Type type, PropertyInfo prop) {
return prop.GetCustomAttributes(typeof(TAttribute)).Any();
}
}
// Usage:
var container = new Container();
container.Options.PropertySelectionBehavior =
new PropertySelectionBehavior<MyInjectAttribute>();
This allows the container to inject properties that are marked explicitly with the [MyInject] attribute.
When possible, always try to let the container create the type for you instead of having types with a default constructor and injecting properties after that. I'm however unfamiliar with Caliburn Micro and I'm not sure if that's possible.
In case you need BuildUp like behavior, you can retrieve a registration from the container and request it to initialize an existing instance for you. That would look as follows:
public void BuildUp(object instance)
{
var registration = this.container.GetRegistration(instance.GetType(), true);
registration.Registration.InitializeInstance(instance);
}
This allows the container to initialize instances and send it through the Simple Injector pipeline which enables property injection and other initialization according to the given registration and configuration rules (such as the explicit property injection example given above).
This works great if the types that need to be built up have a default constructor. If this is not the case, you will have to create (and cache) a new Registration instance by calling Lifestyle.Transient.CreateRegistration and call the InitializeInstance on that instance, just as the SimpleInjectorFilterAttributeFilterProvider of the MVC integration package does (you can take a look at its source code here).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With