Skip to content

Migrating from other libraries

Mapping Injectio and AutoRegisterInject attributes and defaults onto Immediate.Injections.

2 min read

Both libraries below register services from attributes, so the mechanical translation is small. The part that bites is the default registration strategy: Immediate.Injections defaults to registering a class as itself, where the others do not. Set an assembly default first, then port the attributes.

From Injectio

AssemblyInfo.cs
using Immediate.Injections.Shared;

[assembly: RegistrationDefaults(
	RegistrationStrategy = RegistrationStrategy.SelfAndImplementedInterfaces,
	UseProxyFactory = true
)]
InjectioImmediate.Injections
[RegisterSingleton], [RegisterScoped], [RegisterTransient]Same names
Registration = ...RegistrationStrategy = ...
RegistrationStrategy.SelfWithInterfacesRegistrationStrategy.SelfAndImplementedInterfaces
Duplicate = ...DuplicateStrategy = ...
Tags = "foo,bar" (comma-separated string)Tags = ["foo", "bar"] (string array)
Assembly-name override via MSBuild property[assembly: ImmediateAssemblyIdentifier("Name")]

The tag change is the one to grep for — Tags = "foo,bar" will not compile against a string[]? property, so the compiler finds them all for you.

From AutoRegisterInject

AssemblyInfo.cs
using Immediate.Injections.Shared;

[assembly: RegistrationDefaults(
	RegistrationStrategy = RegistrationStrategy.ImplementedInterfaces
)]
AutoRegisterInjectImmediate.Injections
[RegisterSingleton], [RegisterScoped], [RegisterTransient]Same names
[TryRegisterSingleton] and friends[RegisterSingleton(DuplicateStrategy = DuplicateStrategy.Skip)]

The TryRegisterXxx family has no equivalent attribute; the “only register if absent” behavior is now a property, so a [TryRegisterScoped<IService>] becomes:

[RegisterScoped<IService>(DuplicateStrategy = DuplicateStrategy.Skip)]

If most of your registrations were TryRegisterXxx, set the assembly default instead and drop the property from each attribute:

[assembly: RegistrationDefaults(
	RegistrationStrategy = RegistrationStrategy.ImplementedInterfaces,
	DuplicateStrategy = DuplicateStrategy.Skip
)]

After either migration

  • Replace the old library’s startup call with the generated AddXxxServices().
  • Build once and read the diagnostics. INJ0003–INJ0012 catch most translation mistakes at compile time; see Diagnostics.
  • Registrations that silently vanish rather than erroring are almost always a generic class with a Factory or UseProxyFactory — see Open generics.