Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get instance conditionally in StructureMap

I have an interface IFileSystemStructureEvaluator with two concrete implementations: NtfsFileSystemStructureEvaluator and FtpFileSystemStructureEvaluator.

I want to be able to request the appropriate IFileSystemStructureEvaluator depending on whether the Uri that is passed to the constructor is a file uri of an FTP uri.

How can I achieve this in StructureMap?

Thanks

like image 466
Arnold Zokas Avatar asked Aug 11 '09 21:08

Arnold Zokas


1 Answers

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.

http://codebetter.com/blogs/jeremy.miller/archive/2009/01/18/conditional-object-construction-in-structuremap-i-e-fun-with-lambdas.aspx

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

like image 167
Chris Missal Avatar answered Oct 15 '22 08:10

Chris Missal