System.Func转换器挑战 [英] System.Func Converter Challenge

查看:81
本文介绍了System.Func转换器挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.这是场景.您具有一个域模型,其中所有实体都继承了IEntity接口.这将迫使您的应用程序在整个应用程序中对ID使用相同的名称.这对于应用程序来说很好,但是由于我们不能以相同的模式强制世界其他地区.能够查询实体并能够使用相同的查询映射到其他类型,而不必担心后端实现会很酷.

也许某些代码示例按顺序排列:

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:

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;
        }
    }
}



我确实知道Entity Framework Code First模式,我也一直在寻找其他解决方案,所以这不是我想要的.我正在寻找使用C#功能并在类型之间进行手动映射,无论它们是Linq到SQL类,Web Service类型还是将数据存储在类中的任何类型.



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.

推荐答案

基本上,这是不可能的. (这也不是没有可能,只是非常荒谬.如果需要,可以使用Google表达式树".)

为什么不直接将int id传递给DAL方法而不是Func<User,bool>?
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>?


这篇关于System.Func转换器挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆