Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Ok. Here's the scenario. You have a domain model in which all the entities inherit the IEntity interface. This would force your application to use the same name for ID throughout the application. This is fine for the application but since we cannot force the rest of the world in the same pattern. It would be cool to be able to query entities and be able to map to other types with the same query without worrying the backend implementation

Maybe some code example would be in order:

C#
namespace FuncTypeMapper
{
    public class User : IEntity
    {
        public int ID { get; set; }
    }
    public class DbUser
    {
        public int userId { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Trying to get a user with ID 3
            Func<User, bool> entityQuery = u => u.ID == 3;

            // However, the Linq type for user account don't contain property
            // ID for user, it has userId instead
            // so we need to convert the System.Func into DB compatible query
            Func<Func<User, bool>, Func<DbUser, bool>> queryConverter = f => f.????????????;
            // How to solve the above with C#???

            // Then we could just convert the query
            Func<DbUser, bool> dbQuery = queryConverter.Invoke(entityQuery);

            // Before we Invoke it against the database
            var usersFromDb = from u in dc.UserAccounts
                              where dbQuery.Invoke(u)
                              select u;
        }
    }
}


I do know of Entity Framework Code First pattern and I've been looking into other solutions also, so that is not what I am looking for. I am looking for using C# features and hand mapping between the types whether they'd be Linq to SQL classes, Web Service types or whatever that stores data in a class.
Posted
Comments
BillWoodruff 10-Oct-11 6:47am    
Fascinating code ! Since every dbUser is a User (?), I wonder why dbUser doesn't inherit from User. Another idea that comes to mind is using a Dictionary<dbuser,User> to store the mapping required. Take these ideas and reactions with 'a grain of salt' since (as may be obvious) I'm not that up-to-speed on this type of issue.

1 solution

Basically, it's not possible. ( It's not impossible either, just ridiculously difficult. Google "Expression Trees" if you want. )

Why don't you just pass the int id to your DAL method instead of a Func<User,bool>?
 
Share this answer
 
Comments
ConXioN 10-Oct-11 6:41am    
I do currently pass ID into the DAL. Just looking for alternatives. So, instead of having a repository with multiple methods for different scenarios, you could do queries against Entities like so: UserRepository.Get(u => u.ID == model.userId).Single() and alternatively UserRepository.Get(u => u.Username == model.userName).

Thanks for the Google hint, I found something that seems to fit the picture: http://vincentlauzon.wordpress.com/2011/06/25/expression-trees-part-4-simple-mapping-inversion/

At least something to look at.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900