我最近一直在使用依赖注入(Unity)来实现我的领域层和任何基础设施问题之间的低耦合。
我已经结束了很多Unity 容器代码。
一个小片段:
container.RegisterType<IUnitOfWork, EntityFrameworkUnitOfWork>("FirstContext", new PerResolveLifetimeManager(), new InjectionConstructor(new FirstContext()));
container.RegisterType<IUnitOfWork, EntityFrameworkUnitOfWork>("AnotherContext", new PerResolveLifetimeManager(), new InjectionConstructor(new AnotherContext()));
// User Aggregate
container.RegisterType<IEntityMapper<User, UserTable>, UserMapper>();
container.RegisterType<IUserRepository, UserRepository>(
new InjectionConstructor(
new ResolvedParameter<IUnitOfWork>("FirstContext"),
new ResolvedParameter<IEntityMapper<User, UserTable>>()
)
);
// Token Aggregate
container.RegisterType<IEntityMapper<Token, TokenTable>, TokenMapper>();
container.RegisterType<ITokenRepository, TokenRepository>(
new InjectionConstructor(
new ResolvedParameter<IUnitOfWork>("FirstContext"),
new ResolvedParameter<IEntityMapper<Token, TokenTable>>()
)
);
// Payment Aggregate
container.RegisterType<IReadOnlyEntityMapper<Payment, PaymentTable>, PaymentFactory>();
container.RegisterType<IPaymentRepository, PaymentRepository>(
new InjectionConstructor(
new ResolvedParameter<IUnitOfWork>("FirstContext"),
new ResolvedParameter<IReadOnlyEntityMapper<Payment, PaymentTable>>()
)
);
// Customer Aggregate
container.RegisterType<IReadOnlyEntityMapper<Customer, CustomerTable>, CustomerMapper>();
container.RegisterType<ICustomerRepository, CustomerRepository>(
new InjectionConstructor(
new ResolvedParameter<IUnitOfWork>("AnotherContext"),
new ResolvedParameter<IReadOnlyEntityMapper<Customer, CustomerTable>>()
)
);
// Country Aggregate
container.RegisterType<IReadOnlyEntityMapper<Country, CountryTable>, CountryMapper>();
container.RegisterType<ICountryRepository, CountryRepository>(
new InjectionConstructor(
new ResolvedParameter<IUnitOfWork>("AnotherContext"),
new ResolvedParameter<IReadOnlyEntityMapper<Country, CountryTable>>()
)
);
// Province Aggregate
container.RegisterType<IReadOnlyEntityMapper<Province, ProvinceTable>, ProvinceMapper>();
container.RegisterType<IProvinceRepository, ProvinceRepository>(
new InjectionConstructor(
new ResolvedParameter<IUnitOfWork>("AnotherContext"),
new ResolvedParameter<IReadOnlyEntityMapper<Province, ProvinceTable>>()
)
);
有没有更好的方法来组织这个?
我似乎无法在网上找到任何示例/文章/方向。