public override IComponentRegistry RegisterInstance(Type dependencyType, object instance) { Guard.AgainstNull(dependencyType, "dependencyType"); Guard.AgainstNull(instance, "instance"); base.RegisterInstance(dependencyType, instance); try { _container.RegisterSingleton(dependencyType, () => instance); } catch (Exception ex) { throw new TypeRegistrationException(ex.Message, ex); } return(this); }
static void Test() { var dry = new DryIoc.Container(); dry.Register <IMetricSubmitter, EnrichMetricsDecorator>( setup: new DecoratorSetup(z => { return(z.Parent.ImplementationType == typeof(SingleWorker)); }, order: 0, useDecorateeReuse: false)); dry.Register <IMetricSubmitter, DefaultMetricSubmitter>(Reuse.Singleton); dry.Register <SingleWorker>(Reuse.Singleton); dry.Register <ScopedWorker>(Reuse.Singleton); var worker = dry.Resolve <SingleWorker>(); Console.WriteLine(worker); var worker2 = dry.Resolve <ScopedWorker>(); Console.WriteLine(worker2); var container = new global::SimpleInjector.Container(); container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle(); container.ForConsumer <BConsumer>() .RegisterSingleton <AInterface, BImplementation>(); container.ForConsumer <AConsumer>() .Register <AInterface, AImplementation>(Lifestyle.Singleton); container.RegisterSingleton <AConsumer>(); container.RegisterDecorator( typeof(AInterface), typeof(AInterfaceDecorator), Lifestyle.Singleton, z => { return(true); }); container.RegisterDecorator( typeof(AInterface), typeof(BInterfaceDecorator), Lifestyle.Singleton, z => { return(true); }); container.Register <SingleWorker>(Lifestyle.Singleton); container.Register <ScopedWorker>(Lifestyle.Scoped); container.RegisterDecorator <IMetricSubmitter, EnrichMetricsDecorator>(); container.RegisterConditional <IMetricSubmitter, ProxyMetricSubmitter>( Lifestyle.Singleton, z => z.Consumer.ImplementationType == typeof(SingleWorker)); container.Verify(); container.GetInstance <SingleWorker>(); using (AsyncScopedLifestyle.BeginScope(container)) { container.GetInstance <ScopedWorker>(); } container.GetInstance <AConsumer>(); container.GetInstance <AConsumer>(); container.GetInstance <BConsumer>(); container.GetInstance <BConsumer>(); }