我有一个接口IFileSystemStructureEvaluator有两个具体实现:NtfsFileSystemStructureEvaluator和FtpFileSystemStructureEvaluator。
我希望能够根据传递给构造函数的 Uri 是否是 FTP uri 的文件 uri来请求适当的IFileSystemStructureEvaluator 。
如何在 StructureMap 中实现这一点?
谢谢
我有一个接口IFileSystemStructureEvaluator有两个具体实现:NtfsFileSystemStructureEvaluator和FtpFileSystemStructureEvaluator。
我希望能够根据传递给构造函数的 Uri 是否是 FTP uri 的文件 uri来请求适当的IFileSystemStructureEvaluator 。
如何在 StructureMap 中实现这一点?
谢谢
You should check out the Conditional Object construction post by Jeremy Miller. It allows you to use some conditional checks in determining what you'll get as an instance. It sounds like a solution to your problem.
Edits below
There have been several questions on the StructureMap users list about doing conditional construction (i.e., return this object if this condition, else this other object). In order to meet this apparent need, StructureMap 2.5.2 introduces the new ConditionalInstance that allows a user to effectively switch the active Instance based on a Predicate boolean test. Here's a quick example of using the new Conditional() syntax of InstanceExpression:
var container = new Container(x =>
{
x.InstanceOf<Rule>().Is.Conditional(o =>
{
o.If(c => false).ThenIt.Is.OfConcreteType<ARule>();
o.If(c => true).ThenIt.IsThis(GREEN);
o.TheDefault.IsThis(RED);
}).WithName("conditional");
});
More available at the wayback machine https://web.archive.org/web/20090506031557/http://codebetter.com/blogs/jeremy.miller/archive/2009/01/18/conditional-object-construction-in-structuremap-i-e-fun-with-lambdas.aspx