当前位置: 首页>>代码示例>>C#>>正文


C# ServiceContainer.AddService方法代码示例

本文整理汇总了C#中LinFu.IoC.ServiceContainer.AddService方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceContainer.AddService方法的具体用法?C# ServiceContainer.AddService怎么用?C# ServiceContainer.AddService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在LinFu.IoC.ServiceContainer的用法示例。


在下文中一共展示了ServiceContainer.AddService方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ScopeShouldNeverCallDisposableOnScopedObjectCreatedInAnotherThread

        public void ScopeShouldNeverCallDisposableOnScopedObjectCreatedInAnotherThread()
        {
            var mock = new Mock<IDisposable>();

            var container = new ServiceContainer();
            container.AddService(mock.Object);

            using (var scope = container.GetService<IScope>())
            {
                var signal = new ManualResetEvent(false);
                WaitCallback callback = state =>
                                            {
                                                // Create the service instance
                                                var instance = container.GetService<IDisposable>();
                                                signal.Set();
                                            };

                ThreadPool.QueueUserWorkItem(callback);

                // Wait for the thread to execute
                WaitHandle.WaitAny(new WaitHandle[] {signal});
            }

            // The instance should never be disposed
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:25,代码来源:ScopeTests.cs

示例2: Init

 public static void Init()
 {
     ServiceContainer container = new ServiceContainer();
     container.AddService(typeof(IValidator), typeof(Validator));
     container.AddService(typeof(IEntityDuplicateChecker), typeof(EntityDuplicateCheckerStub));
     ServiceLocator.SetLocatorProvider(() => new LinFuServiceLocator(container));
 }
开发者ID:sztupy,项目名称:shaml,代码行数:7,代码来源:ServiceLocatorInitializer.cs

示例3: Linfu

        public Linfu()
        {
            container = new ServiceContainer();
            container.AddService(typeof(Game), LifecycleType.Singleton);
            container.AddService(typeof(Player), LifecycleType.OncePerRequest);
            container.AddService(typeof(Gun), LifecycleType.OncePerRequest);
            container.AddService(typeof(Bullet), LifecycleType.OncePerRequest);
            container.AddService<Func<Bullet>>(r => () => r.Container.GetService<Bullet>(), LifecycleType.OncePerRequest);

            container.DisableAutoFieldInjection();
            container.DisableAutoMethodInjection();
            container.DisableAutoPropertyInjection();
        }
开发者ID:philiplaureano,项目名称:PaulTest,代码行数:13,代码来源:Linfu.cs

示例4: LinFuAspectContainer

 public LinFuAspectContainer(ServiceContainer container)
 {
     Proxy = new MasterProxy {Container = new LinFuServiceLocatorAdapter(container)};
     _container = container;
     _container.PostProcessors.Add(new AspectPostProcessor());
     _container.AddService(Proxy);
 }
开发者ID:nickspoons,项目名称:Snap,代码行数:7,代码来源:LinFuAspectContainer.cs

示例5: LinFuAspectContainer

 public LinFuAspectContainer(ServiceContainer container)
 {
     Proxy = new MasterProxy();
     _container = container;
     _container.PostProcessors.Add(new AspectPostProcessor());
     _container.AddService(Proxy);
 }
开发者ID:JorgeGamba,项目名称:Snap,代码行数:7,代码来源:LinFuAspectContainer.cs

示例6: TestPropertyInjection

        private static void TestPropertyInjection(string serviceName)
        {
            var mockTarget = new Mock<IInjectionTarget>();
            mockTarget.Expect(t => t.SetValue(123));
            var target = mockTarget.Object;

            var container = new ServiceContainer();
            container.AddService(serviceName, target);

            // Use the named fluent interface for
            // named instances
            if (!String.IsNullOrEmpty(serviceName))
            {
                container.Initialize<IInjectionTarget>(serviceName)
                    .With(service => service.SetValue(123));
            }
            else
            {
                // Otherwise, use the other one
                container.Initialize<IInjectionTarget>()
                    .With(service => service.SetValue(123));
            }
            var result = container.GetService<IInjectionTarget>(serviceName);
            Assert.IsNotNull(result);

            // The container should initialize the
            // service on every request
            mockTarget.VerifyAll();
        }
开发者ID:sdether,项目名称:LinFu,代码行数:29,代码来源:FluentPropertyInjectionTests.cs

示例7: ShouldConstructParametersFromContainer

        public void ShouldConstructParametersFromContainer()
        {
            var targetConstructor = typeof(SampleClassWithMultipleConstructors).GetConstructor(new[] { typeof(ISampleService),
                typeof(ISampleService) });

            // Initialize the container
            var mockSampleService = new Mock<ISampleService>();
            IServiceContainer container = new ServiceContainer();
            container.AddService(mockSampleService.Object);
            container.AddService<IArgumentResolver>(new ArgumentResolver());

            // Generate the arguments using the target constructor
            object[] arguments = targetConstructor.ResolveArgumentsFrom(container);
            Assert.AreSame(arguments[0], mockSampleService.Object);
            Assert.AreSame(arguments[1], mockSampleService.Object);
        }
开发者ID:slieser,项目名称:LinFu,代码行数:16,代码来源:ResolutionTests.cs

示例8: Main

        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();
        }
开发者ID:slieser,项目名称:LinFu,代码行数:28,代码来源:Program.cs

示例9: ShouldCallStronglyTypedFunctorInsteadOfActualFactory

        public void ShouldCallStronglyTypedFunctorInsteadOfActualFactory()
        {
            var container = new ServiceContainer();

            Func<int, int, int> addOperation1 = (a, b) => a + b;
            container.AddService("Add", addOperation1);

            Func<int, int, int, int> addOperation2 = (a, b, c) => a + b + c;
            container.AddService("Add", addOperation2);

            Func<int, int, int, int, int> addOperation3 = (a, b, c, d) => a + b + c + d;
            container.AddService("Add", addOperation3);

            Assert.AreEqual(2, container.GetService<int>("Add", 1, 1));
            Assert.AreEqual(3, container.GetService<int>("Add", 1, 1, 1));
            Assert.AreEqual(4, container.GetService<int>("Add", 1, 1, 1, 1));
        }
开发者ID:philiplaureano,项目名称:LinFu,代码行数:17,代码来源:ResolutionTests.cs

示例10: LazyObjectShouldNeverBeInitialized

        public void LazyObjectShouldNeverBeInitialized()
        {
            var container = new ServiceContainer();
            container.AddService<IProxyFactory>(new ProxyFactory());
            container.AddService<IMethodBuilder<MethodInfo>>(new MethodBuilder());

            Assert.IsTrue(container.Contains(typeof (IProxyFactory)));

            var proxyFactory = container.GetService<IProxyFactory>();
            var interceptor = new LazyInterceptor<ISampleService>(() => new SampleLazyService());

            SampleLazyService.Reset();
            // The instance should be uninitialized at this point
            var proxy = proxyFactory.CreateProxy<ISampleService>(interceptor);
            Assert.IsFalse(SampleLazyService.IsInitialized);

            // The instance should be initialized once the method is called
            proxy.DoSomething();
            Assert.IsTrue(SampleLazyService.IsInitialized);
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:20,代码来源:LazyObjectTests.cs

示例11: CanReturnServiceIfInitializedAndRegistered

        public void CanReturnServiceIfInitializedAndRegistered()
        {
            ServiceContainer container = new ServiceContainer();
            container.LoadFromBaseDirectory("Shaml.Data.dll");
            container.AddService("validator", typeof(IValidator), typeof(Validator), LinFu.IoC.Configuration.LifecycleType.OncePerRequest);

            ServiceLocator.SetLocatorProvider(() => new LinFuServiceLocator(container));

            IValidator validatorService = SafeServiceLocator<IValidator>.GetService();

            Assert.That(validatorService, Is.Not.Null);
        }
开发者ID:sztupy,项目名称:shaml,代码行数:12,代码来源:SafeServiceLocatorTests.cs

示例12: ScopeShouldCallDisposableOnScopedObject

        public void ScopeShouldCallDisposableOnScopedObject()
        {
            var mock = new Mock<IDisposable>();
            mock.Expect(disposable => disposable.Dispose());

            var container = new ServiceContainer();
            container.AddService(mock.Object);

            using (var scope = container.GetService<IScope>())
            {
                // Create the service instance
                var instance = container.GetService<IDisposable>();
            }
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:14,代码来源:ScopeTests.cs

示例13: ScopeShouldNeverCallDisposableOnNonScopedObject

        public void ScopeShouldNeverCallDisposableOnNonScopedObject()
        {
            var mock = new Mock<IDisposable>();
            var container = new ServiceContainer();
            container.AddService(mock.Object);

            using (var scope = container.GetService<IScope>())
            {
            }

            // Create the service instance OUTSIDE the scope
            // Note: this should never be disposed
            var instance = container.GetService<IDisposable>();
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:14,代码来源:ScopeTests.cs

示例14: ShouldAutoInjectClassCreatedWithAutoCreate

        public void ShouldAutoInjectClassCreatedWithAutoCreate()
        {
            // Configure the container
            var container = new ServiceContainer();
            container.LoadFromBaseDirectory("*.dll");

            var sampleService = new Mock<ISampleService>();
            container.AddService(sampleService.Object);

            var instance = (SampleClassWithInjectionProperties)container.AutoCreate(typeof(SampleClassWithInjectionProperties));

            // The container should initialize the SomeProperty method to match the mock ISampleService instance
            Assert.IsNotNull(instance.SomeProperty);
            Assert.AreSame(instance.SomeProperty, sampleService.Object);
        }
开发者ID:sdether,项目名称:LinFu,代码行数:15,代码来源:PropertyInjectionTests.cs

示例15: AroundInvokeClassesMarkedWithInterceptorAttributeMustGetActualTargetInstance

        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);
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:20,代码来源:InversionOfControlTests.cs


注:本文中的LinFu.IoC.ServiceContainer.AddService方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。