使用 IOC 容器 往往分为两个阶段
配置阶段
使用阶段
在 配置阶段 我们可能会选择 配置文件 、对所有依赖的程序集反射 、 对指定程序反射 、 硬编码 等方式对组件进行 注册 。
对所有依赖的程序集反射 ,怎么看都是一种即笨重又有些呆板的方法。
然而另外几个方案在多模块化的项目里中 ,
也无法很好的工作,
它们各自需要在系统启动时,明确一些信息 :
一共需要读取哪些配置文件,这些文件在各个子模块的哪儿
一共需要对哪些程序集进行反射
那些负责硬编码注册的类型都在哪儿
如果上述的信息没有被收集完整,那么程序会因为没有注册全部组件而无法正确运行。
事实上
所以通过 Attribute 加 扫描,是一种很好的 自动化注册 手段。
假定我们需要实现下面的接口
// Services/IUserService.cs
public interface IUserService
{
void CreateUser(User user);
}
实现过程大致如下
// Services/DefaultUserService.cs
public class DefaultUserService : IUserService
{
private readonly IRepository userRepo ;
public DefaultUserService(IRepository<User> userRepo) { this.userRepo = userRepo; } public void CreateUser(User user) { this.userRepo.Insert(user); }
讯享网
}
使用 Reface.AppStarter ,你不再需要编写额外的代码将这个 DefaultUserService 注册到 IUserService 上。
你只需要为 DefaultUserService 添加一个 ComponentAttribute 。
// Services/DefaultUserService.cs
[Component]
public class DefaultUserService : IUserService
{
// …
}
这个 特征 就是通知 Reface.AppStarter 将该类型注册到 IOC 容器 中,并注册到它所有实现的接口类型上。
现在我们为刚才 DefaultUserService 所在的 程序集 编写一个 应用模块 的类型。
// UserAppModule.cs
[ComponentScanAppModule]
public class UserAppModule : AppModule
{
}
这就是一个 用户应用模块 ( 所有的 应用模块 应当从 AppModule 继承 ),
它依赖了一个 ComponentScanAppModule 的 应用模块 。
ComponentScanAppModule 会将 目标应用模块 中标记了 ComponentAttribute 的类型注册到 IOC 容器中。
运行程序
public static void Main(String[] args)
{
var app = AppSetup.Start();
IUserService service = app.CreateComponent();
// you can use service now;
}
除了直接启动 UserAppModule ,
也可以将 UserAppModule 加到别的 应用模块 上,这个 自动注册 同样有效。
基于这样的方式, UserAppModule 完全可以开箱即用。
也可以说,任何一个 应用模块 都可以开箱即用。
自动映射配置文件
配置文件总是存在的, .NetFramework 提供了一整套 Configuration 方案。
但是这个方案略显笨重,并且在配置过程完全没有提示,必须依赖大量的文档说明。
所以 Reface.AppStarter 选择了 JSON 作为配置文件的格式 ,
Reface.AppStarter 会读取一个包含了所有配置的 JSON 文件,
将配置中的值映射的 对应 的类型和属性中,
并将这些 类型 以单例的模式注册到 IOC 容器中。
为一个类型添加 ConfigAttribute 就会通知 Reface.AppStarter : “嘿,这有一个配置类”。
下面的例子表示,我们可以通过配置文件指定 根用户的信息
// Configs/UserConfig
[Config(“User”)]
public class UserConfig
{
public string RootUserName { get; set; } = “admin”;
public string RootUserPassword { get; set; } = “”;
}
ConfigAttribute 的构造函数中指定的 User 表示从配置文件的 User 节点中读取配置,配置文件大至如下:
{
“User” : {
“RootUserName” : “ROOT”,
“RootUserPassword” : “”
}
}
如果没有配置文件,或者配置文件中没有编写 User 则会使用属性中指定的默认值将配置类实例注册到 IOC 容器 中。
通过构造函数注入配置类就可以使用它们。
// Services/DefaultUserService.cs
public class DefaultUserService : IUserService
{
private readonly IRepository userRepo;
private readonly UserConfig userConfig;
讯享网public DefaultUserService(IRepository<User> userRepo, UserConfig userConfig) { this.userRepo = userRepo; this.userConfig = userConfig; } public void CreateUser(User user) { if(user.Name == userConfig.RootUserName) throw new SomeException("无效的用户名,不能创建该用户"); this.userRepo.Insert(user); }
// UserAppModule.cs
[ComponentScanAppModule] // 开启自动扫描注册组件
[AutoConfigAppModule] // 开启自动映射配置文件
public class UserAppModule : AppModule
{
}
现在启动 UserAppModule
或者将 UserAppModule 添加到其它 应用模块 的依赖项中,
这些功能都会被启用。
为你的 app.json 添加 $schema 属性,值指向 app.schema.json 文件,就可以得到智能提示功能:
TS1
TS2
你也可以通过上图显示的属性关闭生成 app.schema.json 的功能。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/131193.html