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


C# ServiceContainer.Initialize方法代码示例

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


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

示例1: 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

示例2: 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");

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

示例3: ShouldBeAbleToCreateClosedGenericTypeUsingACustomFactoryInstance

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


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