对比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 ...
随机推荐
- Ubuntu环境下安装Scala以及安装IntelliJ Scala插件(Plugin)
一.Scala介绍 1.结合Spark处理大数据 这是Scala的一个主要应用,而且Spark也是那Scala写的. 2.Java的脚本语言版 可以直接写Scala的脚本,也可以在.sh直接使用Sc ...
- linux常用软连接使用ln -s
[软连接]另外一种连接称之为符号连接(Symbolic Link),也叫软连接.软链接文件有类似于Windows的快捷方式.它实际上是一个特殊的文件.在符号连接中,文件实际上是一个文本文件,其中包含的 ...
- 杂项-关于strlen()的使用
发现了一个很坑的东西. 看下面两份代码: //code1 char s[N]; ;i<strlen(s);i++)Do(); //code2 char s[N]; ;s[i];i++)Do(); ...
- SQFREE - Square-free integers
SQFREE - Square-free integers 求n以内,约数中不包含任意一个平方数的个数,\(n≤10^{14}\). 解 显然为约数计数问题,于是想办法转换为代数问题,不难列出 \[a ...
- 查看java资源的占用
1,使用命令top -p <pid> ,显示你的java进程的内存情况,pid是你的java进程号,比如1232,按H,获取每个线程的内存情况3,找到内存和cpu占用最高的线程pid,比如 ...
- localhost与127.0.0.1区别
一.连接MySQL数据库有两种方式:TCP/IP(一般理解的端口的那种)和Unix套接字(一般叫socket或者sock) 大部分情况下,可以用localhost代表本机127.,但是在MySQL连接 ...
- Type.GetType(string.contains(','))
例如 Type type = Type.GetType("ACalCoreServiceLib.BaseService,ACalCoreServiceLib"); 里面的ACalC ...
- thinkphp Mongo模型
Mongo模型是专门为Mongo数据库驱动而支持的Model扩展,如果需要操作Mongo数据库的话,自定义的模型类必须继承Think\Model\MongoModel. Mongo模型为操作Mongo ...
- csp-s模拟测试61砖块, 数字,甜圈题解
题面:https://www.cnblogs.com/Juve/articles/11626350.html 砖块: 直接模拟即可,map统计被覆盖的次数 #include<iostream&g ...
- TSP+期望——lightoj1287记忆化搜索,好题!
感觉是很经典的题 记忆化时因为不好直接通过E判断某个状态是否已经求过,所以再加一个vis打标记即可 /*E[S][u]表示从u出发当前状态是S的期望*/ #include<bits/stdc++ ...