Skip to content

AddExtension(...) method #222

@ENikS

Description

@ENikS

Method AddExtension(...)

Separating extension management from IUnityContainer

v4.x implementation of IUnityContainer interface had two methods related to extension management:

public interface IUnityContainer
{
    ...
    IUnityContainer AddExtension(UnityContainerExtension extension);
    object Configure(Type configurationInterface);
    ...

In version 5.x it was removed from the interface and added to the UnityContainer class itself. The reason behind this decision was to consolidate extensions in one place and optimize memory and performance.

Although methods no longer reside on the interface, it still returns reference to IUnityContainer instead of UnityContainer.

Discrepancy in argument for AddExtension between v4.x and v5.x

Versions 4 and 5 implemented argument differently:

public interface IUnityContainer
{
    ...
    IUnityContainer AddExtension(UnityContainerExtension extension);              <-- v4
    IUnityContainer AddExtension(IUnityContainerExtensionConfigurator extension); <-- v5
    ...

In version v5 the argument is more permissive and does not account for the requirement for extension to be UnityContainerExtension. This creates a potential for runtime error and should be fixed.

Fix for AddExtension

Next version of Unity will change signature of AddExtension to be:

UnityContainer AddExtension(UnityContainerExtension extension);

AddExtension overload for delegates

An extension derived from UnityContainerExtension has two properties: Container and Context and an abstract method Initialize. (Method Remove has been deprecated, for more info see this issue). Method Initialize is where extension performs it's magic through Container and Context.

For cases where extension does not do anything more than changing some settings, UnityContainer adds support for extension delegates. The delegate could be any method taking one parameter of type ExtensionContext (The context has property Container and is not required to be passed to the method).

class UnityContainer
{
    . . .

    public UnityContainer AddExtension(Action<ExtensionContext> method);

    . . .
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions