c# - Convert LINQ expressions between model and DTO for use in repositories and DAO -


i'm trying abstract out data access layer , repository/model layers in application.

so far, have architecture such have repositories returning , accepting model classes.

example: vehicle model managed vehiclerepository repo.

now, abstract away database layer specific types actual models, have implemented dao objects, implement dto.

example: vehicledao accept , return vehicledto objects (which has different properties vehicle model account database specific types). job of vehiclerepository convert vehicle model vehicledto , again.

the question have when want send through linq expression. repository accept expression based on vehicle model class:

public async task<ilist<vehicle>> getall(expression<func<vehicle, bool>> condition) {     // return results     // _dao type vehicledao     return await _dao.find<t>(condition); } 

my dao object has similar method, accepts , returns vehicledto object, , works directly on database collection.

public async task<ilist<vehicledto>> getall(expression<func<vehicledto, bool>> condition) {     // return results     // _collection database managed collection (mongodb in case)     return await _collection.find<t>(condition).tolistasync(); } 

obviously, i'm getting build error because linq expressions not compatible between vehicle , vehicledto objects...

so, i'm wondering best way tackle problem is? should move conversion of model > dto dao object? should not use expressions querying data mongodb collection, , use concrete functions such getbyname, getbymake etc, instead of having ability specify query in method.

my ultimate goal have model/repo layer isolated dao layer. 1) testing purposes obviously, 2) if need move mongodb else down track, need rewrite/test data access layer.

any problem awesome!

if trying abstract layers must use abstractions.

base functions on interfaces instead of concrete types first thing comes mind:

public interface ivehicledomain  : idomain   //you extend base interface if wanted. {    public string field1 { get; set; } } 

then implement interface amongst domain , dto classes. use interface on both architecure layers, interfaces should exist in common assembly both dto , domain classes can access.

public async task<ilist<vehicle>> getall(expression<func<vehicle, bool>>   condition) {    // return results    // _dao type vehicledao    return await _dao.find<t>(condition); } public async task<ilist<ivehicle>> getall(expression<func<ivehicle, bool>> condition) {    // return results    // _collection database managed collection (mongodb in case)    return await _collection.find<t>(condition).tolistasync(); } 

Comments