Пример #1
0
        public virtual void TestBindToMethod()
        {
            var parameters = new IIocParameter[] { new IocParameter("test", "Test", new IocParameterType("Test")) };

            using (IIocContainer iocContainer = GetIocContainer())
            {
                iocContainer.BindToMethod <ISimple>((adapter, list) =>
                {
                    list.SequenceEqual(parameters).ShouldBeTrue();
                    return(new Simple());
                }, DependencyLifecycle.SingleInstance);

                var simple = (ISimple)iocContainer.Get(typeof(ISimple), null, parameters);
                simple.ShouldNotBeNull();
                iocContainer.Get <ISimple>(null, parameters).ShouldEqual(simple);
                iocContainer.Get(typeof(ISimple), null, parameters).ShouldEqual(simple);
            }

            using (IIocContainer iocContainer = GetIocContainer())
            {
                iocContainer.BindToMethod(typeof(ISimple), (adapter, list) =>
                {
                    list.SequenceEqual(parameters).ShouldBeTrue();
                    return(new Simple());
                }, DependencyLifecycle.SingleInstance);

                var simple = (ISimple)iocContainer.Get(typeof(ISimple), null, parameters);
                simple.ShouldNotBeNull();
                iocContainer.Get <ISimple>(null, parameters).ShouldEqual(simple);
                iocContainer.Get(typeof(ISimple), null, parameters).ShouldEqual(simple);
            }

            using (IIocContainer iocContainer = GetIocContainer())
            {
                iocContainer.BindToMethod <ISimple>((adapter, list) =>
                {
                    list.SequenceEqual(parameters).ShouldBeTrue();
                    return(new Simple());
                }, DependencyLifecycle.TransientInstance);

                var simple = (ISimple)iocContainer.Get(typeof(ISimple), null, parameters);
                simple.ShouldNotBeNull();
                iocContainer.Get <ISimple>(null, parameters).ShouldNotEqual(simple);
                iocContainer.Get(typeof(ISimple), null, parameters).ShouldNotEqual(simple);
            }

            using (IIocContainer iocContainer = GetIocContainer())
            {
                iocContainer.BindToMethod(typeof(ISimple), (adapter, list) =>
                {
                    list.SequenceEqual(parameters).ShouldBeTrue();
                    return(new Simple());
                }, DependencyLifecycle.TransientInstance);

                var simple = (ISimple)iocContainer.Get(typeof(ISimple), null, parameters);
                simple.ShouldNotBeNull();
                iocContainer.Get <ISimple>(null, parameters).ShouldNotEqual(simple);
                iocContainer.Get(typeof(ISimple), null, parameters).ShouldNotEqual(simple);
            }
        }
Пример #2
0
 protected virtual IParameter ConvertParameter(IIocParameter parameter, bool isDelegate)
 {
     if (parameter == null)
     {
         return(null);
     }
     if (parameter.ParameterType == IocParameterType.Constructor)
     {
         if (isDelegate)
         {
             return(new ConstructorArgument(parameter.Name, context => parameter.Value));
         }
         return(new ConstructorArgument(parameter.Name, parameter.Value));
     }
     if (parameter.ParameterType == IocParameterType.Property)
     {
         if (isDelegate)
         {
             return(new PropertyValue(parameter.Name, context => parameter.Value));
         }
         return(new PropertyValue(parameter.Name, parameter.Value));
     }
     Tracer.Warn("The parameter with type {0} is not supported", parameter.ParameterType);
     return(null);
 }
 public override void ShouldResolveClassWithDynamicParameters()
 {
     using (IIocContainer iocContainer = GetIocContainer())
     {
         var parameters = new IIocParameter[]
         {
             new IocParameter("parameterConstructor", "parameterConstructor", IocParameterType.Constructor),
         };
         iocContainer.Bind(typeof(ParameterClass), typeof(ParameterClass), DependencyLifecycle.SingleInstance);
         var parameterClass = iocContainer.Get <ParameterClass>(parameters: parameters);
         parameterClass.ParameterConstructor.ShouldEqual(parameters[0].Value);
     }
 }
Пример #4
0
 public virtual void ShouldResolveClassWithPredefinedParameters()
 {
     using (IIocContainer iocContainer = GetIocContainer())
     {
         var parameters = new IIocParameter[]
         {
             new IocParameter("parameterConstructor", "parameterConstructor", IocParameterType.Constructor),
             new IocParameter("ParameterProperty", "ParameterProperty", IocParameterType.Property)
         };
         iocContainer.Bind(typeof(ParameterClass), typeof(ParameterClass), DependencyLifecycle.SingleInstance, parameters: parameters);
         var parameterClass = iocContainer.Get <ParameterClass>();
         parameterClass.ParameterConstructor.ShouldEqual(parameters[0].Value);
         parameterClass.ParameterProperty.ShouldEqual(parameters[1].Value);
     }
 }
Пример #5
0
 public virtual void ShouldPassPredefinedParametersToMethodDelegate()
 {
     using (IIocContainer iocContainer = GetIocContainer())
     {
         var parameters = new IIocParameter[]
         {
             new IocParameter("parameterConstructor", "parameterConstructor", IocParameterType.Constructor),
             new IocParameter("ParameterProperty", "ParameterProperty", IocParameterType.Property)
         };
         iocContainer.BindToMethod(typeof(Simple), (container, list) =>
         {
             list.SequenceEqual(parameters).ShouldBeTrue();
             return(new Simple());
         }, DependencyLifecycle.SingleInstance, parameters: parameters);
         iocContainer.Get <Simple>();
     }
 }
Пример #6
0
 protected virtual Parameter ConvertParameter(IIocParameter parameter)
 {
     if (parameter == null)
     {
         return(null);
     }
     if (parameter.ParameterType == IocParameterType.Constructor)
     {
         return(new NamedParameter(parameter.Name, parameter.Value));
     }
     if (parameter.ParameterType == IocParameterType.Property)
     {
         return(new NamedPropertyParameter(parameter.Name, parameter.Value));
     }
     Tracer.Warn("The parameter with type {0} is not supported", parameter.ParameterType);
     return(null);
 }
        public void GetViewModelShouldUseIocContainerToCreateViewModelWithParameters()
        {
            var          parameters = new IIocParameter[0];
            const string vmName     = "vmName";
            int          initialize = 0;

            var context = new DataContext
            {
                { InitializationConstants.ViewModelBindingName, vmName },
                { InitializationConstants.IocParameters, parameters }
            };
            var vm = new ViewModel
            {
                InitializeViewModel = dataContext =>
                {
                    dataContext.ShouldEqual(context);
                    ++initialize;
                }
            };
            var iocContainer = new IocContainerMock
            {
                GetFunc = (type, s, arg3) =>
                {
                    s.ShouldEqual(vmName);
                    arg3.ShouldEqual(parameters);
                    type.ShouldEqual(typeof(ViewModel));
                    return(vm);
                }
            };

            ViewModelProvider provider  = GetViewModelProvider(iocContainer);
            IViewModel        viewModel = provider.GetViewModel(typeof(ViewModel), context);

            viewModel.ShouldEqual(vm);
            initialize.ShouldEqual(1);
        }
Пример #8
0
 private static bool CanResolve(IIocParameter parameter, ParameterInfo parameterInfo)
 {
     return(parameter.ParameterType == IocParameterType.Constructor && parameter.Name == parameterInfo.Name);
 }
 /// <summary>
 ///     Converts parameter.
 /// </summary>
 protected virtual IParameter ConvertParameter(IIocParameter parameter)
 {
     if (parameter == null)
         return null;
     if (parameter.ParameterType == IocParameterType.Constructor)
         return new ConstructorArgument(parameter.Name, parameter.Value);
     if (parameter.ParameterType == IocParameterType.Property)
         return new PropertyValue(parameter.Name, parameter.Value);
     Tracer.Warn("The parameter with type {0} is not supported", parameter.ParameterType);
     return null;
 }
Пример #10
0
 private static IList<IIocParameter> GetParameters(IIocParameter[] iocParameters, IBindingContext context)
 {
     if (context.Parameters == null || context.Parameters.Count == 0)
         return iocParameters;
     List<IIocParameter> result = null;
     foreach (var parameter in context.Parameters.OfType<ParameterContainer>())
     {
         if (result == null)
             result = new List<IIocParameter>();
         result.AddRange(parameter.Parameters);
     }
     if (result == null)
         return iocParameters;
     if (iocParameters != null)
         result.AddRange(iocParameters);
     return result;
 }
Пример #11
0
 protected virtual IInjectionParameter ConvertParameter(IIocParameter parameter, bool isDelegate)
 {
     if (parameter == null)
         return null;
     if (parameter.ParameterType == IocParameterType.Constructor)
     {
         if (isDelegate)
             return new ConstructorParameter(parameter.Name, context => parameter.Value);
         return new ConstructorParameter(parameter.Name, parameter.Value);
     }
     if (parameter.ParameterType == IocParameterType.Property)
     {
         if (isDelegate)
             return new PropertyParameter(parameter.Name, context => parameter.Value);
         return new PropertyParameter(parameter.Name, parameter.Value);
     }
     Tracer.Warn("The parameter with type {0} is not supported", parameter.ParameterType);
     return null;
 }
Пример #12
0
        public virtual void TestBindToMethodNamed()
        {
            const string named      = "named";
            var          parameters = new IIocParameter[] { new IocParameter("test", "Test", IocParameterType.Property) };

            using (IIocContainer iocContainer = GetIocContainer())
            {
                iocContainer.BindToMethod <ISimple>((adapter, list) =>
                {
                    list.SequenceEqual(parameters).ShouldBeTrue();
                    return(new Simple());
                }, DependencyLifecycle.SingleInstance, named);

                var simple = (ISimple)iocContainer.Get(typeof(ISimple), named, parameters);
                simple.ShouldNotBeNull();
                iocContainer.Get <ISimple>(named, parameters).ShouldEqual(simple);
                iocContainer.Get(typeof(ISimple), named, parameters).ShouldEqual(simple);

                Action action = () => iocContainer.Get <ISimple>();
                action.ShouldThrow();
            }

            using (IIocContainer iocContainer = GetIocContainer())
            {
                iocContainer.BindToMethod(typeof(ISimple), (adapter, list) =>
                {
                    list.SequenceEqual(parameters).ShouldBeTrue();
                    return(new Simple());
                }, DependencyLifecycle.SingleInstance, named);

                var simple = (ISimple)iocContainer.Get(typeof(ISimple), named, parameters);
                simple.ShouldNotBeNull();
                iocContainer.Get <ISimple>(named, parameters).ShouldEqual(simple);
                iocContainer.Get(typeof(ISimple), named, parameters).ShouldEqual(simple);

                Action action = () => iocContainer.Get <ISimple>();
                action.ShouldThrow();
            }

            using (IIocContainer iocContainer = GetIocContainer())
            {
                iocContainer.BindToMethod <ISimple>((adapter, list) =>
                {
                    list.SequenceEqual(parameters).ShouldBeTrue();
                    return(new Simple());
                }, DependencyLifecycle.TransientInstance, named);

                var simple = (ISimple)iocContainer.Get(typeof(ISimple), named, parameters);
                simple.ShouldNotBeNull();
                iocContainer.Get <ISimple>(named, parameters).ShouldNotEqual(simple);
                iocContainer.Get(typeof(ISimple), named, parameters).ShouldNotEqual(simple);

                Action action = () => iocContainer.Get <ISimple>();
                action.ShouldThrow();
            }

            using (IIocContainer iocContainer = GetIocContainer())
            {
                iocContainer.BindToMethod(typeof(ISimple), (adapter, list) =>
                {
                    list.SequenceEqual(parameters).ShouldBeTrue();
                    return(new Simple());
                }, DependencyLifecycle.TransientInstance, named);

                var simple = (ISimple)iocContainer.Get(typeof(ISimple), named, parameters);
                simple.ShouldNotBeNull();
                iocContainer.Get <ISimple>(named, parameters).ShouldNotEqual(simple);
                iocContainer.Get(typeof(ISimple), named, parameters).ShouldNotEqual(simple);

                Action action = () => iocContainer.Get <ISimple>();
                action.ShouldThrow();
            }
        }
        public void GetViewModelShouldUseIocContainerToCreateViewModelWithParameters()
        {
            var parameters = new IIocParameter[0];
            const string vmName = "vmName";
            int initialize = 0;

            var context = new DataContext
            {
                {InitializationConstants.ViewModelBindingName, vmName},
                {InitializationConstants.IocParameters, parameters}
            };
            var vm = new ViewModel
            {
                InitializeViewModel = dataContext =>
                {
                    dataContext.ShouldEqual(context);
                    ++initialize;
                }
            };
            var iocContainer = new IocContainerMock
            {
                GetFunc = (type, s, arg3) =>
                {
                    s.ShouldEqual(vmName);
                    arg3.ShouldEqual(parameters);
                    type.ShouldEqual(typeof(ViewModel));
                    return vm;
                }
            };

            ViewModelProvider provider = GetViewModelProvider(iocContainer);
            IViewModel viewModel = provider.GetViewModel(typeof(ViewModel), context);
            viewModel.ShouldEqual(vm);
            initialize.ShouldEqual(1);
        }
Пример #14
0
 private static IList<IIocParameter> GetParameters(IIocParameter[] iocParameters, IEnumerable<Parameter> parameters)
 {
     if (parameters == null)
         return iocParameters;
     List<IIocParameter> result = null;
     foreach (var parameter in parameters.OfType<ParameterContainer>())
     {
         if (result == null)
             result = new List<IIocParameter>();
         result.AddRange(parameter.Parameters);
     }
     if (result == null)
         return iocParameters;
     if (iocParameters != null)
         result.AddRange(iocParameters);
     return result;
 }
 protected override object GetFunc(Type type, string s, IIocParameter[] arg3)
 {
     if (type == typeof(IEntityStateManager))
         return StateManager;
     return base.GetFunc(type, s, arg3);
 }
 protected override object GetFunc(Type type, string s, IIocParameter[] arg3)
 {
     if (type == typeof(IValidatorProvider))
         return ValidatorProvider;
     return base.GetFunc(type, s, arg3);
 }