C#数据中介——封装tPair和tPairList类

本文会封装两个类,即tPair和tPairList,用于在应用中传递数据。此外,从本文开始,自定义类型命名会以小写字母t开头,以提高封装代码的辨识度。

tPair类

tPair和tPairList类定义在cfx.data命名空间,首先来看tPair类的实现。tPair类用于处理单个数据项,包含数据名称和值,以及数据的转换等操作。下面的代码(cfx/data/tPair.cs)是tPair类的基本实现。

C#
using System;
namespace cfx.data
{
    public class tPair
    {
        public string Name { get; set; }
        public object Value { get; set; }
        //
        public tPair(string name="",object val=null)
        {
            Name = name;
            Value = val;
        }
        //
        public static tPair Create(string name = "", object val = null)
        {
            return new tPair(name, val);
        }
        //
        public int IntValue
        {
            get { return Obj.ToInt(Value); }
        }
        //
        public long LngValue
        {
            get { return Obj.ToLng(Value); }
        }
        //
        public float SngValue
        {
            get { return Obj.ToSng(Value); }
        }
        //
        public double DblValue
        {
            get { return Obj.ToDbl(Value); }
        }
        //
        public decimal DecValue
        {
            get { return Obj.ToDec(Value); }
        }
        //
        public string StrValue
        {
            get { return Obj.ToStr(Value); }
        }
        //
        public bool BoolValue
        {
            get { return Obj.ToBool(Value); }
        }
        //
        public DateTime DateValue
        {
            get { return Obj.ToDate(Value); }
        }
        //
    }
}

代码中,tPair类定义了两个属性,Name属性为数据名称,Value属性为数据的值。构造函数定义了两个可选参数,Create()静态方法同样定义了两个可选参数,用于返回tPair对象。接下来是一系列的属性,可以获取Value属性转换为不同类型的数据,操作使用了Obj类定义的一系列转换方法,下面的代码(cfx/Obj.cs)是Obj类的实现。

C#
using System;

namespace cfx
{
    public static class Obj
    {
        // 转换为int类型,默认值为0
        public static int ToInt(object obj, int defVal = 0)
        {
            int result;
            if (obj != null && int.TryParse(obj.ToString(), out result))
                return result;
            else
                return defVal;
        }
        // 转换为long类型,默认值为0
        public static long ToLng(object obj, long defVal = 0L)
        {
            long result;
            if (obj != null && long.TryParse(obj.ToString(), out result))
                return result;
            else
                return defVal;
        }
        //
        public static float ToSng(object obj,float defVal = 0F)
        {
            float result;
            if (obj != null && float.TryParse(obj.ToString(), out result))
                return result;
            else
                return defVal;
        }
        //
        public static double ToDbl(object obj, double defVal = 0D)
        {
            double result;
            if (obj != null && double.TryParse(obj.ToString(), out result))
                return result;
            else
                return defVal;
        }
        //
        public static decimal ToDec(object obj, decimal defVal = 0M)
        {
            decimal result;
            if (obj != null && decimal.TryParse(obj.ToString(), out result))
                return result;
            else
                return defVal;
        }
        //
        public static string ToStr(object obj, string defVal = "")
        {
            if (obj != null)
                return obj.ToString();
            else
                return defVal;
        }
        //
        public static bool ToBool(object obj)
        {
            try { return Convert.ToBoolean(obj); }
            catch { return false; }
        }
        //
        public static DateTime ToDate(object obj)
        {
            try { return Convert.ToDateTime(obj); }
            catch { return DateTime.MinValue; }
        }
        //
    }
}

Obj类中定义的转换方法会尽可能不产生抛出错误,并返回一个可用的数据,实际工作中,判断数据的有效性需要额外的代码。

下面的代码,在Program.cs文件中测试tPair类的使用。

C#
using System;
using cfx.data;

namespace csfx_demo
{
    class Program
    {
        static void Main(string[] args)
        {
            tPair p = tPair.Create("age", 25);
            Console.WriteLine(p.Name);  // age
            Console.WriteLine(p.Value);  // 25
            Console.WriteLine(p.IntValue);  // 25
        }
    }
}

tPairList类

tPairList类用于处理tPair数据项的集合,其定义如下面的代码(cfx/data/tPairList.cs)。

C#
using System.Collections.Generic;

namespace cfx.data
{
    public class tPairList : List<tPair>
    {
        public static tPairList Create()
        {
            return new tPairList();
        }
        //
        public tPairList Add(string name, object val)
        {
            Add(tPair.Create(name, val));
            return this;
        }
        //
        public int Find(string name)
        {
            for (int i = 0; i < Count; i++)
            {
                if (string.Compare(this[i].Name, name, true) == 0)
                    return i;
            }
            return -1;
        }
        //
        public tPair Get(string name)
        {
            for (int i = 0; i < Count; i++)
            {
                if (string.Compare(this[i].Name, name, true) == 0)
                    return this[i];
            }
            return tPair.Create();
        }
        //
    }
}

tPairList类继承于List<tPair>,意味着它可以支持List泛型类的操作,代码中增加了几个方法,分别是:

  • Create()静态方法,生成tPairList对象。
  • Add(string name, object val)方法,根据数据名称和值添加数据项,方法返回当前实例,可以使用链接方法连续调用。
  • Find(string name)方法,根据数据名称查找数据项,并返回对应的索引值,没有找到返回-1。请注意,比较数据名称时不区分字母大小写。
  • Get(string name)方法,根据数据名称返回数据项的tPair对象,比较数据名称时同样不区分字母大小写。

下面的代码,在Program.cs文件中测试tPairList类的使用。

C#
using System;
using cfx.data;

namespace csfx_demo
{
    class Program
    {
        static void Main(string[] args)
        {
            tPairList pl = tPairList.Create()
                .Add("name","Tom").Add("age",25);
            for(int i = 0; i < pl.Count; i++)
            {
                Console.WriteLine(pl[i].Name + " : " + pl[i].Value);
            }
        }
    }
}

代码执行结果如下图所示。

测试tPairList

tPair和tPairList包含了数据项和数据集合的基本操作,并集成了数据类型转换功能,可以简化数据传递和转换操作,在后续文章中会看到更多应用示例,并可以根据需要添加功能。