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


C# ServiceContainer.Inject方法代码示例

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


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

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

示例2: Inject

 private static void Inject(string serviceName, Action<IGenerateFactory<ISampleService>> usingFactory,
                            Func<IUsingLambda<ISampleService>, IGenerateFactory<ISampleService>> doInject,
                            ServiceContainer container)
 {
     // HACK: Condense the fluent statements into a single,
     // reusable line of code
     usingFactory(doInject(container.Inject<ISampleService>(serviceName)));
 }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:8,代码来源:FluentExtensionTests.Implementation.cs

示例3: ShouldAutoInjectMethod

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

示例4: ShouldAutoInjectPropertyWithoutCustomAttribute

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

示例5: ContainerMustAllowSurrogatesForNonExistentServiceInstances

        public void ContainerMustAllowSurrogatesForNonExistentServiceInstances()
        {
            var container = new ServiceContainer();
            var mockService = new Mock<ISampleService>();
            ISampleService surrogate = mockService.Object;
            container.Inject<ISampleService>().Using((f, arguments) => surrogate).OncePerRequest();

            var result = container.GetService<ISampleService>();
            Assert.IsNotNull(result);
            Assert.AreSame(surrogate, result);
        }
开发者ID:svgorbunov,项目名称:ScrollsModLoader,代码行数:11,代码来源:InversionOfControlTests.cs

示例6: ShouldAutoInjectServiceListIntoArrayDependency

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

示例7: ShareDataProviderTest

        public ShareDataProviderTest()
        {
            _container = new ServiceContainer();
            _container.Inject<DbProviderFactory>().Using(() => DbProviderFactories.GetFactory("System.Data.SQLite")).AsSingleton();
            _container.AddService("ConnectionString", "Data Source=ShareDataProviderTest.db3");
            _provider = new ShareDataProvider
                            {
                                ConnectionString =
                                    _container.GetService<string>("ConnectionString"),
                                DbFactory = _container.GetService<DbProviderFactory>()
                            };

            DbProviderFactory factory = _container.GetService<DbProviderFactory>();

            using (DbConnection connection = factory.CreateConnection())
            {
                connection.ConnectionString = _container.GetService<string>("ConnectionString");
                connection.Open();

                using (DbTransaction transaction = connection.BeginTransaction())
                {
                    using (DbCommand command = connection.CreateCommand())
                    {
                        command.CommandText = "DROP TABLE IF EXISTS SHARES";

                        command.ExecuteNonQuery();
                    }

                    using (DbCommand command = connection.CreateCommand())
                    {
                        command.CommandText =
                            @"CREATE TABLE SHARES (
                    id INTEGER PRIMARY KEY,
                    name TEXT NOT NULL UNIQUE,
                    path TEXT NOT NULL
                    )";

                        command.ExecuteNonQuery();

                        command.CommandText = "INSERT INTO SHARES(id, name, path) VALUES(NULL, @name, @path)";
                        IDictionary<string, string> shares = new Dictionary<string, string>
                                                                 {
                                                                     {"shareA", "pathA"},
                                                                     {"shareB", "pathB"}
                                                                 };

                        foreach (var share in shares)
                        {
                            command.Parameters.Clear();

                            DbParameter parameter = _container.GetService<DbProviderFactory>().CreateParameter();
                            parameter.DbType = DbType.String;
                            parameter.ParameterName = "@name";
                            parameter.Value = share.Key;

                            command.Parameters.Add(parameter);

                            parameter = _container.GetService<DbProviderFactory>().CreateParameter();
                            parameter.DbType = DbType.String;
                            parameter.ParameterName = "@path";
                            parameter.Value = share.Value;

                            parameter.Value = share.Value;

                            command.Parameters.Add(parameter);

                            command.ExecuteNonQuery();
                        }
                    }

                    transaction.Commit();
                }
            }
        }
开发者ID:browsk,项目名称:XStreamer,代码行数:74,代码来源:ShareDataProviderTest.cs


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