I'll need to expose a bit of background before going further: I have a project that uses nHibernate and some generic repository to forward predicates and return entities as follows:
public abstract class GenericRepository< T >
{
...
public virtual T Single( Expression< Func< T, bool > > predicates )
{
// Get object from nHibernate session object
var retObj = Session
.Query< T >()
.Where( predicates ).SingleOrDefault();
return retObj;
}
...
}
I can then for example get an entity this way:
var entity = Context.MyGenericEntityRepository.Single( e => e.Id == id );
// or
var entity = Context.MyGenericEntityRepository.Single( e => e.Name == name );
But, I also have some entities that because of the nature of the project aren't saved to the database but to the file system (bunch of files). So I use a derived repository that uses some sort of DataAccess class to get the entities from the file system as follows:
public class NotGenericRepository
{
...
// for example
public IList<Entity> All()
{
return _entityDataAccess.All();
}
...
}
As said, the 2nd type of entity isn't stored in the database, however, to facilitate my journey I created a sort of in-memory database system using DataSets and DataTables. So when the solution is first started, I have a singleton called CustomDatabase that gets initialised, creates the DataTables in memory, adds relations between the DataTalbes, and adds them to a general DataSet before scanning the file system to populate the tables.
Using this, I can now query my DataTables instead of scanning through the file system tree every time. I then set up some events in my CustomDatabase so whenever a row is added/deleted/updated, the changes are reflected onto the file system.
So... This is it for the background, sorry for the length...
My question is now fairly simple, I'm looking for a way to somehow translate the lambda expression forwarded by the repository to my DataAccess class, so I can then analyse it and select from my DataTables accordingly...
For example, a call to the repository such as:
var entity = Context.MyNotGenericEntityRepository.Single( e => e.Id == id );
// or
var entity = Context.MyNotGenericEntityRepository.Single( e => e.Name == name );
Should be translated within the DataAccess as:
DataRow entityRow = CustomDatabase.Tables[ "EntityName" ].AsEnumerable().Where( e => e.Field< Guid >( "Id" ) == id);
// or
DataRow entityRow = CustomDatabase.Tables[ "EntityName" ].AsEnumerable().Where( e => e.Field< string >( "Name" ) == name);
Or:
var entity = from myRow in CustomDatabase.Tables[ "EntityName" ].AsEnumerable()
where myRow.Field<Guid>( "Id" ) == id
select myRow;
// or
var entity = from myRow in CustomDatabase.Tables[ "EntityName" ].AsEnumerable()
where myRow.Field<string>( "Name" ) == name
select myRow;
I have absolutely no idea how to do this, I've been looking all over the net, but the problem is that I don't really know how to name this problem so I haven't found much so far... :(
Any help is appreciated since I'm expecting there'll be several ways to tackle this problem :)
Thanks!!