Skip to content

Commit

Permalink
Merge pull request #58 from bing-framework/dev_3.1
Browse files Browse the repository at this point in the history
publish: 2.2.3
  • Loading branch information
jianxuanbing authored Mar 15, 2022
2 parents e1728e5 + 0b844ca commit 9892e0a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
34 changes: 27 additions & 7 deletions framework/src/Bing.Datas.EntityFramework/Logs/EfLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Bing.Extensions;
using Bing.Helpers;
using Bing.Logs;
using Bing.Reflection;
using Bing.Uow;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -37,6 +38,7 @@ public class EfLog : ILogger
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
Func<TState, Exception, string> formatter)
{
var success = false;
var config = GetConfig();
var log = GetUnitOfWork()?.Log;
if (log == null)
Expand All @@ -46,13 +48,31 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
if (!string.IsNullOrWhiteSpace(GetUnitOfWork().TraceId))
log.Tag(GetUnitOfWork()?.TraceId);
log.Tag(TraceLogName);
log
.Caption($"执行EF操作:{formatter(state, exception)}")
.Content($"工作单元跟踪号:{GetUnitOfWork()?.TraceId}")
.Content($"事件ID:{eventId.Id}")
.Content($"事件名称:{eventId.Name}");
AddContent(state, config, log);
log.Exception(exception).Trace();
var caption = string.Empty;
try
{
log
.Content($"工作单元跟踪号:{GetUnitOfWork()?.TraceId}")
.Content($"事件ID:{eventId.Id}")
.Content($"事件名称:{eventId.Name}");
AddContent(state, config, log);
log.Exception(exception);
caption = formatter(state, exception);
success = true;
}
catch (Exception e)
{
InvokeHelper.OnInvokeException?.Invoke(e);
success = false;
}
finally
{
log.Caption($"执行EF操作:{caption}");
if (success)
log.Trace();
else
log.Error();
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ public void Add<TObject>(IChangeTrackable<TObject> leftObj, TObject rightObj) wh
/// <typeparam name="TProperty">属性类型</typeparam>
/// <typeparam name="TValue">值类型</typeparam>
/// <param name="expression">属性表达式。范例:t => t.Name</param>
/// <param name="obj">领域对象</param>
/// <param name="newValue">新值。范例:newEntity.Name</param>
public void Add<TObject, TProperty, TValue>(Expression<Func<TObject, TProperty>> expression, TValue newValue) where TObject : IDomainObject
public void Add<TObject, TProperty, TValue>(Expression<Func<TObject, TProperty>> expression, TObject obj, TValue newValue) where TObject : IDomainObject
{
var member = Lambdas.GetMemberExpression(expression);
var name = Lambdas.GetMemberName(member);
var desc = Reflection.Reflections.GetDisplayNameOrDescription(member.Member);
var value = Lambdas.GetValue(expression);
var value = member.Member.GetPropertyValue(obj);
Add(name, desc, Conv.To<TValue>(value), newValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected virtual void AddChanges(TObject newObj) { }
/// <typeparam name="TValue">值类型</typeparam>
/// <param name="expression">属性表达式。范例:t => t.Name</param>
/// <param name="newValue">新值。范例:newEntity.Name</param>
protected void AddChange<TProperty, TValue>(Expression<Func<TObject, TProperty>> expression, TValue newValue) => _changeTrackingContext.Add(expression, newValue);
protected void AddChange<TProperty, TValue>(Expression<Func<TObject, TProperty>> expression, TValue newValue) => _changeTrackingContext.Add(expression, AssignableType(this), newValue);

/// <summary>
/// 添加变更
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public async Task Test_SaveAsync_Add()
[Fact]
public async Task Test_SaveAsync_Update()
{
_repository.FindAsync(_id).Returns(t => new EntitySample(_id));
_repository.FindAsync(_id).Returns(t => new EntitySample(_id) { Name = "a" });
await _service.SaveAsync(new DtoSample { Id = _id.ToString(), Name = "b" });
await _repository.DidNotReceive().AddAsync(Arg.Any<EntitySample>());
await _repository.Received().UpdateAsync(Arg.Is<EntitySample>(t => t.Name == "b"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public EntitySample(Guid id) : base(id) { }
/// </summary>
[IgnoreMap]
public string IgnoreValue { get; set; }

/// <summary>
/// 添加变更列表
/// </summary>
protected override void AddChanges(EntitySample other)
{
AddChange(x => x.Id, other.Id);
AddChange(x => x.Name, other.Name);
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>2</VersionMajor>
<VersionMinor>2</VersionMinor>
<VersionPatch>2</VersionPatch>
<VersionPatch>3</VersionPatch>
<VersionQuality>20220104-1</VersionQuality>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
<!--<VersionSuffix>preview-$(VersionQuality)</VersionSuffix>-->
Expand Down

0 comments on commit 9892e0a

Please sign in to comment.