对比Model前后数据保存不同值
1.示例代码
public class SysLogBLL<T,W> : BLLBase where T : new()
{
#region 比较方法
/// <summary>
/// 复制对象到指定对象中 只复制名称一样的
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public T Clone(W obj)
{
Type objTye = typeof(T);
Type objTyeW = typeof(W);
T t = new T();
PropertyInfo[] proinfos = objTye.GetProperties();
PropertyInfo[] proinfosw = objTyeW.GetProperties();
foreach (var i in proinfos)
{
foreach (var j in proinfosw)
{
if (i.Name == j.Name)
{
object o = j.GetValue(obj, null);
if (i.GetSetMethod() != null)
{
i.SetValue(t, o, null);
}
}
}
}
return t;
}
public T Clone(T obj)
{
Type objTye = typeof(T);
T model = new T(); PropertyInfo[] properties = objTye.GetProperties();
foreach (PropertyInfo property in properties)
{
if (!property.IsSpecialName)
{
object o = property.GetValue(obj, null);
if (property.GetSetMethod() != null)
{
property.SetValue(model, o, null);
}
}
}
return model;
}
private bool IsEqual(Type dataType, object oldObj, object newObj)
{
if (oldObj == null && newObj == null) return true;
if (oldObj != null && newObj != null)
{
if (dataType == typeof(int)) return (int)oldObj == (int)newObj;
if (dataType == typeof(decimal)) return (decimal)oldObj == (decimal)newObj;
if (dataType == typeof(double)) return (double)oldObj == (double)newObj;
if (dataType == typeof(Guid)) return (Guid)oldObj == (Guid)newObj;
if (dataType == typeof(DateTime)) return (DateTime)oldObj == (DateTime)newObj;
if (dataType == typeof(string)) return (string)oldObj == (string)newObj;
return true; //return oldObj.Equals(newObj);
}
else
{
if (dataType.BaseType == typeof(object)) return true;
else return false;
}
} /// <summary>
/// 比较两个实体,然后返回实体中每个属性值不同的内容
/// </summary>
/// <param name="oldObj"></param>
/// <param name="newObj"></param>
/// <returns></returns>
public List<Hashtable> Compare(T oldObj, T newObj)
{
Type objTye = typeof(T);
List<Hashtable> list = new List<Hashtable>();
// FieldInfo[] myFields = objTye.GetFields(BindingFlags.Public | BindingFlags.Instance);
PropertyInfo[] proinfos = objTye.GetProperties(); try
{
foreach (var item in proinfos)
{
object o = item.GetValue(oldObj, null);
object n = item.GetValue(newObj, null);
var itype = item.PropertyType.GetGenericArguments();
Type itemtype = itype.Length > ? itype[] : item.PropertyType;
if (!IsEqual(itemtype, o, n))
{
Hashtable hs = new Hashtable();
hs.Add("Key", item.Name);
hs.Add("KeyType", (itemtype != null ? itemtype.Name : item.PropertyType.Name));
hs.Add("oValue", o);
hs.Add("nValue", n);
list.Add(hs);
}
}
}
catch (Exception ex)
{ throw;
} return list;
} #endregion #region 保存LogAction
/// <summary>
/// 保存数据
/// </summary>
/// <param name="Lgaction">Crm_LogAction 对象</param>
/// <param name="originaldata">原始对象数据</param>
/// <param name="currentdata">当前对象数据</param>
/// <returns></returns>
public bool SaveLogActoin(Crm_LogAction Lgaction, T originaldata, T currentdata)
{
bool result = false; //执行结果
if (Lgaction != null)
{
try
{
MainEntities.Crm_LogAction.Add(Lgaction);
MainEntities.SaveChanges();
List<Hashtable> lt = Compare(originaldata, currentdata);
if (lt.Count() > )
{
foreach (var h in lt)
{
Hashtable hitem = h as Hashtable;
if (hitem != null && hitem.Count > )
{
MainEntities.Crm_LogActionDetail.Add(new Crm_LogActionDetail
{
LogId = Lgaction.LogId,
ColumnName = hitem["Key"].ToString(),
ColumnDataType = hitem["KeyType"].ToString(),
CreateTime = DateTime.Now,
CreateUser = CurrentUser.Name + "[" + CurrentUser.Id + "]",
NewValue = hitem["nValue"].ToString(),
OldValue = hitem["oValue"].ToString(),
});
}
}
MainEntities.SaveChanges();
result = true;
}
}
catch (Exception)
{ result = false;
}
}
return result;
} /// <summary>
/// 保存数据
/// </summary>
/// <param name="TableName">表名</param>
/// <param name="OperationId">操作类型 0=add 1=delte 2=update </param>
/// <param name="originaldata"></param>
/// <param name="currentdata"></param>
/// <returns></returns>
public bool SaveLogActoin(string TableName, int OperationId, T originaldata, T currentdata)
{
bool result = false; //执行结果 Crm_LogAction Lgaction=new Crm_LogAction ();
try
{
var query = MainEntities.Crm_LogActionType.Where(w => w.TableName == TableName).SingleOrDefault();
if (query != null)
{
Lgaction= new Crm_LogAction
{
OperationId = OperationId,//修改=2
CreateTime = DateTime.Now,
CreateUser = CurrentUser.Name + "[" + CurrentUser.Id + "]",
LogTypeId = query.TypeId,
TableName = query.TableName,
BusinessName = query.BusinessName
}; MainEntities.Crm_LogAction.Add(Lgaction);
MainEntities.SaveChanges(); List<Hashtable> lt = Compare(originaldata, currentdata);if (lt.Count() > )
{
foreach (var h in lt)
{
Hashtable hitem = h as Hashtable;
if (hitem != null && hitem.Count > )
{
MainEntities.Crm_LogActionDetail.Add(new Crm_LogActionDetail
{
LogId = Lgaction.LogId,
ColumnName = hitem["Key"].ToString(),
ColumnDataType = hitem["KeyType"].ToString(),
CreateTime = DateTime.Now,
CreateUser = CurrentUser.Name + "[" + CurrentUser.Id + "]",
NewValue =hitem["nValue"]==null?"": hitem["nValue"].ToString(),
OldValue =hitem["oValue"]==null?"": hitem["oValue"].ToString(),
});
}
}
MainEntities.SaveChanges();
result = true;
}
} }
catch (Exception)
{ result = false;
}
return result;
}
#endregion
} 2.调用示例:
SysLogBLL<Crm_Contact, ContactBankCreateOrEdit> Syslog = new SysLogBLL<Crm_Contact, ContactBankCreateOrEdit>();
Crm_Contact currentdata = Syslog.Clone(model); //新数据
Crm_Contact originaldata = dModel;
Syslog.SaveLogActoin("Crm_Contact", 2, originaldata, currentdata);
对比Model前后数据保存不同值的更多相关文章
- Hibernate数据保存操作方法的原理对比
Interface Session All Superinterfaces: Serializable All Known Subinterfaces: EventSource, Session Al ...
- c# 键值数据保存XML文件
/// <summary> /// 键值数据保存XML文件 /// </summary> /// <param name="fileName"> ...
- Http批量异步发送和数据保存
先说需求. 有个服务程序定时扫描指定文件夹下一个所有文件,文件包含了多个用户(客户)信息及对应的http发送地址和发送数据.现在该服务程序需要提取这些用户信息,然后批量进行发送:发送完后需要将http ...
- springMVC源码分析--FlashMap和FlashMapManager重定向数据保存
在上一篇博客springMVC源码分析--页面跳转RedirectView(三)中我们看到了在RedirectView跳转时会将跳转之前的请求中的参数保存到fFlashMap中,然后通过FlashMa ...
- Delphi:ClientDataset+TDataSetProvider的数据保存问题
看到一篇介绍ClientDataSet和TDataSetProvider,非常精彩,特此保存. ==================================================== ...
- Python小数据保存,有多少中分类?不妨看看他们的类比与推荐方案...
小数据存储 我们在编写代码的时候,经常会涉及到数据存储的情况,如果是爬虫得到的大数据,我们会选择使用数据库,或者excel存储.但如果只是一些小数据,或者说关联性较强且存在存储后复用的数据,我们该如何 ...
- controller进行数据保存以及作用域
controller进行数据保存以及作用域 一.request域 1.ModelAndView 在ModelAndView中进行存键值对,也可以进行跳转的地址存储,但是返回类型必须是ModelAndV ...
- Android中的数据保存
形式 Android的数据保存分为3种形式:file, SharedPreference, Database 文件 主要思想就是通过Context类中提供的openFileInput和openFile ...
- 编辑器插件数据保存之Serializable
Editor数据保存需求 做编辑器插件开发时,当打开一个窗口,对数值进行修改后,在关闭窗口或重新打开Unity时,希望能保存上次的数据. 相关知识 Serialization ,ScriptableO ...
随机推荐
- C# 封装首页、上一页、下一月、尾页处理器
public void BtnPageClickEvent(object sender,string focusForeground,string lostFocusForeground) { But ...
- 【笔记篇】Ubuntu一日游
今天做数据的时候在Windows下出问题了(好像是爆栈了QAQ) 于是乎就打开了自己的Ubuntu虚拟机… 然而沉迷Windows的我已经忘记自己对这台虚拟机做过什么(比如装残了一个ycm自己都不知道 ...
- Luogu P2717 寒假作业(平衡树)
P2717 寒假作业 题意 题目背景 \(zzs\)和\(zzy\)正在被寒假作业折磨,然而他们有答案可以抄啊. 题目描述 他们共有\(n\)项寒假作业.\(zzy\)给每项寒假作业都定义了一个疲劳值 ...
- 任意文件读取漏洞常用payload合集
直接整理到github上了,https://github.com/tdifg/payloads 其他payload以后不定期更新
- bzoj4144 Petrol
题意:给你一张n个点m条边的带权无向图.其中由s个点是加油站.询问从x加油站到y加油站,油箱容量<=b,能否走到? n,m,q,s<=20W,b<=2e9. 标程: #include ...
- localhost与127.0.0.1区别
一.连接MySQL数据库有两种方式:TCP/IP(一般理解的端口的那种)和Unix套接字(一般叫socket或者sock) 大部分情况下,可以用localhost代表本机127.,但是在MySQL连接 ...
- 关于操作系统中英文切换的.po和.mo介绍
一.文件简介 .po文件,.mo文件,.pot文件是由gettext程序生成或者使用的源代码和编译结果. 1..pot文件 是一种模板文件,其实质与.po文件一样,其中包含了从源代码中提取所有的 ...
- springboot中的web项目不能访问templates中的静态资源
方法1: 重新创建文件夹,配置yml文件: spring.resources.static-locations=classpath:/view/ spring.mvc.view.suffix=.htm ...
- memcache课程---1、memcache介绍及安装(memcache作用)
memcache课程---1.memcache介绍及安装(memcache作用) 一.总结 一句话总结: 减少对数据库的访问,因为数据库的访问比较花费时间 1.memcache为什么比操作数据库快的多 ...
- HOOK NtCreateSection
本程序使用了hde32反汇编引擎,所以性能更加稳定! #pragma once #include <ntddk.h> NTSYSAPI NTSTATUS NTAPI NtCreateSec ...