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前后数据保存不同值的更多相关文章

  1. Hibernate数据保存操作方法的原理对比

    Interface Session All Superinterfaces: Serializable All Known Subinterfaces: EventSource, Session Al ...

  2. c# 键值数据保存XML文件

    /// <summary> /// 键值数据保存XML文件 /// </summary> /// <param name="fileName"> ...

  3. Http批量异步发送和数据保存

    先说需求. 有个服务程序定时扫描指定文件夹下一个所有文件,文件包含了多个用户(客户)信息及对应的http发送地址和发送数据.现在该服务程序需要提取这些用户信息,然后批量进行发送:发送完后需要将http ...

  4. springMVC源码分析--FlashMap和FlashMapManager重定向数据保存

    在上一篇博客springMVC源码分析--页面跳转RedirectView(三)中我们看到了在RedirectView跳转时会将跳转之前的请求中的参数保存到fFlashMap中,然后通过FlashMa ...

  5. Delphi:ClientDataset+TDataSetProvider的数据保存问题

    看到一篇介绍ClientDataSet和TDataSetProvider,非常精彩,特此保存. ==================================================== ...

  6. Python小数据保存,有多少中分类?不妨看看他们的类比与推荐方案...

    小数据存储 我们在编写代码的时候,经常会涉及到数据存储的情况,如果是爬虫得到的大数据,我们会选择使用数据库,或者excel存储.但如果只是一些小数据,或者说关联性较强且存在存储后复用的数据,我们该如何 ...

  7. controller进行数据保存以及作用域

    controller进行数据保存以及作用域 一.request域 1.ModelAndView 在ModelAndView中进行存键值对,也可以进行跳转的地址存储,但是返回类型必须是ModelAndV ...

  8. Android中的数据保存

    形式 Android的数据保存分为3种形式:file, SharedPreference, Database 文件 主要思想就是通过Context类中提供的openFileInput和openFile ...

  9. 编辑器插件数据保存之Serializable

    Editor数据保存需求 做编辑器插件开发时,当打开一个窗口,对数值进行修改后,在关闭窗口或重新打开Unity时,希望能保存上次的数据. 相关知识 Serialization ,ScriptableO ...

随机推荐

  1. easyui combotree的使用示例

    一.View: 1.定义输入控件 <input id="ParentId" name="ParentId"> 2.绑定combotree $('#P ...

  2. Python对象和类

    Python 里的所有数据都是以对象形式存在的,对象是类的实例. 定义类(class) 使用class来定义一个类. 比如,定义一个cat类,如下: class Cat(): def __init__ ...

  3. activeMQ消息队列安装配置

    1.  下载 到官网下载最新版本,有windows版本和linux版本的. http://activemq.apache.org/download.html 2.   windows下部署 Activ ...

  4. data hazard in CPU pipeline

    1, background info 5 stages in CPU pipeline: IF, ID, EX, MM, WB IF – Instruction Fetch ID – Instruct ...

  5. csps模拟67神炎皇,降雷皇,幻魔皇题解

    题面:https://www.cnblogs.com/Juve/articles/11648975.html 神炎皇: 打表找规律?和$\phi$有关? 答案就是$\sum\limits_{i=2}^ ...

  6. 第一个duilib程序 - 实现HelloWorld详解

    duilib是一个windows下的皮肤库,用win32写的... 先看个效果图吧: 要使用duilib库,必须先把库导入,代码如下: View Row Code 1 #include "x ...

  7. Zuul上传文件

    对于1M以内的文件上传,无需任何处理,大文件10M以上需要为上传路径添加/zuul前缀,也可使用zuul.servlet-path自定义前缀 如果Zuul使用了Ribbon做负载均衡,那么对于超大的文 ...

  8. filebeat+redis+logstash+elasticsearch+kibana搭建日志分析系统

    filebeat+redis+elk搭建日志分析系统 官网下载地址:https://www.elastic.co/downloads 1.下载安装filebeat wget https://artif ...

  9. eclipse memory analyzer对系统内存溢出堆文件解析(转)

    本文转之:https://blog.csdn.net/rachel_luo/article/details/8992461 前言 性能分析工具之-- Eclipse Memory Analyzer t ...

  10. 推荐5款超实用的.NET性能分析工具

    虽然.NET框架号称永远不会发生内存泄漏,原因是引入了内存回收机制.但在实际应用中,往往我们分配了对象但没有释放指向该对象的引用,导致对象永远无法释放.最常见的情况就是给对象添加了事件处理函数,但当不 ...