public void Value_Test2() { var container = new IocContainer(); var childContainer = new IocContainer(container); container.Add("A", 15); Assert.Equal(15, childContainer.Get("A")); int index = 0; container.Add("B", lmp => ++ index); Assert.Equal(1, childContainer.Get("B")); Assert.Equal(2, childContainer.Get("B")); index = 0; container.Add("C", lmp => ++ index, true); Assert.Equal(1, childContainer.Get("C")); Assert.Equal(1, childContainer.Get("C")); Assert.True(childContainer.Contains("A", true)); container.Remove("A", true); Assert.False(childContainer.Contains("A", true)); index = 0; container.Add("D", lmp => ++ index); Assert.Equal(1, childContainer.Get("D")); Assert.Equal(2, container.Get("D")); }
public void TypeValue_Test2() { var type = typeof(IValueService); var container = new IocContainer(); var childContainer = new IocContainer(container); container.Add(type, "A", 15); Assert.Equal(15, childContainer.Get(type, "A")); int index = 0; container.Add(type, "B", lmp => ++ index); Assert.Equal(1, childContainer.Get(type, "B")); Assert.Equal(2, childContainer.Get(type, "B")); index = 0; container.Add(type, "C", lmp => ++ index, true); Assert.Equal(1, childContainer.Get(type, "C")); Assert.Equal(1, childContainer.Get(type, "C")); Assert.True(childContainer.Contains(type, "A", true)); container.Remove(type, "A", true); Assert.False(childContainer.Contains(type, "A", true)); index = 0; container.Add(type, "D", lmp => ++ index); Assert.Equal(1, childContainer.Get(type, "D")); Assert.Equal(2, container.Get(type, "D")); }
public void Value_Test3() { var parentContainer = new IocContainer(); var childContainer = new IocContainer(parentContainer); parentContainer.Add("value1", 1); parentContainer.Add("value2", "2"); parentContainer.Add("value3", false); childContainer.Add("value1", 9999); childContainer.Add("value2", "9999"); childContainer.Add(typeof(IValueService), typeof(ValueService1)); parentContainer.Add(typeof(IValueService), typeof(ValueService2)); var childService = childContainer.Get(typeof(IValueService)) as IValueService; var parentService = parentContainer.Get(typeof(IValueService)) as IValueService; Assert.Equal(1, parentService.Value1); Assert.Equal("2", parentService.Value2); Assert.Equal(false, parentService.Value3); Assert.Equal(9999, childService.Value1); Assert.Equal("9999", childService.Value2); Assert.Equal(false, childService.Value3); }
/// <summary> /// 创建一个用于命令模型的服务容器。 /// </summary> /// <param name="userFactory">用户工厂。</param> /// <param name="mockFactoryCallback">模拟的执行器工厂回调函数。</param> /// <param name="redisProvider">Redis 提供程序。若为 null 值表示启用基于应用程序域各种提供程序的服务容器。</param> /// <returns>服务容器。</returns> public static IIocContainer CreateContainer(IUserFactory userFactory , Action <MockExecutorFactory> mockFactoryCallback = null , IRedisProvider redisProvider = null) { if (userFactory == null) { userFactory = new UserFactory(c => null); } var container = new IocContainer(); container.Add(userFactory); if (redisProvider != null) { container.Add(redisProvider); } if (mockFactoryCallback != null) { var executorFactory = new MockExecutorFactory(container); mockFactoryCallback(executorFactory); container.Add <IExecutorFactory>(executorFactory); } if (Db.Engine != null) { container.Add <IDbEngine>(lmps => Db.Context); } else { container.Add <IDbEngine>(lmps => new DbEngine(new SqlEngineProvider("UNIT TEST"))); } return(container); }
public void LastMappingTest2() { var container = new IocContainer(); container.Add("baseValue1", 10); container.Add("baseValue2", 20); container.Add(typeof(LastMapperTestModel)); var ex = Assert.Throws <ArgumentException>(() => container.Get(typeof(LastMapperTestModel), 50)); Assert.Equal("value1", ex.ParamName); }
public void LastMappingTest1() { var container = new IocContainer(); container.Add("baseValue1", 10); container.Add("baseValue2", 20); container.Add(typeof(LastMapperTestModel)); var model = container.Get(typeof(LastMapperTestModel), 50, 80) as LastMapperTestModel; Assert.Equal(10, model.BaseValue1); Assert.Equal(20, model.BaseValue2); Assert.Equal(80, model.Value1); Assert.Equal(50, model.Value2); }
public TestBase() { var container = new IocContainer(); this._factory = new ExecutorFactory(container); var type = this.GetType(); var db = type.GetAttribute <DbAttribute>(); var provider = db == null ? "ce" : db.Provider; switch (provider) { case "sql": this._testManager = new MsSqlTestManager(); break; default: this._testManager = new MsCeTestManager(); break; } this._Engine = this._testManager.Engine; container.Add <IDbEngine>(lmps => this.Context); var classScripts = this.GetType().GetAttribute <ScriptsAttribute>(); if (classScripts != null) { this._testManager.Execute(classScripts.Keys); } }
public void AddService_TypeObjectBoolean_Test1() { var container = new IocContainer(); container.Add(typeof(IService2), new XService2()); var childContainer = new IocContainer(container); Assert.IsAssignableFrom<XService2>(childContainer.Get(typeof(IService2))); }
public void DefaultMappingTest() { var container = new IocContainer(); var service = container.Get <IDefaultMappingService>(); Assert.IsType <DefaultMappingService2>(service); container.DestroyAll(); container.Add(typeof(IDefaultMappingService)); service = container.Get <IDefaultMappingService>(); Assert.IsType <DefaultMappingService2>(service); container.DestroyAll(); container.Add(typeof(IDefaultMappingService), typeof(DefaultMappingService)); service = container.Get <IDefaultMappingService>(); Assert.IsType <DefaultMappingService>(service); }
public void GetServiceTest2() { var type = typeof(DefaultService1); var container = new IocContainer(); container.Add(type, true); var childContainer = new IocContainer(container); Assert.Equal(childContainer.Get(type), container.Get(type)); }
public void GetUserTest() { var container = new IocContainer(); var username = "******"; var s = new SimpleCommandModelService(container); Assert.Null(s.User); container.Add <IUserFactory>(new UserFactory(ioc => username)); Assert.Equal(username, s.User); }
public void AddService_TypeObjectBoolean_Test1() { var container = new IocContainer(); container.Add(typeof(IService2), new XService2()); var childContainer = new IocContainer(container); Assert.IsAssignableFrom <XService2>(childContainer.Get(typeof(IService2))); }
public void GetServiceTest3() { var type = typeof(DefaultService1); var container = new IocContainer(); var childContainer = new IocContainer(container); container.Add(type, true); childContainer.Add(type, true); Assert.NotEqual(childContainer.Get(type), container.Get(type)); }
public void ContainsServiceServiceTest1() { var container = new IocContainer(); var childContainer = new IocContainer(container); var itype = typeof(IService2); childContainer.Add(itype, lmp => new XService2(), promote: true); Assert.True(childContainer.Contains(itype)); childContainer.Remove(itype); Assert.False(childContainer.Contains(itype)); Assert.True(childContainer.Contains(itype, true)); }
public void GetUserTest() { var container = new IocContainer(); var command = new SimpleCommand(); var context = CreateContext(container, command); Assert.Null(context.User); var username = "******"; container.Add <IUserFactory>(new UserFactory(ioc => username)); Assert.Equal(username, context.User); }
public void TypeValue_Test3() { var parentContainer = new IocContainer(); var childContainer = new IocContainer(parentContainer); parentContainer.Add("value1", 1); parentContainer.Add("value2", "2"); parentContainer.Add("value3", false); childContainer.Add("value1", 9999); childContainer.Add("value2", "9999"); childContainer.Add(typeof(IValueService), typeof(ValueService1)); parentContainer.Add(typeof(IValueService), typeof(ValueService2)); var childService = childContainer.Get(typeof(IValueService)) as IValueService; var parentService = parentContainer.Get(typeof(IValueService)) as IValueService; Assert.Equal(1, parentService.Value1); Assert.Equal("2", parentService.Value2); Assert.Equal(false, parentService.Value3); Assert.Equal(9999, childService.Value1); Assert.Equal("9999", childService.Value2); Assert.Equal(false, childService.Value3); childContainer.Add(typeof(IValueService), "value2", "8888"); var childService2 = childContainer.Get(typeof(IValueService)) as IValueService; Assert.Equal(9999, childService2.Value1); Assert.Equal("9999", childService2.Value2); //- 因为已映射,所以这里的值还是历史值 Assert.Equal(false, childService2.Value3); childContainer.Remove(typeof(IValueService)); childContainer.Add(typeof(IValueService), typeof(ValueService1)); var childService3 = childContainer.Get(typeof(IValueService)) as IValueService; Assert.Equal("8888", childService3.Value2); }
public void AddService_TypeBooleanBoolean_Test2() { var type = typeof(DefaultService1); var itype = typeof(IService1); var container = new IocContainer(); container.Add(type, true); Assert.NotNull(container.Get(type)); Assert.NotNull(container.Get(itype)); Assert.Equal(container.Get(type), container.Get(type)); Assert.NotEqual(container.Get(type), container.Get(itype)); Assert.IsAssignableFrom(type, container.Get(itype)); }
public void RemoveServiceTest2() { var container = new IocContainer(); var childContainer = new IocContainer(container); var itype = typeof(IService2); childContainer.Add(itype, lmp => new XService2(), promote: true); Assert.True(container.Contains(itype)); Assert.True(childContainer.Contains(itype)); container.Add(itype, lmp => new DefaultService2()); Assert.IsAssignableFrom <XService2>(childContainer.Get(itype)); Assert.IsAssignableFrom <DefaultService2>(container.Get(itype)); childContainer.Remove(itype, true); Assert.False(container.Contains(itype)); Assert.False(childContainer.Contains(itype)); }
public void DefaultMappingTest2() { var container = new IocContainer(); var service = container.Get<DefaultMappingCtorService>(); Assert.IsType<DefaultMappingService2>(service.InnerService); container.DestroyAll(); container.Add(typeof(IDefaultMappingService), typeof(DefaultMappingService)); service = container.Get<DefaultMappingCtorService>(); Assert.IsType<DefaultMappingService>(service.InnerService); }
public void Value_Test2() { var container = new IocContainer(); var childContainer = new IocContainer(container); container.Add("A", 15); Assert.Equal(15, childContainer.Get("A")); int index = 0; container.Add("B", lmp => ++index); Assert.Equal(1, childContainer.Get("B")); Assert.Equal(2, childContainer.Get("B")); index = 0; container.Add("C", lmp => ++index, true); Assert.Equal(1, childContainer.Get("C")); Assert.Equal(1, childContainer.Get("C")); Assert.True(childContainer.Contains("A", true)); container.Remove("A", true); Assert.False(childContainer.Contains("A", true)); index = 0; container.Add("D", lmp => ++index); Assert.Equal(1, childContainer.Get("D")); Assert.Equal(2, container.Get("D")); }
public void TypeValue_Test2() { var type = typeof(IValueService); var container = new IocContainer(); var childContainer = new IocContainer(container); container.Add(type, "A", 15); Assert.Equal(15, childContainer.Get(type, "A")); int index = 0; container.Add(type, "B", lmp => ++index); Assert.Equal(1, childContainer.Get(type, "B")); Assert.Equal(2, childContainer.Get(type, "B")); index = 0; container.Add(type, "C", lmp => ++index, true); Assert.Equal(1, childContainer.Get(type, "C")); Assert.Equal(1, childContainer.Get(type, "C")); Assert.True(childContainer.Contains(type, "A", true)); container.Remove(type, "A", true); Assert.False(childContainer.Contains(type, "A", true)); index = 0; container.Add(type, "D", lmp => ++index); Assert.Equal(1, childContainer.Get(type, "D")); Assert.Equal(2, container.Get(type, "D")); }
public void LastMappingTest2() { var container = new IocContainer(); container.Add("baseValue1", 10); container.Add("baseValue2", 20); container.Add(typeof(LastMapperTestModel)); var ex = Assert.Throws<ArgumentException>(() => container.Get(typeof(LastMapperTestModel), 50)); Assert.Equal("value1", ex.ParamName); }
public void RemoveServiceTest1() { var container = new IocContainer(); var childContainer = new IocContainer(container); var itype = typeof(IService2); childContainer.Add(itype, lmp => new XService2(), promote: true); Assert.True(container.Contains(itype)); Assert.True(childContainer.Contains(itype)); container.Add(itype, lmp => new DefaultService2()); Assert.IsAssignableFrom<XService2>(childContainer.Get(itype)); Assert.IsAssignableFrom<DefaultService2>(container.Get(itype)); childContainer.Remove(itype); Assert.IsAssignableFrom<DefaultService2>(childContainer.Get(itype)); Assert.IsAssignableFrom<DefaultService2>(container.Get(itype)); Assert.False(childContainer.Contains(itype)); }