Nullreference with "With" + custom Instance

0 views
Skip to first unread message

fquednau

unread,
Dec 14, 2008, 1:59:05 PM12/14/08
to structuremap-users
Hi Jeremy,
interesting posts here today - all have somehow to do with context,
schcontext and how to change instances based on that. Based on your
keyword "Instance" in "Is there a way to do context-driven selection
of registered instances?", I started experimenting. My first hope was
that one could get to an instance passed in with the "With" keyword,
hoping that I could get access to it in a custom instance.

public interface ICommand { }
public class CommandA : ICommand { }
public class Context { public string ID { get; set; } }
public class MyInstance : Instance { ... }

ObjectFactory.Initialize(
i => i.ForRequestedType<ICommand>().TheDefault.Is.Instance(new
MyInstance()));

var cmd = ObjectFactory.With(new Context() { ID =
"Hi" }).GetInstance<ICommand>();

I was surprised that this blew up with a null reference at
StructureMap.Graph.Constructor.Visit(IArgumentVisitor visitor)

More standard construction configuration does not lead to this issue.
It looks like in this specific scenario the "Constructor" is trying to
get the constructor of the *interface*, which is passed as
"Pluggedtype". _ctor is then left to be null, but is duly accessed
when wanting to iterate its parameters.
Maybe it is not a bug per se, but it seems unfair to your
"Constructor" to throw him into a situation of scanning an interface
type for its constructor :)

On a minor note, how do I get to the "With(obj)" obj from within a
custom instance, then? ;)

Jeremy Miller

unread,
Dec 19, 2008, 2:09:33 PM12/19/08
to structuremap-users
Ok, it took a week for me to get to this, but I reproduced the bug and
just committed a patch to the trunk.

Thanks for the heads up.

Jeremy
Reply all
Reply to author
Forward
0 new messages