zoukankan      html  css  js  c++  java
  • LiteORM学习一:EntityObject 设计

    LiteORM项目地址:http://www.codeproject.com/KB/database/lite.aspx

    ListORM 怎么设计Entity呢?

    listorm 是利用微软自带的Attribute 来实现数据库与实体类的映射过程。

    image

    lmgorm是利用xml来实现数据库与实体类的映射过程。这个会在LmgORM系列会有详细的介绍的。

    image

    using lite;
    // maps to table dbo.person
    [Table]
    public class Person

    // maps to table dbo.users
    [Table(Name="users")]
    public class User

    // maps to table people.person
    [Table(Schema="people")]
    public class Person

    // maps to view people.transactView
    [Table(Name="transactView",Schema="people")]
    public class Purchase

    属性

    // maps to [order_id]
    [Column(Name="order_id")]
    private int orderId;

    // maps to [customer_id]
    [Column(Name="customer_id")] // 这个用的比较多哦
    public int CustomerId { get; set; }

    // maps to [quantity]
    [Column]
    public int Quantity { get; set; }

    主键

    [Column, ID, PK] //加入了 ID 属性 代表是自增长 列
    protected int studentNumber;

    [Column(Name
    = "UserID", Alias = "id"), PK] // 加入了 PK代码是主键
    private int _UserID;

    完整实体映射代码:

         [Table(Name = "User")]

        public class ListUser
        {
            [Column(Name 
    = "UserID", Alias = "id"),ID, PK]
            
    private int _UserID;

            [Column(Name 
    = "UserName")]
            
    private string _UserName;

            
    public int UserID
            {
                
    get { return _UserID; }
                
    set { _UserID = value; }
            }
            
            
    public string UserName
            {
                
    get { return _UserName; }
                
    set { _UserName = value; }
            }

            
    #region
            
    public override string ToString()
            {
                
    string info = "UserID= " + UserID.ToString() + "  UserName=" + UserName;
                
    return info;
            }
            
    public int Add()
            {
                IDb db 
    = DbFactory.Instance.GetDb();
                
    int records = db.Insert(this);
                
    return records;
            }
            
    public void Update()
            {
                IDb db 
    = DbFactory.Instance.GetDb();
                
    int records = db.Update(this);
            }
            
    public void Delete(string column)
            {
                IDb db 
    = DbFactory.Instance.GetDb();
                IQuery q 
    = db.Query();
                q.Constrain(column).Equal(
    1);
                db.Delete(
    this.GetType(), q);
            }
            
    public ListUser Find(string id)
            {
                IDb db 
    = DbFactory.Instance.GetDb();
                ListUser p2 
    = (ListUser)db.Find(this.GetType(), id);
                
    return p2;
            }
            
    public System.Collections.IList Query()
            {
    //查询
                IDb db = DbFactory.Instance.GetDb();
                IQuery q 
    = db.Query();
                System.Collections.IList list 
    = db.Select(this.GetType(), q);
                
    return list;
            }
            
    #endregion
        }

    内部实现

    TableAttribute  表映射

    ColumnAttribute 列映射

    PKAttribute 主键映射

    SPResultAttribute 存储过程或视图的映射

    不知道理解的对不对。以后在完善。

    作者:罗敏贵
    邮箱:minguiluo@163.com
    QQ群:34178394 建群 主要是寻找志同道合的人士一起学习和讨论自己的所学所思
    出处:http://luomingui.cnblogs.com/
    说明:专注于微软平台项目架构、熟悉设计模式、架构设计、敏捷个人和项目管理。现主要从事WinForm、ASP.NET、等方面的项目开发、架构、管理工作。文章为作者平时里的思考和练习,可能有不当之处,请博客园的园友们多提宝贵意见。
    知识共享许可协议本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。

  • 相关阅读:
    MVC中使用jquery的浏览器缓存问题
    3 工厂方法模式
    2 简单工厂模式
    1 单例模式
    Sqlserver With as
    Memcache的使用
    mysql的分区和分表
    MySQL主从复制与读写分离
    SqlServer 表分区
    SqlServer 2012 AlwaysOn
  • 原文地址:https://www.cnblogs.com/luomingui/p/2000810.html
Copyright © 2011-2022 走看看