Simple Injector 4.1:覆盖参数注入行为

时间:2018-03-27 20:16:24

标签: c# c#-4.0 dependency-injection simple-injector

我很擅长在C#中使用DI,并看看Windsor,Ninject,Autofac,Unity和Simple Injector。我最初放弃了Simple Injector,因为我需要值类型注入(fx。连接字符串),但发现了一个描述这个的博客文章:https://cuttingedge.it/blogs/steven/pivot/entry.php?id=94。不幸的是,博客文章已过时,因为版本4.1中已弃用IDependencyInjectionBehavior.BuildExpression,而IDependencyInjectionBehavior.GetInstanceProducer已被引入。 我不确定如何使用新的InstanceProducer执行blogpost所描述的内容。 InstanceProducer有一个静态方法FromExpression,但我不确定应该使用哪种类型等。

我目前正在使用Pure DI并进行以下设置:

// Settings
var conLocal = ConfigurationManager.ConnectionStrings["APIPortMan"].ConnectionString;
var con = ConfigurationManager.ConnectionStrings["PortMan"].ConnectionString;
var conAzure = ConfigurationManager.ConnectionStrings["Azure"].ConnectionString;
var conSitecore = ConfigurationManager.ConnectionStrings["Sitecore"].ConnectionString;
var azureStorageAccount = ConfigurationManager.AppSettings.Get("StorageConnection");
var reportUploadPath = ConfigurationManager.AppSettings.Get("ReportUploadPath");
var PfsmlPath = ConfigurationManager.AppSettings.Get("PfsmlPath");
var reloadCounter = int.Parse(ConfigurationManager.AppSettings.Get("transactionServiceReloadCounter"));
var systemStartDate = DateTime.Parse(ConfigurationManager.AppSettings.Get("holdingServiceStartDate"));
var semaphoreCount = int.Parse(ConfigurationManager.AppSettings.Get("semaphoreCount"));

然后我有一些使用这些设置的存储库:

// Repositories
var _accountRepository = new AccountRepository(con, conAzure);
var _aggregatedPortfolioRelationshipRepository = new AggregatedPortfolioRelationshipRepository(conAzure);
var _aggregatedClientRelationshipRepository = new AggregatedClientRelationshipRepository(con, conAzure);
var _assetBondRepository = new AssetBondRepository(con, conAzure);
var _assetClassRepository = new AssetClassRepository(con, conAzure);
var _assetDerivativeRepository = new AssetDerivativeRepository(con, conAzure);
var _assetRepository = new AssetRepository(con, conAzure);
var _benchmarkRepository = new BenchmarkRepository(con, conAzure);
var _benchmarkWeightRepository = new BenchmarkWeightRepository(con, conAzure);
var _clientRepository = new ClientRepository(con, conAzure);
var _defaultPriceRepository = new DefaultPriceRepository(con, conAzure);
var _emailRepository = new UpdateEmailOutput(conAzure);
var _exchangeRateRepository = new ExchangeRateRepository(con, conAzure);
var _failedHoldingRepository = new FailedHoldingRepository(conLocal);
var _GICSRepository = new GICSRepository(con, conAzure);
var _holdingRepository = new HoldingRepository(conAzure);
var _limitLineRepository = new LimitLineRepository(con, conAzure);
var _PFSMLRepository = new PFSMLRepository(PfsmlPath);
var _portfolioRepository = new PortfolioRepository(con, conAzure);
var _sitecoreReportRepository = new SitecoreReportRepository(conSitecore, reportUploadPath);
var _systemInfoRepository = new SystemInfoRepository(conAzure);
var _transactionRepository = new TransactionRepository(con, conAzure);

后来一些服务消耗了存储库和一些设置。

由于大多数存储库共享一个公共接口IRepository<T>(除了IAssetRepository之类的单个接口,它扩展了类型为Asset的IRepository),我希望能够使用批处理创建/自动布线。另外,我希望每次对主代码进行更改时都避免更改DI设置(即新的构造函数参数,新的存储库接口/类等)。我也想避免使用lambda&#34;&#34; new&#34;和getInstance在注册过程中,因为每当我更改构造函数时都需要更改DI设置。

我已经在构造函数中采用了blogpost中提到的约定(使用AzureConnectionStringPortManConnectionString等)。我现在需要做的就是确保Simple Injector根据约定处理值类型参数:)

1 个答案:

答案 0 :(得分:2)

我在Github上发布了同样的问题(正如Steven所说)。 https://github.com/simpleinjector/SimpleInjector/blob/v4.0.x/src/SimpleInjector.CodeSamples/ParameterConventionExtensions.cs包含基于约定的方法的更新版本,尽管Steven / dotnetjunkie说服我采用不同的方法(使用设置对象)。