Skip to content

Instantly share code, notes, and snippets.

@andreasohlund
Created September 29, 2011 17:36
Show Gist options
  • Select an option

  • Save andreasohlund/1251362 to your computer and use it in GitHub Desktop.

Select an option

Save andreasohlund/1251362 to your computer and use it in GitHub Desktop.
StructureMapBug
namespace NamedInstancesBug
{
using NUnit.Framework;
using StructureMap;
[TestFixture]
public class When_missing_named_instance_handler_is_invoked
{
[Test]
public void Should_pass_the_name_of_the_named_instance_to_the_context()
{
var container = new Container(x=>
{
x.For<ISomeThing>().MissingNamedInstanceIs.ConstructedBy(ctx =>
{
return new SomeThing(ctx.RequestedName);
});
x.ForConcreteType<ClassDependingOnISomething>()
.Configure.Ctor<ISomeThing>().Named("SomeThing");
});
var something = container.GetInstance<ISomeThing>("SomeName");
var other = container.GetInstance<ClassDependingOnISomething>();
Assert.AreEqual("SomeName",((SomeThing)something).Name);
Assert.AreEqual("SomeThing", other.NameOfSomething);
}
}
public class ClassDependingOnISomething
{
ISomeThing someThing;
public ClassDependingOnISomething(ISomeThing someThing)
{
this.someThing = someThing;
}
public string NameOfSomething
{
get { return ((SomeThing) someThing).Name; }
}
}
public class SomeThing : ISomeThing
{
public string Name { get; set; }
public SomeThing(string requestedName)
{
Name = requestedName;
}
}
public interface ISomeThing
{
}
}
@hkoppel
Copy link
Copy Markdown

hkoppel commented Feb 14, 2012

Same problem here - I have lots of similar registrations in the registries and I wanted to clean in up with similar implementation. RequestedName is always passed as "DEFAULT", so no cigar:(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment