Mini容器主要实现了两个接口,一个是服务注册表接口,一个是服务定位器接口。那么组件的注册就要依靠服务注册表接口IServiceRegistry了。
IServiceRegistry接口的定义:
public interface IServiceRegistry { IServiceRegistry Register(IComponentInfo info);//通过组件元数据注册组件到容器中 IServiceRegistry RegisterInstance(string id, object instance);//注册实例 IServiceRegistry RegisterInstance(string id, Type contract, object instance);//注册实例 bool HasRegister(Type contract);//是否注册了实现给定契约接口的组件
bool HasRegister(string id);//是否注册了含有组件Id的组件
void UnRegister(string id);//通过组件Id注销组件 void UnRegister(Type contract);//通过契约类型注销相应的组件 IServiceRegistry Compose(object componentInstance);//将组件实例所依赖的其它组件都通过容器自动注入进来 }
IServiceRegistry的扩展方法:
static IServiceRegistry RegisterInstance<TContract, TComponent>(this IServiceRegistry registry, string id, TComponent instance) where TComponent : TContract static IServiceRegistry RegisterInstance<TContract, TComponent>(this IServiceRegistry registry, TComponent instance) where TComponent : TContract static IServiceRegistry RegisterInstance(this IServiceRegistry registry, object instance) static IServiceRegistry Register<TComponent>(this IServiceRegistry registry) static IServiceRegistry Register<TComponent>(this IServiceRegistry registry, string id) static IServiceRegistry Register(this IServiceRegistry registry, Type contract, Type component) static IServiceRegistry Register<TContract, TComponent>(this IServiceRegistry registry, string id, LifestyleFlags lifestyle) where TComponent : TContract static IServiceRegistry Register<TContract, TComponent>(this IServiceRegistry registry) where TComponent : TContract static IServiceRegistry Register<TContract, TComponent>(this IServiceRegistry registry, string id) where TComponent : TContract static IServiceRegistry Register(this IServiceRegistry registry, Type component) static IServiceRegistry Register(this IServiceRegistry registry, string id, Type component) static IServiceRegistry Register(this IServiceRegistry registry, string id, Type contract, Type component) static IServiceRegistry Register<TContract>(this IServiceRegistry registry, Func<object> creator) static IServiceRegistry Register<TContract>(this IServiceRegistry registry, string id, Func<object> creator) static IServiceRegistry RegisteryFromAssemblyOf<T>(this IServiceRegistry registry)
Mini容器通过核心接口保证了一个轻量级内核,通过扩展方法等来丰富Mini容器的Api。
服务注册表门面类ServiceRegistry:向用户提供注册表接口
public static class ServiceRegistry { public static IServiceRegistry Current{ get; } public static IServiceRegistry Register<TComponent>() public static IServiceRegistry Register<TComponent>(string id) public static IServiceRegistry RegisteryFromAssemblyOf<T>() public static IServiceRegistry Register(params Action<IBindingExpression>[] handlers)//流畅API注册方式 public static IServiceRegistry Register(params Action<IComponentExpression>[] handlers))//流畅API注册方式 public static bool HasRegister<TContract>() public static bool HasRegister(Type service) public static bool HasRegister(string id) public static IServiceRegistry Compose(object component) }
Mini 容器官方网站:
推荐资源: