Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define Default constructor Structuremap in a Generic Repository

I have a generic IRepository that has 2 constructors, one have none parameters, other has the datacontext as parameter. I want to define to structuremap to aways in this case use the parameterless constructor. I want a way to create a parameterless contructor, other solutions that I have seen, they create a new Datacontext and pass it to the constructor that has parameters.

like image 722
Ricky Avatar asked May 03 '10 15:05

Ricky


1 Answers

By default, StructureMap will use the constructor with the most arguments. In your case, since you want it to use the parameterless constructor, use the DefaultConstructorAttribute:

[DefaultConstructor]
public void Repository<T>() { }

public void Repository<T>(DataContext dataContext) { } 
like image 56
Michael Hedgpeth Avatar answered Nov 02 '22 13:11

Michael Hedgpeth