本文介绍了StructureMap:选择具体类型嵌套依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 计算器:public interface ICalculator{ int Calculate(int a, int b);}public class Calculator : ICalculator{ private readonly ICalculatorStrategy _calculatorStrategy; public Calculator(ICalculatorStrategy calculatorStrategy) { _calculatorStrategy = calculatorStrategy; } public int Calculate(int a, int b) { return _calculatorStrategy.Calculate(a, b); }} 计算器stragies:Calculator stragies:public interface ICalculatorStrategy{ int Calculate(int a, int b);}public class AdditionCalculator : ICalculatorStrategy{ public int Calculate(int a, int b) { return a + b; }}public class MultiplyCalculator : ICalculatorStrategy{ public int Calculate(int a, int b) { return a * b; }} 计算器用法:Calculator usages:public class CalculatorUsageOne{ private readonly ICalculator _calculator; public CalculatorUsageOne(ICalculator calculator) { _calculator = calculator; } public void Process() { Console.WriteLine(_calculator.Calculate(6, 5)); }}public class CalculatorUsageTwo{ private readonly ICalculator _calculator; public CalculatorUsageTwo(ICalculator calculator) { _calculator = calculator; } public void Process() { Console.WriteLine(_calculator.Calculate(6, 5)); }} Structuremap注册地:Structuremap Registry:public class DependencyRegistry : Registry{ public DependencyRegistry() { For<ICalculatorStrategy>().Use<AdditionCalculator>().Named("Addition"); For<ICalculatorStrategy>().Use<MultiplyCalculator>().Named("Multiply"); For<ICalculator>().Use<Calculator.Calculator>(); }}有关CalculatorUsageOne我想补充的数字(使用 AdditionCalculator )。 。对于CalculatorUsageTwo我想乘的数字(使用 MultiplyCalculator )。For CalculatorUsageOne I want to add the numbers (use AdditionCalculator).For CalculatorUsageTwo I want to multiply the numbers (use MultiplyCalculator).我如何与StructureMap实现这一目标?How do I achieve this with StructureMap?推荐答案尝试这样的:For<CalculatorUsageOne>().Use<CalculatorUsageOne>() .Ctor<ICalculator>().Is<Calculator.Calculator>( x => x.Ctor<ICalculatorStrategy>().Is<AdditionCalculator>() );For<CalculatorUsageTwo>().Use<CalculatorUsageTwo>() .Ctor<ICalculator>().Is<Calculator.Calculator>( x => x.Ctor<ICalculatorStrategy>().Is<MultiplyCalculator>() );您可以窝深你的对象图配置,因为你需要。无论如何,我会考虑使用泛型这里表现出更明确的方式的依赖关系。You can nest your objects graph configurations as deep as you need. Anyway, I would think about using generics here to show the dependencies in more explicit way. 关于仿制药编辑:无论使用泛型这里是一个不错的主意取决于从您的方案。如果没有指定具体的依赖关系的 CalculatorUsages 故意的,这就是你的目标是有它的战略无关,你的解决方案似乎是最好的。Whether using generics is a good idea here depends from your scenario. If you didn't specified the concrete dependencies for your CalculatorUsages on purpose and that's your goal to have it strategy-agnostic, your solution seems to be the best.但如果你只是需要有计算器中的中间层的共同实现,你可以指定中的泛型参数计算器的依赖,使之明确。也许它不是最好的用例在这里,但它可以在某种程度上是这样的:But if you just need to have common implementation of Calculator in "middle layer", you can specify Calculator's dependency in generic parameter to make it explicit. Maybe it's not the best use case here, but it can go somehow like this:public class CalculatorUsageOne{ public CalculatorUsageOne(ICalculator<AdditionCalculator> calculator) { // ... }}public class Calculator<T> where T : ICalculatorStrategy{ public Calculator(T strategy) { // ... }}和这样注册它:For(typeof(ICalculator<>).Use(typeof(Calculator<>);这将告诉StructureMap通过任何通用的参数要求 ICalculator 到计算器(开放式泛型的),然后在实例构造函数中的战略目标。This will tell StructureMap to pass any generic parameter for requested ICalculator to Calculator (open generics), which then instantiates the strategy objects in constructor.另外,你可以使用标记接口代替仿制药,但再次,这一切从您的特定方案取决于它可能是从一开始就最适合。Alternatively, you can use marker interfaces instead of generics, but once again, it all depends from your particular scenario and it may be that the simplest solution from very beginning fits best. 这篇关于StructureMap:选择具体类型嵌套依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-06 01:18