Exemplo n.º 1
0
        private static StashboxContainer CreateContainer(Action <RegistrationConfigurator> scopeConfig = null)
        {
            var sb = new StashboxContainer(
                config => config
                .WithUnknownTypeResolution(ctx => ctx.WhenHas <DoResolveAttribute>())
                .WithAutoMemberInjection(
                    Stashbox.Configuration.Rules.AutoMemberInjectionRules.PropertiesWithPublicSetter,
                    ti => ti.CustomAttributes.OfType <DoResolveAttribute>().Any()
                    )
                );

            var allTypes = new[]
            {
                new Tuple <Type, Type>(typeof(ITier1), typeof(Tier1)),
                new Tuple <Type, Type>(typeof(ITier2), typeof(Tier2)),
                new Tuple <Type, Type>(typeof(TierBase), typeof(Tier3)),
            }.ToList();

            foreach (var type in allTypes)
            {
                var tbuilt     = type.Item2;
                var tinterface = type.Item1;


                var targs = typeof(PrivateArgs <>).MakeGenericType(tinterface);
                sb.Register(tinterface, tbuilt,
                            c =>
                            c
                            .WithFactory(d =>
                {
                    PrivateArgs argContainer = d.Resolve(targs) as PrivateArgs;
                    object[] args            = argContainer?.ArgList;
                    object res = null;
                    try { res = Activator.CreateInstance(tbuilt, args ?? new object[0]); }
                    catch { }

                    return(res);
                })
                            .WithAutoMemberInjection(
                                Stashbox.Configuration.Rules.AutoMemberInjectionRules.PropertiesWithPublicSetter,
                                ti => ti.CustomAttributes.OfType <DoResolveAttribute>().Any())
                            // Simple extension method allowing conditional configuration inline
                            .ApplyIf(scopeConfig != null, scopeConfig)
                            );
            }

            return(sb);
        }
Exemplo n.º 2
0
        public void ContextEstablishedInChildContainersCanBeAccessedWhenUsingAParentScopeConstruction()
        {
            StashboxContainer sb1 = CreateContainer(c => c.WithSingletonLifetime());
            StashboxContainer sb2 = CreateContainer(c => c.WithSingletonLifetime());

            // This works
            sb1.PutInstanceInScope(typeof(PrivateArgs <ITier2>), PrivateArgs <ITier2> .Get("Bob"));
            sb1.PutInstanceInScope(typeof(PrivateArgs <TierBase>), PrivateArgs <TierBase> .Get(5));
            ITier1 renderer = (ITier1)sb1.Resolve(typeof(ITier1));


            using var scope = sb2.BeginScope();
            scope.PutInstanceInScope(typeof(PrivateArgs <ITier2>), PrivateArgs <ITier2> .Get("Bob"));
            scope.PutInstanceInScope(typeof(PrivateArgs <TierBase>), PrivateArgs <TierBase> .Get(5));
            ITier1 renderer2 = (ITier1)scope.Resolve(typeof(ITier1));
        }