views:

80

answers:

2

StructureMap newbie question.

public class SomeClass: IInterface1, IInterface2 {
}

I would like the following test to pass:

Assert.AreSameInstance(
    container.GetInstance<IInterface1>(), 
    container.GetInstance<IInterface2>());

How would I do an explicit registration of this?

I know in Castle Windsor I would do something like

kernel.Register(Component.For(typeof(IInterface1), typeof(IInterface2))
    .ImplementedBy(typeof(SomeClass));

But I don't see any equivalent API

A: 

Wouldn't you just tell it to instantiate them as a Singleton?

Jaxidian
Well...yes, but how to get the same singleton instance to implement each interface? To be clear, only one SomeClass instance should ever be instantiated.
George Mauer
Oh, so you create a "base class" that implements both interfaces. Then you specify for that base class to return a singleton. Then for both interfaces, you specify that it return that base class. That should do it. Sorry, no dev environment here and I'm far from a guru who can whip up the syntax for you. :-/
Jaxidian
Well yes, I understand the idea but the syntax is exactly what I cannot find. I tried x.For<IInterface1>().Singleton().User<SomeClass>();x.For<IInterface2>().Singleton().User<SomeClass>();<br />but that does not work
George Mauer
Correct syntax aside, I would expect something like this: `x.For<IInterface1>().Use<SomeClass>(); x.For<IInterface2>().Use<SomeClass>(); x.For<SomeClass>().Singleton().Use<SomeClass>();`. I know it seems a little redundant, but I believe that's how you'd do this.
Jaxidian
Sorry Jax, that's not it - just tried and it doesn't work. In any case, that would expose three services from the container when I would only to expose two. Either way, that doesn't work.
George Mauer
Sorry, that's the best I can do to help you. Voting your thread up, hoping you can get better help from somebody else. Sorry!
Jaxidian
No problem, thanks Jax
George Mauer
+4  A: 
ObjectFactory.Initialize(x => 
{ 
    x.For<IInterface1>().Singleton().Use<MyClass>(); 
    x.Forward<IInterface1, IInterface2>(); 
}); 
Phil Sandler
Awesome thanks Phil!
George Mauer