Reputation: 1045
I'm implementing repository pattern following an example on the internet in its repository its written:
private Northwind db = null;
private DbSet<T> table = null;
public Repository()
{
this.db = new Northwind();
table = db.Set<T>();
}
public Repository(Northwind db)
{
this.db = db;
table = db.Set<T>();
}
Why two constructors? And whats the difference? When I instantiate it, I have two options, but I don't know what exactly it does
Upvotes: 1
Views: 284
Reputation: 1878
You could instantiate a new context in the repository, you are able to create multiple repositories in one controller.
Upvotes: 0
Reputation: 120
The first constructor implements a default constructor, it simply generates a new Northwind object.
The second constructor takes a Northwind object as a parameter, and allows you pass a previously created Northwind object to the Repository constructor.
If you were to do this ...
Northwind db = new Northwind();
Repository repo = new Repository(db);
It would be identical to calling ...
Repository repo = new Repository();
You would generally use the second constructor when you had already created and populated, or done something with the Northwind object you had created (difficult to know without knowing the implementation of Northwind.)
Upvotes: 1