posts - 84, comments - 289, trackbacks - 10, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

我想组建新的Entity框架

Posted on 2011-10-01 17:38 faib 阅读(1740) 评论(5) 编辑 收藏

    这个想法已经有很长一段时间了,并且目前已经有一个雏形的版本了,我暂定它为Fireasy.Data.Entity。

    我先说一说我的想法,实体的映射将采用依赖属性的方式进行定义,这样可以避免使用反射进行实体的初始化,而且也比较实现其他代码的切入。

    在这个框架里,还是提供了引用实体和实体集的概念,它们也可以通过lazy加载进来,另外,还设计一个支持枚举的属性和一个同步属性。

    另外,根据实际项目的需要,还会将同一个实体根据不同的规则映射多个不同的数据表,以提供数据分布式和隔离式存储。还会提供一个树结构的映射及相应的持久化类,以达到快速应用。

    实体的继承特性将在下一期进行考虑。

 

    以下是实体类的代码示例:

 

    [EntityMapping("PRODUCTS")]
    public class Product : EntityObject
    {
        public readonly static IProperty _ProductId = PropertyUnity.RegisterProperty("ProductId"
            typeof(int), typeof(Product), 
            new PropertyMapInfo
            {
                FieldName = "PRODUCTID",
                GenerateType = IdentityGenerateType.AutoIncrement,
                IsPrimaryKey = true,
                IsNullable = false
            });

        public readonly static IProperty _ProductName = PropertyUnity.RegisterProperty("ProductName"
            typeof(string), typeof(Product), 
            new PropertyMapInfo
            {
                FieldName = "PRODUCTNAME",
                IsNullable = false
            });

        public readonly static IProperty _SupplierID = PropertyUnity.RegisterProperty("SupplierID"
            typeof(int?), typeof(Product), 
            new PropertyMapInfo { FieldName = "SUPPLIERID" });

        public readonly static IProperty _CategoryID = PropertyUnity.RegisterProperty("CategoryID"
            typeof(int?), typeof(Product), 
            new PropertyMapInfo { FieldName = "CATEGORYID" });

        public readonly static IProperty _QuantityPerUnit = PropertyUnity.RegisterProperty("QuantityPerUnit"
            typeof(string), typeof(Product), 
            new PropertyMapInfo { FieldName = "QUANTITYPERUNIT" });

        public readonly static IProperty _UnitPrice = PropertyUnity.RegisterProperty("UnitPrice"
            typeof(decimal?), typeof(Product), 
            new PropertyMapInfo { FieldName = "UNITPRICE" });

        public readonly static IProperty _UnitsInStock = PropertyUnity.RegisterProperty("UnitsInStock"
            typeof(short?), typeof(Product), 
            new PropertyMapInfo { FieldName = "UNITSINSTOCK" });

        public readonly static IProperty _UnitsOnOrder = PropertyUnity.RegisterProperty("UnitsOnOrder"
            typeof(short?), typeof(Product), 
            new PropertyMapInfo { FieldName = "REORDERLEVEL" });

        public readonly static IProperty _ReorderLevel = PropertyUnity.RegisterProperty("ReorderLevel"
            typeof(short?), typeof(Product), 
            new PropertyMapInfo { FieldName = "REORDERLEVEL" });

        public readonly static IProperty _Discontinued = PropertyUnity.RegisterProperty("Discontinued"
            typeof(bool?), typeof(Product),
            new PropertyMapInfo { FieldName = "DISCONTINUED", DefaultValue = true });

        public readonly static IProperty _OrderDetails = PropertyUnity.RegisterSpecialProperty("OrderDetails"
            typeof(EntitySet<OrderDetails>), typeof(Product));

        public readonly static IProperty _DelFlag = PropertyUnity.RegisterProperty("DelFlag"
            typeof(bool?), typeof(Product), new PropertyMapInfo { FieldName = "DEL_FLAG", IsFakeDeleteFlag = true });

        public int ProductId
        {
            get { return (int)GetValue(_ProductId); }
            set { SetValue(_ProductId, value); }
        }

        public string ProductName
        {
            get { return (string)GetValue(_ProductName); }
            set { SetValue(_ProductName, value); }
        }

        public int? SupplierID
        {
            get { return (int)GetValue(_SupplierID); }
            set { SetValue(_SupplierID, value); }
        }

        public int? CategoryID
        {
            get { return (int)GetValue(_CategoryID); }
            set { SetValue(_CategoryID, value); }
        }

        public string QuantityPerUnit
        {
            get { return (string)GetValue(_QuantityPerUnit); }
            set { SetValue(_QuantityPerUnit, value); }
        }

        public decimal? UnitPrice
        {
            get { return (decimal)GetValue(_UnitPrice); }
            set { SetValue(_UnitPrice, value); }
        }

        public short? UnitsInStock
        {
            get { return (Int16)GetValue(_UnitsInStock); }
            set { SetValue(_UnitsInStock, value); }
        }

        public short? UnitsOnOrder
        {
            get { return (Int16)GetValue(_UnitsOnOrder); }
            set { SetValue(_UnitsOnOrder, value); }
        }

        public short? ReorderLevel
        {
            get { return (Int16)GetValue(_ReorderLevel); }
            set { SetValue(_ReorderLevel, value); }
        }

        public bool? Discontinued
        {
            get { return (bool)GetValue(_Discontinued); }
            set { SetValue(_Discontinued, value); }
        }

        public EntitySet<OrderDetails> OrderDetails
        {
            get { return (EntitySet<OrderDetails>)GetValue(_OrderDetails); }
            set { SetValue(_OrderDetails, value); }
        }

        public bool? DelFlag
        {
            get { return (bool)GetValue(_DelFlag); }
            set { SetValue(_DelFlag, value); }
        }

    }

 

与EF相似地,实体间的关系采用Assembly特性进行定义:

 

[assembly: Relationship("Product:OrderDetails"typeof(Product), typeof(OrderDetails), "ProductId=>ProductId")]
[assembly: Relationship("Orders:OrderDetails"typeof(Orders), typeof(OrderDetails), "OrderID=>OrderId")]
[assembly: Relationship("Customer:Orders"typeof(Customers), typeof(Orders), "CustomerID=>CustomerID")]

 

    框架也提供对Linq查询的支持,目前支持mssql、oracle、mysql和sqlite几种数据库。

 

    目前的测试结果显示,数据加载的速度比EF稍稍快了一点点,现在需要请大虾帮忙验证这种方式的可行性。也请有兴趣的朋友一起加入讨论,多给我提些意见。QQ群号:6406277。

标签: 框架, orm, entity

Feedback

#1楼  回复 引用 查看   

2011-10-01 23:45 by 诺贝尔      
你和那个pdf.net有什么不同?

#2楼[楼主]  回复 引用 查看   

2011-10-02 00:28 by faib      
pdf.net之前没有关注过,刚刚我查看后,发现我的映射方式与他的实现差不多,只是我的是使用静态属性,我的属性信息是保存在缓存中,而他的则是保存在每一个实例中(字典中)。

#3楼  回复 引用 查看   

2011-10-02 10:14 by willieQ      
和几年前的nbear有什么区别呢?

#4楼  回复 引用 查看   

2011-10-04 17:47 by 瓜虫      
我到现在都一直在庆幸自己没去上大学,而且我觉得高考是一定要改革的。我将继续不遗余力的说高考和大学的坏话。我很早前就说过,现如今的大学像妓女一样,只要有钱,全国所有大学都乖乖排成一排随便你点,想上哪个上哪个,愿意多花点钱甚至可以几个一起上。

链接于http://www.jufeng-game.com

#5楼  回复 引用 查看   

2011-10-07 09:46 by 深山老林      
请问您想构建一个新的Entity框架?新框架有什么好处,能解决什么问题?