大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C#技巧 > 一个ORM的实现(附源代码)

一个ORM的实现(附源代码)(2)

关键词:源代码ORM  阅读(1123) 赞(15)

[摘要]本文是对一个ORM的实现(附源代码)的讲解,对学习C#编程技术有所帮助,与大家分享。

3   示例

static void Main(string[] args)
{
    TestORM();
    TestPaging();

    Console.ReadLine();
}

static void TestORM()
{
    UserDao dao = new UserDao();
    try
    {
        dao.BeginTransaction();
        // add model
        User mUser1 = new User();
        mUser1.ID = "1";
        mUser1.UserName = "Mike1";
        mUser1.UserPwd = "1234";
        mUser1.CreateBy = "System";
        mUser1.CreateDate = DateTime.Now;
        dao.Add(mUser1);

        Console.WriteLine("Create Model Successfully.");
        string cmdText = "SELECT * FROM TUser WHERE ID='1' ORDER BY ID;";
        List<User> list0 = dao.GetListBySQL(cmdText);
        foreach (User model in list0)
        {
            Console.WriteLine(model.UserName);
        }
        Console.WriteLine("Get List Successfully.");

        //嵌套事务
        UserDao dao2 = new UserDao();
        try
        {
            dao2.BeginTransaction();
            //update model
            User mUser2 = new User();
            mUser2.ID = "1";
            mUser2.UserName = "Mike.Jiang";
            mUser2.UserPwd = null;
            mUser2.CreateDate = null;
            dao.Update(mUser2);
            Console.WriteLine("Update Model Successfully.");

            User mUser3 = dao.GetModel("1");
            if (mUser3 != null)
                Console.WriteLine(mUser3.UserName);

            Console.WriteLine("Get Model Successfully.");
            dao2.CommitTransaction();
        }
        catch
        {
            dao2.RollbackTransaction();
        }

        dao.Delete("1");

        Console.WriteLine("Delete Model Successfully.");
        dao.CommitTransaction();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        dao.RollbackTransaction();
    }
}

static void TestPaging()
{
    UserDao dao = new UserDao();
    // add 100 user
    for (int i = 1; i <= 100; i++)
    {
        User mUser1 = new User();
        mUser1.ID = i.ToString();
        mUser1.UserName = "Mike" + i.ToString();
        mUser1.UserPwd = "1234";
        mUser1.CreateBy = "System";
        mUser1.CreateDate = DateTime.Now;
        dao.Add(mUser1);
    }

    string cmdText = string.Format("SELECT * FROM TUser");
    DataTable table = DBHelper.ExecutePagingDataTable(CommandType.Text, cmdText, 1, 10, "ORDER BY ID");

    foreach (DataRow row in table.Rows)
    {
        Console.WriteLine(row["ID"]);
    }

    Console.WriteLine("Get Datatabe paging successfuly.");

    List<User> list = dao.GetListPaging(cmdText, 2, 10, "ORDER BY ID");
    foreach (User m in list)
    {
        Console.WriteLine(m.ID);
    }

    Console.WriteLine("Get Model list paging successfully.");


    cmdText = "DELETE FROM TUser";
    dao.ExecuteNonQuery(CommandType.Text, cmdText);
}

4  总结

这个版本的ORM示例,示例是没有认真去写了,因为不认为会有人用。但是这个ORM的代码是我认为比较简单的一个版本,只具有ORM的核心功能,几个类,只要具有c#基础的人都能看懂,有兴趣的可以看下。后续,自己会加上分页的Pager相关的内容、数据同步、数据验证的功能和和根据SQL自动生成实体对象的功能。

所有的源代码: BaseProject.7z

«上一页12下一页»


相关评论