博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

我想组建新的Entity框架

Posted on 2011-10-01 17:38  faib  阅读(2529)  评论(6编辑  收藏  举报

    这个想法已经有很长一段时间了,并且目前已经有一个雏形的版本了,我暂定它为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。