本文整理汇总了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();
}
示例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)));
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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();
}
}
}