container icon indicating copy to clipboard operation
container copied to clipboard

New IUnityContainer.Register Method

Open ENikS opened this issue 6 years ago • 0 comments

Net IUnityContainer.Register() method

As explained in #336 current registration strategy that Unity uses to register contracts is not very efficient. It is very chatty and allocates a some objects on the heap.

To optimize the process several changes are being made:

  • Registration information is collected and passed to the container in RegistrationDescriptor structure
  • Unity no longer provides different methods for registration of Types, Instances, and Factories
  • New method Register is added to IUnityContainer interface instead of RegisterType, RegisterInstance, and RegisterFactory

The Register method

The new method is defined as follows:

IUnityContainer Register(params RegistrationDescriptor[] descriptors);

in fact, two methods are added to the interface. An overload that takes ReadOnlySpan<RegistrationDescriptor> span is also provided:

IUnityContainer Register(in ReadOnlySpan<RegistrationDescriptor> span);

This second overload helps when host program attempts to limit allocations on the stack.

To pass registration data to the method Unity defines RegistrationDescriptor structure

RegistrationDescriptor structure

The simplified declaration is as follows:

public readonly struct RegistrationDescriptor
{
    #region Constructors

    // Type registration
    public RegistrationDescriptor(Type type, string? name, ITypeLifetimeManager manager, params Type[] registerAs)
    {
    }

    // Instance registration
    public RegistrationDescriptor(object? instance, string? name, IInstanceLifetimeManager manager, params Type[] registerAs)
    {
    }

    // Factory registration
    public RegistrationDescriptor(ResolveDelegate<IResolveContext> factory, string? name, IFactoryLifetimeManager manager, params Type[] registerAs)
    {
    }

    #endregion


    #region Public Members

    public readonly Type[] RegisterAs;
        
    public readonly string? Name;

    public readonly Type? Type;

    public readonly object? Instance;

    public readonly ResolveDelegate<IResolveContext>? Factory;

    #endregion
}

The structure provides several constructors with signatures that allow Type, Instance, and Factory registrations. Each constructor has a parameter RegisterAs which allows registration of multiple contracts/aliases/interfaces for the component.

Legacy support

For backwards compatibility extension methods RegisterType, RegisterInstance, and RegisterFactory with matching signatures are created. Obviously, these are much slower than the new method and should not be used for new development.

Calling convention

The new method could be used in several different ways:

Creating structures directly

In most simplest case this notation can be used for registrations:

Container.Register(
    new RegistrationDescriptor(typeof(Foo), null, new TransientLifetimeManager()),
    new RegistrationDescriptor(typeof(Type), null, (IInstanceLifetimeManager)new ContainerControlledLifetimeManager(), typeof(object)));

This statement creates three registrations:

  • a type registration with Foo registered as Foo
  • an instance registration with type Type registered as Object

Simple, but not very intuitive or easy to read. To help with creating these structures Unity adds several extension methods

Extension methods

Directly creating RegistrationDescription structures is fast, but sometimes clarity of code is more important that few lost cycles. To help with proper registration syntax Unity defines few extension methods. With extension methods the same registration will look like this:

Container.Register(
    typeof(Foo).RegisterType()
               .WithInjectionMembers(...),
    typeof(Type).RegisterInstance(typeof(object));

Extension methods are not free, in terms of overhead, but not terrible either. The data is passed and returned by reference and at most only two stack parameters are allocated for each call:


BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.959 (1909/November2018Update/19H2)
AMD Ryzen Threadripper 2970WX, 1 CPU, 48 logical and 24 physical cores
.NET Core SDK=5.0.100-preview.8.20363.2
  [Host]     : .NET Core 5.0.0 (CoreCLR 5.0.20.36102, CoreFX 5.0.20.36102), X64 RyuJIT
  DefaultJob : .NET Core 5.0.0 (CoreCLR 5.0.20.36102, CoreFX 5.0.20.36102), X64 RyuJIT


Method Mean Error StdDev
'new RegistrationDescriptor()' 29.39 ns 0.305 ns 0.255 ns
RegisterXXX() 29.75 ns 0.268 ns 0.238 ns

ENikS avatar Jul 11 '20 03:07 ENikS