IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    有意思的lambda表达式

    潘魏增发表于 2008-05-25 16:28:05
    love 0

    有这么一种场景,有一个Category的实体类对应着一个名为Category的数据库表。Category类中有Name和Path属性,在新建或更新Category对象时要保证这两个属性在数据库中的值唯一。当然你可以在数据库中的Name和Path字段都建一个唯一索引,那如果不采用这种做法应该怎么做呢?最简单的方法就是写两个名为CheckNameValid和CheckPathValid的函数。很明显这种做法不易于维护,比如说当Category类增加其他需要保证唯一的属性或者属性名字需要改变的时候。我的写法如下:

    /// <summary>
    /// 检查数据库字段中的值是否唯一
    /// </summary>
    /// <param name="t">Cateroy的集合</param>
    /// <param name="func">表达式</param>
    /// <param name="isNew">是否是新建Category</param>
    /// <returns></returns>
    protected bool CheckValid(
                    IEnumerable<Category> t, 
                    Func<Category, Boolean> func, 
                    bool isNew)
    {
        Category c = t.SingleOrDefault(func);
        if (isNew)
            return c == null;
        else
            return c == null || (c != null && c.ID == Convert.ToInt32(ID.Text));
    }
    
    //调用方法一
    protected Func<Category, Boolean> func = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        func = selectCategory;
    }
    protected bool selectCategory(Category c)
    {
        return c.Name == this.Name.Text;
    }
    CheckValid(kr.Categories, selectCategory, true);
    //调用方法二
    CheckValid(kr.Categories, delegate(Category c){ 
            return c.Name == Name.Text;
        }
        , true);
    CheckValid(kr.Categories, delegate(Category c){ 
            return c.Path == Path.Text;
        }
        , false);
    //调用方法三
    CheckValid(kr.Categories, c => c.Name == Name.Text, true);
    CheckValid(kr.Categories, c => c.Path == Path.Text, false);

    三种调用方法,当然是使用了lambda表达式的方法三最简洁明了。还可以扩展一下,可以把它写成某个Utility类泛型静态方法,专门检查实体类在数据库字段中的值是否唯一,这时Convert.ToInt32(ID.Text)就要抽出来做成一个参数,这里不就写了。

    另外在瘦子师兄的点拨下,发现C# 3.0里面有个类似Javascript中prototype的概念,称为扩展方法(Extension Methods),在System.Linq中运用了大量的扩展方法(例如前面例子中的SingleOrDefault)。扩展方法貌似没prototype灵活,比较的例子如下:

    public static class Program 
    {
        public static void Main(string [] args)
        {
            string s = "Hello World";
            s.Print();
            s.ToString();
            Console.WriteLine(s.ToString());
            Console.Read();
        }
    }
    public static class Tools
    {
        public static void Print(this string s)
        {
            Console.WriteLine(s.ToUpper());
        }
        public static void ToString(this string s)
        {
            Console.WriteLine(s.ToUpper());
        }
    }
    function A(){this.name = "AA";}
    A.prototype = {
        show:function(){ alert(this.name);}
    }
    var a1 = new A();
    a1.show();
    //直接覆写A的prototype中的show
    A.prototype.show = function(){ alert("modified:"+this.name);}
    var a2 = new A();
    a2.name = "a2";
    a2.show();

    对于一个你不能修改源代码的类,可以通过这种方式为它添加方法。但这个只是编译期的(s.Print()翻译成tools.Print(s)),也就是说编译之后是不能通过反射来取得它的,它并不是要扩展类(例子中的String类)中的真正方法,而是某个静态类(例子中的Tools)的静态方法。

    具体介绍参见这篇文章Deep Dive on Extension Methods。



沪ICP备19023445号-2号
友情链接