- 增加 Aop.AuditValue 事件,在插入/更新数据时审计属性值;

This commit is contained in:
28810 2019-08-25 18:13:02 +08:00
parent baa6c413a0
commit b57d35ae9b
12 changed files with 344 additions and 9 deletions

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Xunit;
namespace FreeSql.Tests.MySqlConnector
{
public class MySqlAop
{
class TestAuditValue
{
public Guid id { get; set; }
[Now]
public DateTime createtime { get; set; }
}
class NowAttribute: Attribute { }
[Fact]
public void AuditValue()
{
var now = DateTime.Now;
var item = new TestAuditValue();
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
{
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
e.Value = DateTime.Now;
};
g.mysql.Aop.AuditValue += audit;
g.mysql.Insert(item).ExecuteAffrows();
g.mysql.Aop.AuditValue -= audit;
Assert.Equal(item.createtime.Date, now.Date);
}
}
}

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Xunit;
namespace FreeSql.Tests.MySql
{
public class MySqlAopTest
{
class TestAuditValue
{
public Guid id { get; set; }
[Now]
public DateTime createtime { get; set; }
}
class NowAttribute: Attribute { }
[Fact]
public void AuditValue()
{
var now = DateTime.Now;
var item = new TestAuditValue();
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
{
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
e.Value = DateTime.Now;
};
g.mysql.Aop.AuditValue += audit;
g.mysql.Insert(item).ExecuteAffrows();
g.mysql.Aop.AuditValue -= audit;
Assert.Equal(item.createtime.Date, now.Date);
}
}
}

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Xunit;
namespace FreeSql.Tests.Oracle
{
public class OracleAopTest
{
class TestAuditValue
{
public Guid id { get; set; }
[Now]
public DateTime createtime { get; set; }
}
class NowAttribute: Attribute { }
[Fact]
public void AuditValue()
{
var now = DateTime.Now;
var item = new TestAuditValue();
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
{
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
e.Value = DateTime.Now;
};
g.oracle.Aop.AuditValue += audit;
g.oracle.Insert(item).ExecuteAffrows();
g.oracle.Aop.AuditValue -= audit;
Assert.Equal(item.createtime.Date, now.Date);
}
}
}

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Xunit;
namespace FreeSql.Tests.PostgreSQL
{
public class PostgreSQLAopTest
{
class TestAuditValue
{
public Guid id { get; set; }
[Now]
public DateTime createtime { get; set; }
}
class NowAttribute: Attribute { }
[Fact]
public void AuditValue()
{
var now = DateTime.Now;
var item = new TestAuditValue();
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
{
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
e.Value = DateTime.Now;
};
g.pgsql.Aop.AuditValue += audit;
g.pgsql.Insert(item).ExecuteAffrows();
g.pgsql.Aop.AuditValue -= audit;
Assert.Equal(item.createtime.Date, now.Date);
}
}
}

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Xunit;
namespace FreeSql.Tests.SqlServer
{
public class SqlServerAopTest
{
class TestAuditValue
{
public Guid id { get; set; }
[Now]
public DateTime createtime { get; set; }
}
class NowAttribute: Attribute { }
[Fact]
public void AuditValue()
{
var now = DateTime.Now;
var item = new TestAuditValue();
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
{
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
e.Value = DateTime.Now;
};
g.sqlserver.Aop.AuditValue += audit;
g.sqlserver.Insert(item).ExecuteAffrows();
g.sqlserver.Aop.AuditValue -= audit;
Assert.Equal(item.createtime.Date, now.Date);
}
}
}

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Xunit;
namespace FreeSql.Tests.Sqlite
{
public class SqliteAopTest
{
class TestAuditValue
{
public Guid id { get; set; }
[Now]
public DateTime createtime { get; set; }
}
class NowAttribute: Attribute { }
[Fact]
public void AuditValue()
{
var now = DateTime.Now;
var item = new TestAuditValue();
EventHandler<Aop.AuditValueEventArgs> audit = (s, e) =>
{
if (e.Property.GetCustomAttribute<NowAttribute>(false) != null)
e.Value = DateTime.Now;
};
g.sqlite.Aop.AuditValue += audit;
g.sqlite.Insert(item).ExecuteAffrows();
g.sqlite.Aop.AuditValue -= audit;
Assert.Equal(item.createtime.Date, now.Date);
}
}
}

View File

@ -2020,6 +2020,11 @@
CodeFirst迁移执行完成触发
</summary>
</member>
<member name="P:FreeSql.IAop.AuditValue">
<summary>
Insert/Update自动值处理, e.Column.SetMapValue(
</summary>
</member>
<member name="P:FreeSql.Aop.ToListEventArgs.List">
<summary>
可重新装饰的引用数据
@ -2145,6 +2150,26 @@
耗时(单位:毫秒)
</summary>
</member>
<member name="P:FreeSql.Aop.AuditValueEventArgs.AutoValueType">
<summary>
类型
</summary>
</member>
<member name="P:FreeSql.Aop.AuditValueEventArgs.Column">
<summary>
属性列的元数据
</summary>
</member>
<member name="P:FreeSql.Aop.AuditValueEventArgs.Property">
<summary>
反射的属性信息
</summary>
</member>
<member name="P:FreeSql.Aop.AuditValueEventArgs.Value">
<summary>
获取实体的属性值,也可以设置实体的属性新值
</summary>
</member>
<member name="P:FreeSql.ICodeFirst.IsAutoSyncStructure">
<summary>
【开发环境必备】自动同步实体结构到数据库,程序运行中检查实体表是否存在,然后创建或修改

View File

@ -1,5 +1,6 @@
using FreeSql.DataAnnotations;
using FreeSql.DatabaseModel;
using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.Data.Common;
@ -53,6 +54,11 @@ namespace FreeSql
/// CodeFirst迁移执行完成触发
/// </summary>
EventHandler<Aop.SyncStructureAfterEventArgs> SyncStructureAfter { get; set; }
/// <summary>
/// Insert/Update自动值处理, e.Column.SetMapValue(
/// </summary>
EventHandler<Aop.AuditValueEventArgs> AuditValue { get; set; }
}
}
@ -264,4 +270,43 @@ namespace FreeSql.Aop
/// </summary>
public long ElapsedMilliseconds => this.Stopwatch.ElapsedMilliseconds;
}
public class AuditValueEventArgs : EventArgs
{
public AuditValueEventArgs(AutoValueType autoValueType, ColumnInfo column, PropertyInfo property, object value)
{
this.AutoValueType = autoValueType;
this.Column = column;
this.Property = property;
this.Value = value;
}
/// <summary>
/// 类型
/// </summary>
public AutoValueType AutoValueType { get; }
/// <summary>
/// 属性列的元数据
/// </summary>
public ColumnInfo Column { get; }
/// <summary>
/// 反射的属性信息
/// </summary>
public PropertyInfo Property { get; }
/// <summary>
/// 获取实体的属性值,也可以设置实体的属性新值
/// </summary>
public object Value
{
get => _value;
set
{
_value = value;
this.IsChanged = true;
}
}
private object _value;
internal bool IsChanged { get; private set; }
}
public enum AutoValueType { Update, Insert }
}

View File

@ -17,5 +17,6 @@ namespace FreeSql.Internal.CommonProvider
public EventHandler<Aop.CurdAfterEventArgs> CurdAfter { get; set; }
public EventHandler<Aop.SyncStructureBeforeEventArgs> SyncStructureBefore { get; set; }
public EventHandler<Aop.SyncStructureAfterEventArgs> SyncStructureAfter { get; set; }
public EventHandler<Aop.AuditValueEventArgs> AuditValue { get; set; }
}
}

View File

@ -559,6 +559,13 @@ namespace FreeSql.Internal.CommonProvider
object val = col.GetMapValue(d);
if (col.Attribute.IsPrimary && col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
col.SetMapValue(d, val = FreeUtil.NewMongodbId());
if (_orm.Aop.AuditValue != null)
{
var auditArgs = new Aop.AuditValueEventArgs(Aop.AutoValueType.Insert, col, _table.Properties[col.CsName], val);
_orm.Aop.AuditValue(this, auditArgs);
if (auditArgs.Value != null)
col.SetMapValue(d, val = auditArgs.Value);
}
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, col.Attribute.MapType, val));
else

View File

@ -605,15 +605,20 @@ namespace FreeSql.Internal.CommonProvider
{
if (colidx > 0) sb.Append(", ");
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
var value = col.GetMapValue(_source.First());
if (_noneParameter)
var val = col.GetMapValue(_source.First());
if (_orm.Aop.AuditValue != null)
{
sb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, value));
var auditArgs = new Aop.AuditValueEventArgs(Aop.AutoValueType.Update, col, _table.Properties[col.CsName], val);
_orm.Aop.AuditValue(this, auditArgs);
if (auditArgs.Value != null)
col.SetMapValue(_source.First(), val = auditArgs.Value);
}
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, val));
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
_commonUtils.AppendParamter(_paramsSource, null, col.Attribute.MapType, value);
_commonUtils.AppendParamter(_paramsSource, null, col.Attribute.MapType, val);
}
++colidx;
}
@ -646,17 +651,22 @@ namespace FreeSql.Internal.CommonProvider
cwsb.Append(" \r\nWHEN ");
ToSqlWhen(cwsb, _table.Primarys, d);
cwsb.Append(" THEN ");
var value = col.GetMapValue(d);
if (_noneParameter)
var val = col.GetMapValue(d);
if (_orm.Aop.AuditValue != null)
{
cwsb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, value));
var auditArgs = new Aop.AuditValueEventArgs(Aop.AutoValueType.Update, col, _table.Properties[col.CsName], val);
_orm.Aop.AuditValue(this, auditArgs);
if (auditArgs.Value != null)
col.SetMapValue(_source.First(), val = auditArgs.Value);
}
if (_noneParameter)
cwsb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, val));
else
{
cwsb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
_commonUtils.AppendParamter(_paramsSource, null, col.Attribute.MapType, value);
_commonUtils.AppendParamter(_paramsSource, null, col.Attribute.MapType, val);
}
if (isnull == false) isnull = value == null || value == DBNull.Value;
if (isnull == false) isnull = val == null || val == DBNull.Value;
}
cwsb.Append(" END");
if (isnull == false)

View File

@ -70,6 +70,13 @@ namespace FreeSql.Oracle.Curd
object val = col.GetMapValue(d);
if (col.Attribute.IsPrimary && col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
col.SetMapValue(d, val = FreeUtil.NewMongodbId());
if (_orm.Aop.AuditValue != null)
{
var auditArgs = new Aop.AuditValueEventArgs(Aop.AutoValueType.Insert, col, _table.Properties[col.CsName], val);
_orm.Aop.AuditValue(this, auditArgs);
if (auditArgs.Value != null)
col.SetMapValue(d, val = auditArgs.Value);
}
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, col.Attribute.MapType, val));
else