Skip to content

Sharing singleton instances between Resolve and ResolveAll and subtypes #129

Answered by zastrowm
zastrowm asked this question in Q&A
Discussion options

You must be logged in to vote

It looks like I can get the desired behavior by manually registering the types in the assembly and using AsImplementedTypes:

using var container = new StashboxContainer();

foreach (var type in typeof(ITA_T1).Assembly.GetTypes()
            .Where(it => it.Name.StartsWith("TA_") && it.IsClass && !it.IsAbstract))
{
    container.Register(
    type,
    configurator: opt => opt.WithSingletonLifetime()
                            .AsImplementedTypes()
    );
}

var all = container.ResolveAll<ITA_T1>();

// sanity test that there is a TA_T1
Assert.Contains(all, it => it is TA_T1);

// however they aren't the same instance
var instance = container.Resolve<TA_T1>();
Assert.Contains(all, it => it 

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@z4kn4fein
Comment options

@zastrowm
Comment options

@z4kn4fein
Comment options

Answer selected by zastrowm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants