【知识积累】Entity Framework学习:POCO延迟加载

背景:

之前在公司做一个石化项目的时候,用到了POCO。以往都是直接用Entity, 通过Mapping映射进行Entity到POCO的转换,转换的POCO实体支持延迟加载,它主要是配合code first使用(Code first则是指我们先定义POCO这样的实体class,然后生成数据库),现在也可以使用 EF Power tools将已经存在的数据库反向生成poco的class。

是什么:

POCO即Plain Old CLR Object 。Entity Framework4.0为实体提供了简单传统的CLR对象支持。实体对象可以独立于EF存在,由此EF更好地支持了测试驱动开发和领域驱动设计。同时,EF仍旧可以帮助跟踪POCO实体的变化,允许延迟加载,也回自动修正对导航属性和外键的改动。

项目实践:

基本实现如下图,相信大家会很有共鸣:

【知识积累】Entity Framework学习:POCO延迟加载_第1张图片

首先是在数据库中执行sql语句进行建表。

<span style="font-family:FangSong_GB2312;font-size:18px;"><strong>-- Create table
create table T_USER
(
  userid      NVARCHAR2(10) not null,
  username    NVARCHAR2(50) not null,
  password    NVARCHAR2(10) not null,
  contact_tel NUMBER,
  email       NVARCHAR2(50),
  create_date TIMESTAMP(6)
)
tablespace SYSTEM
  pctfree 10
  pctused 40
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the columns 
comment on column T_USER.userid
  is '用户代码';
comment on column T_USER.username
  is '用户名称';
comment on column T_USER.password
  is '用户密码';
comment on column T_USER.contact_tel
  is '联系电话';
comment on column T_USER.email
  is '邮箱';
comment on column T_USER.create_date
  is '创建日期';
-- Create/Recreate primary, unique and foreign key constraints 
alter table T_USER
  add primary key (USERID)
  using index 
  tablespace SYSTEM
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );</strong></span>

在项目中新建相应的Entity类

<span style="font-family:FangSong_GB2312;font-size:18px;"><strong> public class ApprovalProcess:BaseEntity
    {
        /// <summary>
        /// ID
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 节点名称
        /// </summary>

        public string NodeName { get; set; }

        /// <summary>
        /// 计划类型
        /// </summary>

        public string PlanType { get; set; }

        /// <summary>
        /// 状态ID
        /// </summary>

        public int StateID { get; set; }

        /// <summary>
        /// 类型ID
        /// </summary>

        public int TypeID { get; set; }
    }</strong></span>

新建相应的POCO类

<span style="font-family:FangSong_GB2312;font-size:18px;"><strong> /// <summary>
    /// 审批过程表
    /// </summary>
    [Class(Table = "t_dps_approvalprocess", OptimisticLock = OptimisticLockMode.Version, DynamicUpdate = false)]
    [Cache(Usage = CacheUsage.ReadWrite)]
    [Serializable]
    public class ApprovalProcess
    {
        [Id(0, TypeType = typeof(int), Name = "ID", UnsavedValue = "0")]
        [Column(1, Name = "id", NotNull = true, SqlType = "number")]
        [Generator(2, Class = "sequence")]
        [Param(3, Name = "sequence", Content = "s_dps_approvalprocess")]
        public virtual int ID { get; set; }

        /// <summary>
        /// 节点名称
        /// </summary>
        [Property(Column = "nodename")]
        public virtual string NodeName { get; set; }
        /// <summary>
        /// 计划类型
        /// </summary>
        [Property(Column = "plantype")]
        public virtual string PlanType { get; set; }
        /// <summary>
        /// 状态ID
        /// </summary>
        [Property(Column = "stateid")]
        public virtual int StateID { get; set; }
        /// <summary>
        /// 种类ID
        /// </summary>
        [Property(Column = "typeid")]
        public virtual int TypeID { get; set; }
    }</strong></span>
业务逻辑层方法编写(以根据审批人姓名和过程ID查询审批人信息为例)

<span style="font-family:FangSong_GB2312;font-size:18px;"><strong>  public ResultPage<Entity.ApprovalUser> QueryApprovalUserByApprovUserNameAndProcessID(Entity.ApprovalUser param, string approvalusername,int processid)
        {
            try
            {
                var page = new Page<POCO.ApprovalUser>();
                page.PageCurrent = param.PageIndex;
                page.PageSize = param.PageSize;
                page.OrderBy = "ID";
                var poco = ApproveSettingDAL.QueryApprovalUserByApprovUserNameAndProcessID(page, param, approvalusername.Trim(),processid);
                var entity = Mapper.Map<IList<POCO.ApprovalUser>, IList<Entity.ApprovalUser>>(poco);
                return new ResultPage<Entity.ApprovalUser>
                {
                    total = page.total,
                    data = entity
                };
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }</strong></span>

POCO延迟加载的优势:

1)非常宽容,因为只在需要的时候加载数据,不需要预先计划;

2)可能因为数据访问的延迟而降低性能,考虑到每访问父实体的子实体时,就需要访问数据库。



你可能感兴趣的:(【知识积累】Entity Framework学习:POCO延迟加载)