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. Linux-c 线程锁

      typedef struct _my_mutex { pthread_mutex_t mutex; //互斥锁 pthread_mutexattr_t mta; //互斥锁属性 } my_mute ...

  2. Android开发 设备横屏与竖屏的详解

    需要了解横竖屏切换关键知识 1.在Android设备的横竖屏幕,每一次切换横竖屏其实是在重新创建Activity,Activity会重新走一遍生命周期.从onCreate 到 onDestroy 2. ...

  3. [转].NET4.0新特性集合贴

    vs2010正式版4月12日发布了,前几天我也下了一个,但这几天都没有时间好好试用一下,今天针对C#语言的新特性使用了一下,感觉还不错,有几个新特性和大家分享一下,希望我没有太火星…… 一.新关键词— ...

  4. 移动端,fixed bottom问题

    //不显示 .bar { position:fixed; bottom:0; z-index:99; } //显示 .bar{ position:fixed; bottom:calc(90vh); / ...

  5. js遍历获取表格内数据方法

    1.一般的表格结构如下 <table> <tr> <td>id</td> <td>name</td> </tr> & ...

  6. 根据url提取网站域名的方法小结

    前言:最近使用到了他人总结的一个基础类库.查看了下源码,发现String帮助类的一个辅助方法不是很严谨,重构之. 1.原来程序的写法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  7. VS2010文件包含

    一. 关于iostream.h VS2010的iostream不加.h在后面(加.h的是旧版本),把iostream作为类,正确使用方法: #include<iostream> using ...

  8. PAT甲级——A1009 Product of Polynomials

    This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...

  9. hibernate查询timestamp条件

    参考https://blog.csdn.net/zuozuoshenghen/article/details/50540661 Mysql中Timestamp字段的格式为yyyy-MM-dd HH-m ...

  10. HttpServletRequest request 获取当前登录的用户-获取当前用户

    有的业务需要知道当前登录的用户 当然需要引用这个啦 import javax.servlet.http.HttpServletRequest; 然后 HttpSession session = req ...