1. 通过组件Id获取组件实例:
[Test]
public void GetByIdTest()
{
ServiceRegistry.Register<Person>("person");
var person = ServiceLocator.Get<IPerson>("person");
Assert.IsTrue(person != null);
var person2 = ServiceLocator.Get<Person>("person");
Assert.IsTrue(person2 != null);
Assert.AreSame(person, person2);
Assert.IsTrue(Person.HasVisited);
}
2. 通过契约类型获取组件实例:
[Test]
public void GetByTypeTest()
{
ServiceRegistry.Register<Person>();
var person = ServiceLocator.Get<IPerson>();
Assert.IsTrue(person != null);
var person2 = ServiceLocator.Get<IPerson,Person>();
Assert.IsTrue(person2 != null);
Assert.AreSame(person, person2);
Assert.IsTrue(Person.HasVisited);
}
3. 通过契约类型获取所有组件实例:
[Contract]
interface IPerson
{
string Name { get; set; }
}
class Person : IPerson
{
public string Name { get; set; }
public static bool HasVisited;
public Person()
{
HasVisited = true;
}
}
[Contract]
interface IHorse
{
}
[Component]
class RedHorse : IHorse
{
}
class BlackHorse : IHorse
{
}
class Person2 : IPerson
{
public string Name { get; set; }
public IHorse Horse { get; set; }
public Person2(IHorse horse)
{
Horse = horse;
}
public Person2() { }
}
[Test]
public void GetAllTest()
{
ServiceRegistry
.Register<Person>()
.Register(typeof(Person2));
var person = ServiceLocator.Get<IPerson>();
Assert.IsTrue(person != null);
Assert.IsTrue(typeof(IPerson).IsAssignableFrom(person.GetType()));
var items = ServiceLocator.GetAll<IPerson>().ToArray();
Assert.IsTrue(items.Length == 2);
Assert.IsTrue(items[0] is Person);
Assert.IsTrue(items[1] is Person2);
}
4. 通过向组件的构造函数传递参数数组的方式获取:
[Contract]
interface IParameterConstructorInterface
{
int Id { get; }
string Name { get; }
IPerson Person { get; }
}
class ParameterConstructorClass : IParameterConstructorInterface
{
public int Id { get; private set; }
public string Name { get; private set; }
public IPerson Person { get; private set; }
public ParameterConstructorClass(int id, string name, IPerson person)
{
Id = id;
Name = name;
Person = person;
}
}
//传递数组的方式
[Test]
public void NeedParameterConstructorTest()
{
var person = new Person();
ServiceRegistry.Register<ParameterConstructorClass>();
var instance = ServiceLocator.Current.Get(typeof(IParameterConstructorInterface), 10, "ZhangSan", person) as IParameterConstructorInterface;
Assert.IsNotNull(instance);
Assert.IsNotNull(instance);
Assert.AreEqual(10, instance.Id);
Assert.IsTrue("ZhangSan"== instance.Name);
Assert.AreSame(person, instance.Person);
}
//传递命名参数字典
[Test]
public void NeedNamedParameterConstructorTest()
{
var ps = new Dictionary<string, object>();
var person = new Person();
ps["id"] = 10;
ps["name"] = "ZhangSan";
ps["person"] = person;
ServiceRegistry.Register<ParameterConstructorClass>();
var instance = ServiceLocator.Current.Get(typeof(IParameterConstructorInterface), ps) as IParameterConstructorInterface;
Assert.IsNotNull(instance);
Assert.AreEqual(10, instance.Id);
Assert.IsTrue("ZhangSan" == instance.Name);
Assert.AreSame(person, instance.Person);
}
Mini 容器官方网站:
http://nlite.codeplex.com/
推荐资源: