示例#1
0
        public void Test_CanLoad_AndResolve_BindToProviderInstance(int nTestThreads)
        {
            StandardDependencyResolver resolver =
                new StandardDependencyResolver();

            List <DependencyRegistration> dependencies =
                new List <DependencyRegistration>();

            dependencies.Add(DependencyRegistration.BindToProviderInstance(typeof(IAsSingletonSampleDependency),
                                                                           new AsSingletonSampleDependencyProvider(),
                                                                           DependencyScope.Singleton));

            dependencies.Add(DependencyRegistration.BindToProviderInstance(typeof(IAsThreadSingletonSampleDependency),
                                                                           new AsThreadSingletonSampleDependencyProvider(),
                                                                           DependencyScope.Thread));

            dependencies.Add(DependencyRegistration.BindToProviderInstance(typeof(IAsTransientSampleDependency),
                                                                           new AsTransientSampleDependencyProvider(),
                                                                           DependencyScope.Transient));

            resolver.Load(dependencies);

            Assert_DependenciesCanBeResolved(resolver);
            Assert_DependenciesCorrectlyResolved(nTestThreads, resolver);
        }
        public IDependencyBindingScopeSetup BindToProviderInstance <T, TImplementation> (IDependencyProvider <TImplementation> implementationProvider)
            where TImplementation : T
        {
            if (HasBindingFor <T>())
            {
                throw new InvalidOperationException($"Target type {typeof( T ).Name} is already bound.");
            }

            DependencyRegistration reg = DependencyRegistration.BindToProviderInstance(typeof(T),
                                                                                       asProviderInstance: implementationProvider,
                                                                                       scope: DependencyScope.Transient);

            mDependencyRegistrations.Add(reg);
            return(new StandardDependencyBindingScopeSetup(reg));
        }
        public IDependencySetup BindToProviderInstance <T, TImplementation> (IDependencyProvider <TImplementation> implementationProvider,
                                                                             DependencyScope scope)
            where TImplementation : T
        {
            if (HasBindingFor <T>())
            {
                throw new InvalidOperationException($"Target type {typeof( T ).Name} is already bound.");
            }

            mDependencyRegistrations.Add(DependencyRegistration.BindToProviderInstance(typeof(T),
                                                                                       asProviderInstance: implementationProvider,
                                                                                       scope: scope));

            return(this);
        }