Mar*_*nez 4 c# simple-injector
如何归档自动注册,但忽略已经注册的任何类型?我在Simple Injector文档中引用了代码
var repositoryAssembly = typeof(SqlUserRepository).Assembly;
var registrations =
from type in repositoryAssembly.GetExportedTypes()
where type.Namespace == "MyComp.MyProd.BL.SqlRepositories"
where type.GetInterfaces().Any()
select new
{
Service = type.GetInterfaces().Single(),
Implementation = type
};
foreach (var reg in registrations)
{
// TODO: how to check reg.Service has already registered or not
container.Register(reg.Service, reg.Implementation, Lifestyle.Transient);
}
Run Code Online (Sandbox Code Playgroud)
例如,我具有ISampleRepository接口,并且在不同的程序集中有2个实现
项目1:工作
var container = new Container();
container.AutoRegistration();
Run Code Online (Sandbox Code Playgroud)
项目2:异常,因为ISampleRepository已注册
var container = new Container();
container.Register<ISampleRepository, OverrideSampleRepository>();
container.AutoRegistration();
Run Code Online (Sandbox Code Playgroud)
该文档描述了如何覆盖现有注册。该文档包含以下示例:
var container = new Container();
container.Options.AllowOverridingRegistrations = true;
// Register IUserService.
container.Register<IUserService, FakeUserService>();
// Replaces the previous registration
container.Register<IUserService, RealUserService>();
Run Code Online (Sandbox Code Playgroud)
但是,请务必仔细阅读文档。
| 归档时间: |
|
| 查看次数: |
1707 次 |
| 最近记录: |