1

Have the following Interface

public interface IFoo<T> : IComparable<IPayRecordField<T>> where T : IComparable

I want auto fill all properties of IFoo with the implementation Foo. Using Structuremap

EXample

class SomeClass
{
   IFoo<string> MyFoo {get; set;} //this should be autofilled by structuremap with an instance of Foo<string>
}
Middy
  • 99
  • 1
  • 9

1 Answers1

1

That's actually rather simple. You can map an open genric interface to an open generic implementation just as you would register any other type:

container.Configure(r => r
    .For(typeof(IFoo<>))
    .Use(typeof(Foo<>)));

StructureMap will automatically resolve a Foo<SomeType> when you request a IFoo<SomeType>.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • Yep thats for a new instance. But how do I do a property injection into SomeClass So MyFoo is set automagically with Foo – Middy Aug 22 '13 at 12:16
  • See this answer: http://stackoverflow.com/questions/4979453/structuremap-beginner-property-injection – Steven Aug 22 '13 at 12:35
  • I cant see how that works for generics? – Middy Aug 22 '13 at 12:49
  • There are `FillAllPropertiesOfType` and `Setter` methods to allow property injection on either a single type (Setter) or all types (FillAllPropertiesOfType), but unfortunately there are only generic overloads available. So the only thing you can do is mark the `MyFoo` property with an `[SetterProperty]` attribute. Not pretty, but it will do the trick. – Steven Aug 22 '13 at 13:00