Given:
class MyDao
{
public int SiteId {get;set;}
public Cv3AddressDao ReadSingle(Expression<Func<Cv3AddressDao, bool>> predicate)
{ //...
}
}
class MyEntity
{
public int SiteId {get;set;}
}
How can I take the predicate parameter of type Expression<Func<MyDao, bool>>
and convert it to Expression<Func<MyEntity, bool>>
?
The type of Answer I am looking for
Please note that answers must show how conversion works. I want an implementation similar to this but that works :)....
public MyDao ReadSingle(Expression<Func<MyDao , bool>> predicate)
{
var mappedPredicate = MapFun<MyDao , MyEntity>(predicate);
var result = repository.GetSingle<MyEntity>(mappedPredicate);
return Convert(result);//Converts Entity to Dao...safe to ignore this line
}
Expression<Func<B, bool>> MapFun<A, B>(Expression<Func<A, bool>> input)
{
Expression<Func<B, bool>> result = null;//How to convert?
return result;
}
Details regarding why I want this design...
This is an asp.net MVC 5 application. The reason this conversion is important is so my View is not aware of the type MyEntity
. In other words if I were to do:
myDaoObject.ReadSingle<MyEntity>(myEntity => myEntity.SiteId == "123");
Then my View layer has to reference my DLL layer because here I am using MyEntity
. I want the View layer to work with the Dao instead:
myDaoObject.ReadSingle<MyDao>(myDao=> myDao.SiteId == "123");
But, now I have to convert the Dao to make it useable with the Repository. The Repository is only aware of DL objects. I am trying to avoid creating a redundant Repository just to support this translation. The DAO contains all the properties of the Entity plus some.