注册
CustomerController
还有集装箱。
public static void Main(string[] args) {
var container = new UnityContainer()
.RegisterType<ICustomerService, CustomerService>()
.RegisterType<CustomerController>();
CustomerController c = container.Resolve<CustomerController>();
c.Operation();
//...
}
这个
container
解析控制器时将插入依赖项
实际上不再需要默认构造函数
[InjectionConstructor]
属性,如果依赖项仅通过其他构造函数使用
public class CustomerController {
private readonly ICustomerService _customerService;
[InjectionConstructor]
public CustomerController(ICustomerService customerService) {
_customerService = customerService;
}
public void Operation() {
Console.WriteLine(_customerService.Operation());
}
}