New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AppendInstance<TService> and Append<TService>(Func<TService> instanceCreator, Lifestyle lifestyte) to Collection #603

Open
Metaphis opened this Issue Aug 7, 2018 · 3 comments

Comments

2 participants
@Metaphis

Metaphis commented Aug 7, 2018

Registering a single instance as part of a collection or a single singleton requires to supply a registration to to the Append(Type, Registration) method. Registrations are not well documented and figuring out how to accomplish this is difficult. These two overloads would provide the same set of registrations options to a collection as they would to the container itself.

Alternatively doing work to make creating registration more straightforward could work around this issue.

@dotnetjunkie dotnetjunkie added this to the Backlog milestone Aug 7, 2018

@dotnetjunkie

This comment has been minimized.

Collaborator

dotnetjunkie commented Aug 7, 2018

Great suggestion.

In the meantime, you can achieve this by doing the following:

container.Collection.Append(typeof(ISomeType),
    Lifestyle.Singleton.CreateRegistration<ISomeType>(() => new SomeTypeImpl(), container));

If you have many such registrations, you can add an extension method to simplify things:

public static void Append<T>(
    this ContainerCollectionRegistrator registrator, T instance, Container container)
    where T : class
{
    registrator.Append(typeof(T),
        Lifestyle.Singleton.CreateRegistration(() => instance, container));
}

Which can be used as follows:

container.Collection.Append<IMyType>(new MyTypeImpl(), container);

@dotnetjunkie dotnetjunkie changed the title from Add AppendInstance<TService> and Append<TService>(Func<TService> instanceCreator, Lifestyle lifesyte) to Collection to Add AppendInstance<TService> and Append<TService>(Func<TService> instanceCreator, Lifestyle lifestyte) to Collection Aug 7, 2018

@Metaphis

This comment has been minimized.

Metaphis commented Aug 7, 2018

Thanks for the code sample. I've done exactly that. One note here is that the Container on the ContainerCollectionRegistrator is private. It would make for nicer extensions if the container was exposed so that the extension could be written this way:

public static void Append<T>(
    this ContainerCollectionRegistrator registrator, T instance)
    where T : class
{
    registrator.Append(typeof(T),
        Lifestyle.Singleton.CreateRegistration(() => instance, registrator.Container));
}
@dotnetjunkie

This comment has been minimized.

Collaborator

dotnetjunkie commented Aug 7, 2018

It would make for nicer extensions if the container was exposed so that the extension could be written this way

I came to the same conclusion while writing my previous extension method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment