Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The trick is to make sure every single profile as at least a rule defined in it. If you don't specify a rule (configA) it won't create/see the profile.</p> <p>Given these classes:</p> <pre><code>public interface IFoo { string SayHello(); } public class Foo : IFoo { public string SayHello() { return "Hello"; } } public class SpecialFoo : IFoo { public string SayHello() { return "Hello Special"; } } public interface IBar { } public class Bar : IBar { } public interface IDummy { } public class Dummy : IDummy{ } </code></pre> <p>You can define this registry:</p> <pre><code>public class MyRegistry : Registry { protected override void configure() { ForRequestedType&lt;IBar&gt;().TheDefault.Is.OfConcreteType&lt;Bar&gt;(); ForRequestedType&lt;IFoo&gt;().TheDefault.Is.OfConcreteType&lt;Foo&gt;(); CreateProfileNotEmpty("configA"); CreateProfileNotEmpty("configB") .For&lt;IFoo&gt;().UseConcreteType&lt;SpecialFoo&gt;(); } StructureMap.Configuration.DSL.Expressions.ProfileExpression CreateProfileNotEmpty(string profile) { return CreateProfile(profile) .For&lt;IDummy&gt;().UseConcreteType&lt;Dummy&gt;(); } } </code></pre> <p>And it will work with these tests:</p> <pre><code>[TestMethod] public void TestMethod1() { var container = new Container(new MyRegistry()); Assert.IsNotNull(container.GetInstance&lt;IBar&gt;()); Assert.AreEqual("Hello", container.GetInstance&lt;IFoo&gt;().SayHello()); container.SetDefaultsToProfile("configB"); Assert.IsNotNull(container.GetInstance&lt;IBar&gt;()); Assert.AreEqual("Hello Special", container.GetInstance&lt;IFoo&gt;().SayHello()); container.SetDefaultsToProfile("configA"); Assert.IsNotNull(container.GetInstance&lt;IBar&gt;()); Assert.AreEqual("Hello", container.GetInstance&lt;IFoo&gt;().SayHello()); } </code></pre> <p>if you replace CreateProfileNotEmpty with simple CreateProfile, it will fail on the line that sets the default to configA.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload