container icon indicating copy to clipboard operation
container copied to clipboard

Optimize events

Open ENikS opened this issue 6 years ago • 0 comments

Optimization of registration and child container events

Extensions registered with a container presented with ExtensionContext exposing these events:

public event EventHandler<RegisterEventArgs> Registering;
public event EventHandler<RegisterInstanceEventArgs> RegisteringInstance;
public event EventHandler<ChildContainerCreatedEventArgs> ChildContainerCreated;

These events are raised when either type or instance are registered or child container is created.

Issues with current implementation

There are several problems with current implementation:

  • The implementation uses inefficient generic EventHandler<T>
  • Event invocation requires instantiation of wrapper classes RegisterEventArgs, RegisterInstanceEventArgs, ChildContainerCreatedEventArgs and adds a lot of overhead.
  • No event for factory registration

Optimizing Events

Implementation could be streamlined if events are implemented with designated event handler delegates:

Event handlers

New event handlers are declared like this:

/// <summary>
/// Registration event handler
/// </summary>
/// <param name="container">Container where the registration took place</param>
/// <param name="registration">Reference to <see cref="RegistrationData"/> structure</param>
public delegate void RegistrationEvent(object container, ref RegistrationData registration);

/// <summary>
/// Child container created event handler
/// </summary>
/// <param name="parent">Container creating the child</param>
/// <param name="child">Context of created child container</param>
public delegate void ChildCreatedEvent(object parent, ExtensionContext child);

Event Declarations

Instead of three different events, there are only two events: one for all registrations and another one for child container creation

/// <summary>
/// This event is raised on new registration
/// </summary>
public abstract event RegistrationEvent Registering;

/// <summary>
/// This event is raised when the <see cref="IUnityContainer.CreateChildContainer"/> 
/// method is called and new child container is created. It allow extensions to 
/// perform any additional initialization they may require.
/// </summary>
public abstract event ChildCreatedEvent ChildContainerCreated;

Breaking changes

Registering event

Changes to Registering event as following:

  • Parameter container is the registering UnityContainer. It could reference either the current container or, for global singleton registrations, the root container.

  • Instead of RegisterEventArgs class, structure RegistrationData containing all registration information will be passed by reference.

ChildContainerCreated event

Event ChildContainerCreated is changed as follows:

  • ChildContainer is no longer passed as part of ChildContainerCreatedEventArgs. Instead child ExtensionContext contains property Container

  • Type of second parameter is changed from ChildContainerCreatedEventArgs to ExtensionContext. The context is of newly created child container.

ENikS avatar Dec 20 '19 05:12 ENikS