static void Main(string[] args) { string directory = AppDomain.CurrentDomain.BaseDirectory; IServiceContainer container = new ServiceContainer(); // Load CarLibrary3.dll; If you need load // all the libaries in a directory, use "*.dll" instead container.LoadFrom(directory, "CarLibrary3.dll"); // Configure the container inject instances // into the Car class constructor container.Inject<IVehicle>() .Using(ioc => new Car(ioc.GetService<IEngine>(), ioc.GetService<IPerson>())) .OncePerRequest(); Person person = new Person(); person.Name = "Someone"; person.Age = 18; container.AddService<IPerson>(person); IVehicle vehicle = container.GetService<IVehicle>(); vehicle.Move(); Console.WriteLine("Press ENTER to continue..."); Console.ReadLine(); }
static void Main(string[] args) { string directory = AppDomain.CurrentDomain.BaseDirectory; IServiceContainer container = new ServiceContainer(); container.LoadFrom(directory, "*.dll"); Console.WriteLine("Press ENTER to continue..."); Console.ReadLine(); }
public void LinFu_Container_Allow_Wildcard_Matching() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); SnapConfiguration.For(new LinFuAspectContainer(container)).Configure(c => { c.IncludeNamespace("SnapTests.*"); c.Bind<HandleErrorInterceptor>().To<HandleErrorAttribute>(); }); Assert.DoesNotThrow(() => container.GetService<IBadCode>()); }
public void LinFu_Container_Fails_Without_Match() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); SnapConfiguration.For(new LinFuAspectContainer(container)).Configure(c => { c.IncludeNamespace("Does.Not.Work"); c.Bind<HandleErrorInterceptor>().To<HandleErrorAttribute>(); }); Assert.Throws<NullReferenceException>(() => container.GetService<IBadCode>()); }
static void Main(string[] args) { string directory = AppDomain.CurrentDomain.BaseDirectory; var container = new ServiceContainer(); // Load CarLibrary.dll; If you need load // all the libaries in a directory, use "*.dll" instead container.LoadFrom(directory, "CarLibrary.dll"); IVehicle vehicle = container.GetService<IVehicle>(); Console.WriteLine("Press ENTER to continue..."); Console.ReadLine(); return; }
public void ShouldAutoInjectMethod() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll"); var instance = new SampleClassWithInjectionMethod(); // Initialize the container container.Inject<ISampleService>().Using<SampleClass>().OncePerRequest(); container.Inject<ISampleService>("MyService").Using(c => instance).OncePerRequest(); var result = container.GetService<ISampleService>("MyService"); Assert.AreSame(result, instance); // On initialization, the instance.Property value // should be a SampleClass type Assert.IsNotNull(instance.Property); Assert.IsInstanceOfType(typeof(SampleClass), instance.Property); }
public void AroundInvokeClassesMarkedWithInterceptorAttributeMustGetActualTargetInstance() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); var mockService = new Mock<ISampleWrappedInterface>(); mockService.Expect(mock => mock.DoSomething()); // Add the target instance container.AddService(mockService.Object); // The service must return a proxy var service = container.GetService<ISampleWrappedInterface>(); Assert.AreNotSame(service, mockService.Object); // Execute the method and 'catch' the target instance once the method call is made service.DoSomething(); var holder = container.GetService<ITargetHolder>("SampleAroundInvokeInterceptorClass"); Assert.AreSame(holder.Target, mockService.Object); }
static void Main(string[] args) { string directory = AppDomain.CurrentDomain.BaseDirectory; IServiceContainer container = new ServiceContainer(); // Load CarLibrary2.dll; If you need load // all the libaries in a directory, use "*.dll" instead container.LoadFrom(directory, "CarLibrary2.dll"); Person person = new Person(); person.Name = "Someone"; person.Age = 18; container.AddService<IPerson>(person); IVehicle vehicle = container.GetService<IVehicle>(); vehicle.Move(); Console.WriteLine("Press ENTER to continue..."); Console.ReadLine(); }
public void ShouldAutoInjectPropertyWithoutCustomAttribute() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll"); var instance = new SampleClassWithUnmarkedInjectionProperties(); // Initialize the container with the dummy service container.Inject<ISampleService>().Using<SampleClass>().OncePerRequest(); container.Inject<ISampleService>("MyService").Using(c => instance).OncePerRequest(); // Enable automatic property injection for every property container.SetCustomPropertyInjectionAttribute(null); // Get the service instance var result = container.GetService<ISampleService>("MyService"); Assert.AreSame(result, instance); // Ensure that the injection occurred Assert.IsNotNull(instance.SomeProperty); Assert.IsInstanceOfType(typeof(SampleClass), instance.SomeProperty); }
static void Main() { #region LinFu //создаем LinFu IoC контейнер var container = new ServiceContainer(); //настраиваем контейнер container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "Logger.dll"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //Для хранилища SQL Server Application.Run(new Form1(container.GetService<ISqlRepository>())); //Для хранилища XML файл //Application.Run(new Form1(container.GetService<IXmlRepository>())); #endregion #region Unity ////создаем IoC Unity //IUnityContainer container = new UnityContainer(); ////Для хранилища SQL Server //container.RegisterType(typeof(IRepository), typeof(EFRepository)); ////Для хранилища XML файл //container.RegisterType(typeof(IRepository), typeof(XmlRepository)); //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(container.Resolve<Form1>()); #endregion }
public void ContainerMustLoadAssemblyFromMemory() { var container = new ServiceContainer(); container.LoadFrom(typeof (SampleClass).Assembly); // Verify that the container loaded the sample assembly into memory Assert.IsTrue(container.Contains(typeof (ISampleService))); }
public void ContainerMustLoadAnyGenericServiceTypeInstanceFromAGenericConcreteClassMarkedWithTheImplementsAttribute() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); string serviceName = "NonSpecificGenericService"; // The container must be able to create any type that derives from ISampleService<T> // despite whether or not the specific generic service type is explicitly registered as a service Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<int>))); Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<double>))); Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<string>))); // Both service types must be valid services Assert.IsNotNull(container.GetService<ISampleGenericService<int>>()); Assert.IsNotNull(container.GetService<ISampleGenericService<double>>()); Assert.IsNotNull(container.GetService<ISampleGenericService<string>>()); }
public void ContainerMustGracefullyHandleRecursiveServiceDependencies() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll"); container.AddService(typeof (SampleRecursiveTestComponent1), typeof (SampleRecursiveTestComponent1)); container.AddService(typeof (SampleRecursiveTestComponent2), typeof (SampleRecursiveTestComponent2)); try { var result = container.GetService<SampleRecursiveTestComponent1>(); } catch (Exception ex) { Assert.IsNotInstanceOfType(typeof (StackOverflowException), ex); } }
public void ShouldBeAbleToCreateClosedGenericTypeUsingACustomFactoryInstance() { var container = new ServiceContainer(); container.Initialize(); container.LoadFrom(typeof (MyClass<>).Assembly); // Get ServiceNotFoundException here instead of a service instance. string serviceName = "frobozz"; var service = container.GetService<MyClass<string>>(serviceName); Console.WriteLine("foo"); Assert.AreEqual(serviceName, service.Value); }
public void ContainerMustCallIInitializeOnServicesCreatedFromCustomFactory() { var mockFactory = new Mock<IFactory>(); var mockInitialize = new Mock<IInitialize>(); mockFactory.Expect(f => f.CreateInstance(It.IsAny<IFactoryRequest>())) .Returns(mockInitialize.Object); // The IInitialize instance must be called once it // leaves the custom factory mockInitialize.Expect(i => i.Initialize(It.IsAny<IServiceContainer>())); var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll"); container.AddFactory(typeof (IInitialize), mockFactory.Object); var result = container.GetService<IInitialize>(); mockFactory.VerifyAll(); mockInitialize.VerifyAll(); }
private static void VerifyInitializeCall(ServiceContainer container, Mock<IInitialize> mockService) { container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); container.AddService(mockService.Object); mockService.Expect(i => i.Initialize(container)).AtMostOnce(); // The container should return the same instance var firstResult = container.GetService<IInitialize>(); var secondResult = container.GetService<IInitialize>(); Assert.AreSame(firstResult, secondResult); // The Initialize() method should only be called once mockService.Verify(); }
public void ShouldSetPropertyValue() { var targetType = typeof(SampleClassWithInjectionProperties); var targetProperty = targetType.GetProperty("SomeProperty"); Assert.IsNotNull(targetProperty); // Configure the target var instance = new SampleClassWithInjectionProperties(); // This is the service that should be assigned // to the SomeProperty property object service = new SampleClass(); // Initialize the container var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll"); IPropertySetter setter = container.GetService<IPropertySetter>(); Assert.IsNotNull(setter); setter.Set(instance, targetProperty, service); Assert.IsNotNull(instance.SomeProperty); Assert.AreSame(service, instance.SomeProperty); }
public void LinFu_Container_Ignores_Types_Without_Decoration() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); SnapConfiguration.For(new LinFuAspectContainer(container)).Configure(c => { c.IncludeNamespace("SnapTests*"); c.Bind<FirstInterceptor>().To<FirstAttribute>(); }); var code = container.GetService<INotInterceptable>(); Assert.IsFalse(code.GetType().Name.Equals("INotInterceptableProxy")); }
public void ShouldAutoInjectServiceListIntoArrayDependency() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll"); var instance = new SampleClassWithArrayPropertyDependency(); // Initialize the container container.Inject<ISampleService>().Using<SampleClass>().OncePerRequest(); container.Inject<SampleClassWithArrayPropertyDependency>().Using(c => instance).OncePerRequest(); var result = container.GetService<SampleClassWithArrayPropertyDependency>(); Assert.IsNotNull(result); Assert.IsNotNull(result.Property); var serviceCount = result.Property.Count(); Assert.IsTrue(serviceCount > 0); }
public void LinFu_Container_Supports_Multiple_Method_Aspects() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); SnapConfiguration.For(new LinFuAspectContainer(container)).Configure(c => { c.IncludeNamespace("SnapTests*"); c.Bind<FirstInterceptor>().To<FirstAttribute>(); c.Bind<SecondInterceptor>().To<SecondAttribute>(); }); var code = container.GetService<IOrderedCode>(); code.RunInOrder(); Assert.AreEqual("First", OrderedCode.Actions[0]); Assert.AreEqual("Second", OrderedCode.Actions[1]); }
public void LinFu_Container_Supports_Method_Aspects() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); SnapConfiguration.For(new LinFuAspectContainer(container)).Configure(c => { c.IncludeNamespace("SnapTests.*"); c.Bind<HandleErrorInterceptor>().To<HandleErrorAttribute>(); }); var x = container.GetService<IBadCode>(); Assert.DoesNotThrow(x.GiddyUp); }
public void ContainerMustLoadSpecificGenericServiceTypesFromAGenericConcreteClassMarkedWithTheImplementsAttribute() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); string serviceName = "SpecificGenericService"; // The container must be able to create both registered service types Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<int>))); Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<double>))); // Both service types must be valid services Assert.IsNotNull(container.GetService<ISampleGenericService<int>>()); Assert.IsNotNull(container.GetService<ISampleGenericService<double>>()); }
public void ShouldLoadStronglyTypedFactoryFromLoadFromExtensionMethod() { var container = new ServiceContainer(); container.LoadFrom(typeof(SampleClass).Assembly); var serviceInstance = container.GetService<ISampleService>("Test"); Assert.IsNotNull(serviceInstance); }
public void ContainerShouldBeAbleToCreateAGenericServiceImplementationThatHasAConstructorWithPrimitiveArguments() { var container = new ServiceContainer(); container.LoadFrom(typeof (SampleService<>).Assembly); ISampleService<int> s = null; // All fail with ServiceNotFoundException. s = container.GetService<ISampleService<int>>(42, "frobozz", false); Assert.IsNotNull(s); s = container.GetService<ISampleService<int>>(42, "frobozz"); Assert.IsNotNull(s); s = container.GetService<ISampleService<int>>(42, false); Assert.IsNotNull(s); s = container.GetService<ISampleService<int>>(null, "frobozz", false); Assert.IsNotNull(s); }
public void ShouldDetermineWhichPropertiesShouldBeInjected() { var targetType = typeof(SampleClassWithInjectionProperties); var targetProperty = targetType.GetProperty("SomeProperty"); Assert.IsNotNull(targetProperty); // Load the property injection filter by default var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll"); var filter = container.GetService<IMemberInjectionFilter<PropertyInfo>>(); Assert.IsNotNull(filter); // The filter should return the targetProperty var properties = filter.GetInjectableMembers(targetType); Assert.IsTrue(properties.Count() > 0); var result = properties.First(); Assert.AreEqual(targetProperty, result); }
public void ContainerMustAllowServicesToBeIntercepted() { var container = new ServiceContainer(); container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); var mock = new Mock<ISampleInterceptedInterface>(); ISampleInterceptedInterface mockInstance = mock.Object; container.AddService(mockInstance); // The container must automatically load the interceptor // from the sample assembly var result = container.GetService<ISampleInterceptedInterface>(); Assert.AreNotSame(mockInstance, result); var proxy = (IProxy) result; Assert.IsNotNull(proxy.Interceptor); }
static void Main(string[] args) { string directory = AppDomain.CurrentDomain.BaseDirectory; IServiceContainer container = new ServiceContainer(); // Load CarLibrary3.dll; If you need load // all the libaries in a directory, use "*.dll" instead container.LoadFrom(directory, "CarLibrary3.dll"); // Inject the OldPerson type container.Inject<IPerson>("OldPerson").Using<Person>(); container.Initialize<IPerson>("OldPerson") .With(p => { p.Age = 99; p.Name = "OldPerson"; }); // Inject the YoungPerson type container.Inject<IPerson>("YoungPerson").Using<Person>(); container.Initialize<IPerson>("YoungPerson") .With(p => { p.Age = 16; p.Name = "YoungPerson"; }); // Inject the DeadEngine type container.Inject<IEngine>("DeadEngine").Using<DeadEngine>() .OncePerRequest(); // Inject the OldEngine type container.Inject<IEngine>("OldEngine").Using<OldEngine>() .OncePerRequest(); // Inject the BrokenVehicle type into the container container.Inject<IVehicle>("BrokenVehicle") .Using<Car>().OncePerRequest(); #region Broken Vehicle Configuration AddVehicle(container, "BrokenVehicle", "DeadEngine", "YoungPerson"); #endregion #region Old Vehicle Configuration AddVehicle(container, "OldVehicle", "OldEngine", "OldPerson"); #endregion // Inject the OldVehicle type into the container container.Inject<IVehicle>("OldVehicle") .Using<Car>().OncePerRequest(); Person person = new Person(); person.Name = "Someone"; person.Age = 18; container.AddService<IPerson>(person); var brokenVehicle = container.GetService<IVehicle>("BrokenVehicle"); var oldVehicle = container.GetService<IVehicle>("OldVehicle"); Console.Write("Broken Vehicle: "); brokenVehicle.Move(); Console.Write("Old Vehicle: "); oldVehicle.Move(); Console.WriteLine("Press ENTER to continue..."); Console.ReadLine(); }