container icon indicating copy to clipboard operation
container copied to clipboard

IUnityContainer improvements

Open ENikS opened this issue 6 years ago • 0 comments

Modifications to IUnityContainer interface

IUnityContainer interface did not change much since version 3.5. This version introduces several changes and improvements

Adding property Name

Unity containers are lucking any means to be identified by external sources and extensions. This prevents extensions from implementing various scenarios where precise targeting is required. For example, Unity.Configuration allows creating named configuration sections, but got no means to associate these with particular instance of the container.

To fix this limitation property Name is being introduced to the IUnityContainer interface:

string? Name { get; }

Preallocating storage

Unity manages storage required for registrations automatically. It tries to keep footprint down and allocates memory in 25% - 30% increments. For large number of registrations it creates unnecessary overhead. To solve this issue method CreateChildContainer been changed to accept a name, and required capacity:

IUnityContainer CreateChildContainer(string? name, int capacity);

Replacing registration methods

Unity exposes three methods: RegisterType, RegisterInstance, and RegisterFactory. These are used for registering types, instances, and factories with the container. This arrangement creates very flexible, but rather slow solution. Overhead created by calling these methods for every single registration is significant and slows registration process dramatically.

To optimize registration process and eliminate method call overhead a new method is introduced:

IUnityContainer Register(params RegistrationDescriptor[] registrations);

For more information see unitycontainer/container#242

ENikS avatar Jun 26 '20 18:06 ENikS