Structure Map - I do not want to use the most greedy constructor!

I am trying to configure the NCommon NHRepository in my project using the Outline map. How can I stop him from choosing a greedy constructor?

public class NHRepository<TEntity> : RepositoryBase<TEntity> { public NHRepository () {} public NHRepository(ISession session) { _privateSession = session; } ... } 

Configuration of my structure configuration

 ForRequestedType(typeof (IRepository<>)) .TheDefaultIsConcreteType(typeof(NHRepository<>)) 

Greetings Jake

+7
structuremap
source share
2 answers

You can set the [DefaultConstructor] attribute for the constructor you want by default. In your case, setting it in the NHRepository() constructor will force it to initialize the default constructor for StructureMap.

Update: well, in the latest version of StructureMap using .NET 3.5, you can also specify it using the SelectConstructor method:

 var container = new Container(x => { x.SelectConstructor<NHRepository>(()=>new NHRepository()); }); 

Finally, I’m sure that you can define it in the StructureMap XML configuration, but I haven’t used it. You can do a little search on it. For more information on the above method, see: http://structuremap.sourceforge.net/ConstructorAndSetterInjection.htm#section3

+8
source share

So +1 for Razzie, because it would work if the NHRepository is in my own build, instead I decided to wrap the NHRepository with my own repository, as shown below.

 public class Repository<T> : NHRepository<T> { [DefaultConstructor] public Repository() { } public Repository(ISession session) { } } ForRequestedType(typeof (IRepository<>)) .TheDefaultIsConcreteType(typeof (Repository<>)); 
+1
source share

All Articles