Caliburn.Micro and simple injector - InjectProperties are deprecated - how to BuildUp ()?

The function of simple injectors has been InjectProperties

deprecated before and will be removed in a future version. How can I use it in Caliburn.Micro BuildUp

override? The framework uses this internally over and IoC.BuildUp

over again!

+3


source to share


1 answer


InjectProperties

is marked with the attribute [Obsolete]

, and the compiler message points to the URL https://simpleinjector.org/depr1 which gives more details, The reference page explains why this method is deprecated and how to change your code or configuration to do so.

This page also refers to the insert paragraph of the extensibility points page properties . This page describes how to set up a container to enable property insertion. For example:

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 explicitly marked with the attribute [MyInject]

.

Whenever possible, always try to have the container create a type for you, instead of having types with a default constructor and injecting properties afterwards. However, I am not familiar with Caliburn Micro and I am not sure if this is possible.



If you require BuildUp behavior, you can get the registration from the container and ask it to initialize an existing instance for you. It looks like this:

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 dispatch it through the Simple Injector Pipeline , which allows properties and other initializations to be injected according to given registration and (like the property injection example above).

This works great if the types you want to create have a default constructor. If this is not the case, you will have to create (and cache) a new instance Registration

by calling Lifestyle.Transient.CreateRegistration

and calling InitializeInstance

on that instance, as the SimpleInjectorFilterAttributeFilterProvider

MVC Integration Pack does (you can see its source code here ).

+6


source







All Articles