当前位置:编程学习 > asp >>

在EF中构建业务层小记

   在使用Entity Framework时,它很好的帮我们处理了数据处理层,在业务层,我们对实体的操作,也有一些通用的东西,比如对实体的增、删、改,这三种操作,大多数实体都相同,下面的代码就是通用的在业务处理层的方法实体,如果有单独的处理,子业务处理类可以继承这个业务父类来实现自己的功能。
         下面是定义业务父类,有三个公共方法:Add ,Remove,Mofify,和一个私有方法,GetEntitySetName,这个方作用是:在添加通用实体类时,将调用数据库实体对象的AddObject方汉,它的第一个参数需要一个实体集合名,这个名称可以通过GetEntitySetName方法得到,这个方法需要一个要添加的实体作为参数传递。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Data.Mapping;
using System.Data.Metadata.Edm;
using System.Data.EntityClient;
using System.Reflection;
using System.Collections;
 class BllEntity
    {
        //数据库对象
        protected TSMS_DBEntities tsms = new TSMS_DBEntities();
        /// <summary>
        /// 从映射文件中获取实体和数据表对应名称
        /// </summary>
        /// <param name="entityname">实体名</param>
        /// <returns>数据表名</returns>
        string GetEntitySetName(EntityObject entity)
        {
            Type entitytype = entity.GetType();//获取实体类型
            string entityname = entitytype.Name;//获取实体名称
 
            //构建连接对象
            EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder(System.Configuration.ConfigurationManager.ConnectionStrings["TSMS_DBEntities"].ConnectionString);
            string[] arrstr = ecsb.Metadata.Split('|');//得到映射文件
            EdmItemCollection e = new EdmItemCollection(arrstr[0]);//得到CSDL
            StoreItemCollection s = new StoreItemCollection(arrstr[1]);//得到SSDL
            StorageMappingItemCollection smt = new StorageMappingItemCollection(e, s, arrstr[2]);//得到MSL
            //获取合部实体对应关系
            var entities = smt[0].GetType().GetProperty("EntitySetMaps", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(smt[0], null);
            foreach (var entityvalue in (IList)entities)
            {
                //反射得到StorageSetMapping类型
                Assembly ass = Assembly.Load("System.Data.Entity,Version=3.5.0.0,culture=Neutral,PublicKeyToken=b77a5c561934e089");
                Type type = ass.GetType("System.Data.Mapping.StorageSetMapping");
                EntitySet es = (EntitySet)type.GetField("m_extent", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entityvalue);
                if (es.ElementType.Name == entityname)
                {
                    return es.Name;
                }
            }
            return null;
        }
        /// <summary>
        /// 添加实体对象
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Add(EntityObject entity)
        {
            try
            {
 
                string entitysetname = GetEntitySetName(entity);
                if (entitysetname != null)
                {
                    tsms.AddObject(entitysetname, entity);
                    tsms.SaveChanges();
                    return true;
                }
                else
                {
                    throw new BllException("获取数据库实体表失败!");
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Remove(

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,