Reputation: 2610
I am creating a core library for one of our internal project. This core library supposed to contain some abstract classes and generic classes which will then be extended by specialized projects. For example, There is an AbstractPerson class with standard properties and methods for a Person class while another project will implement a Person class which will inherit from the AbstractPerson class to add project specific functionality. Now we need to impelment DAL for this common and specialized projects.As most of operations are generic so i want to include them into core library as Repository classes. However, Repository classes need to access LINQ dataContext. Which is generated from the specialized databases. Hence there is no dataContext available in Core library to work. So how could i can create a common repository classes for generic methods which can reside in the common library.
Upvotes: 0
Views: 145
Reputation: 921
Can you rely on your DataContexts to implement a particular interface (even if only to say "These collections are present!")?
If so, by setting the context of the repository to a value passed in on the constructor, or allowing a context to be passed in each function call, you can inject the DataContext you want to use, as long as it implements the required interface.
public IEnumerable<Person> SearchPersons(IHasPersonsTable context, string searchFilter)
{
return IHasSomePersonsTable.Persons
.Where(p => p.SearchableThing.Contains(searchFilter));
}
It's a little prettier if you pass the context as a parameter to the repository constructor/factory method/whatever.
Upvotes: 1