我正在研究一個小型類庫並使用簡單注射器作爲我的DI。 類庫有一個接入點(我猜想某種服務),它是public
,它有一些internal
服務和存儲庫。簡單的注射器和內部構造函數
我看到Simple Injector不支持帶內部構造函數的構造函數注入。 例如,我的產品服務是這樣的:
internal class ProductService : IProductService
{
private IProductRepository _productRepository;
internal ProductService(IProductRepository repository)
{
if (repository == null) throw new ArgumentNullException("repository");
_productRepository = repository;
}
}
而且我的設置:
container.Register<IProductService, ProductService>();
container.Register<IProductRepository>(() => new ProductRepository());
當我運行代碼,我得到以下異常:
For the container to be able to create ProductService, it should contain exactly one public constructor, but it has 0.
我的問題:
1)是否有一個特定的原因,注入內部類不起作用建築/設計術語?
2)這種行爲(使用依賴注入與不應該公開的類)是如何實現的並且是它的期望?
的原因是什麼這些類是內部的,而不是公衆:
這個自定義構造函數解析行爲可以如下注冊? – Steven