I am designing a Data Information Interface Layer. I want the users of this layer to be not aware of the Data Source and still use the goodness of LINQ syntax. I want to use standard LINQ providers as implementation of this layer and want to avoid writing Custom LINQ provider
Consider following Example.
The Information Interface Layer is declared as following
interface IMyData
{
int Intdata { get; }
double DoubleData { get; }
}
interface IMyDataProviderLayer
{
IQueryable< IMyData > Context { get; }
}
and in Client code that uses this layer
//dataProvider implements IMyDataProviderLayer
var dataCollection = from data in dataProvider.Context
where data.Intdata == 5
select data;
The interface implementation will access the data from real data source and needs to use the standard LINQ providers.
Is it possible to do something like above? Do I need to implement LINQ provider from scratch even the Data Source has standard LINQ implementations?
Or Is there a better way to achieve what I am trying to do here?
Thanks in advance.
I think you are looking for something like a Repository Pattern. You would have an interface as follows:
Implementation(use dependency injection here):
Use:
You may also use
IQueryableinstead ofIEnumerablein you repository. I explained some difference between the 2 here. But basically, the IQueryable has the SQL get generated and sent to the database while IEnumerable calls the database then does the query in memory.This way, you may change out the ORM from LINQ to SQL to entity framework or anything that can abstract the query to IQueryable or IEnumerable.