public static List <DependencyRegistration> GetAll()
 {
     return(new List <DependencyRegistration>()
     {
         DependencyRegistration.BindToInstance(typeof(ISampleExecutorDependency),
                                               new SampleExecutorDependencyImpl())
     });
 }
        public IDependencySetup BindToInstance <T> (T instance)
        {
            if (HasBindingFor <T>())
            {
                throw new InvalidOperationException($"Target type {typeof( T ).Name} is already bound.");
            }

            mDependencyRegistrations.Add(DependencyRegistration.BindToInstance(typeof(T),
                                                                               asInstance: instance));

            return(this);
        }
        public IDependencySetup BindToInstance(Type target, object instance)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            if (HasBindingFor(target))
            {
                throw new InvalidOperationException($"Target type {target.Name} is already bound.");
            }

            mDependencyRegistrations.Add(DependencyRegistration.BindToInstance(target,
                                                                               asInstance: instance));

            return(this);
        }
예제 #4
0
        public void Test_CanLoad_AndResolve_BindToInstance()
        {
            StandardDependencyResolver resolver =
                new StandardDependencyResolver();

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

            IAsSingletonSampleDependency instance = new AsSingletonSampleDependencyImpl();

            dependencies.Add(DependencyRegistration.BindToInstance(typeof(IAsSingletonSampleDependency),
                                                                   instance));

            resolver.Load(dependencies);

            Assert.IsTrue(resolver.CanResolve(typeof(IAsSingletonSampleDependency)));
            Assert.IsTrue(resolver.CanResolve <IAsSingletonSampleDependency>());

            Assert.AreSame(instance,
                           resolver.TryResolve <IAsSingletonSampleDependency>());

            Assert.AreSame(resolver.TryResolve <IAsSingletonSampleDependency>(),
                           resolver.TryResolve <IAsSingletonSampleDependency>());
        }