Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加泛型AjaxResult<T> #297

Open
wants to merge 4 commits into
base: dev/snow
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions samples/web/Liuliu.Demo.WebApi/Liuliu.Demo.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

<ItemGroup>
<ProjectReference Include="..\..\..\src\OSharp.AutoMapper\OSharp.AutoMapper.csproj" />
<ProjectReference Include="..\..\..\src\OSharp.EntityFrameworkCore.MySql\OSharp.EntityFrameworkCore.MySql.csproj" />
<ProjectReference Include="..\..\..\src\OSharp.EntityFrameworkCore.Oracle\OSharp.EntityFrameworkCore.Oracle.csproj" />
<ProjectReference Include="..\..\..\src\OSharp.EntityFrameworkCore.PostgreSql\OSharp.EntityFrameworkCore.PostgreSql.csproj" />
<ProjectReference Include="..\..\..\src\OSharp.EntityFrameworkCore.Sqlite\OSharp.EntityFrameworkCore.Sqlite.csproj" />
<ProjectReference Include="..\..\..\src\OSharp.EntityFrameworkCore.SqlServer\OSharp.EntityFrameworkCore.SqlServer.csproj" />
Expand Down
18 changes: 17 additions & 1 deletion src/OSharp.AspNetCore/Mvc/Filters/UnitOfWorkImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// <last-date>2019-05-14 17:37</last-date>
// -----------------------------------------------------------------------

using Newtonsoft.Json;

namespace OSharp.AspNetCore.Mvc.Filters;

/// <summary>
Expand Down Expand Up @@ -55,6 +57,7 @@ public void OnActionExecuted(ActionExecutedContext context)
context.ExceptionHandled = true;
}
}
var x = context.Result;
if (context.Result is JsonResult result1)
{
if (result1.Value is AjaxResult ajax)
Expand All @@ -80,7 +83,20 @@ public void OnActionExecuted(ActionExecutedContext context)
}
else
{
_unitOfWork?.Commit();
try
{
var ajax2 = JsonConvert.DeserializeObject<AjaxResult>(result2.Value.ToJsonString());
type = ajax2.Type;
message = ajax2.Content;
if (ajax2.Succeeded())
{
_unitOfWork?.Commit();
}
}
catch
{
_unitOfWork?.Commit();
}
}
}
//普通请求
Expand Down
133 changes: 133 additions & 0 deletions src/OSharp.AspNetCore/UI/AjaxResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,36 @@ public AjaxResult(object data, AjaxResultType type = AjaxResultType.Success, str
: this(content ?? type.ToDescription(), data, type)
{ }

/// <summary>
/// 初始化一个<see cref="AjaxResult"/>类型的新实例
/// </summary>
public AjaxResult(AjaxResultType type, string content, object data)
{
Type = type;
Content = content;
Data = data;
}

/// <summary>
/// 初始化一个<see cref="AjaxResult"/>类型的新实例
/// </summary>
public AjaxResult(AjaxResultType type, string content)
{
Type = type;
Content = content;
Data = null;
}

/// <summary>
/// 初始化一个<see cref="AjaxResult"/>类型的新实例
/// </summary>
public AjaxResult(string content)
{
Type = AjaxResultType.Success;
Content = content;
Data = null;
}

/// <summary>
/// 获取或设置 Ajax操作结果类型
/// </summary>
Expand Down Expand Up @@ -84,3 +114,106 @@ public static AjaxResult Success(object data = null)
return new AjaxResult("操作执行成功", AjaxResultType.Success, data);
}
}

public class AjaxResult<T> where T : class
{
/// <summary>
/// 初始化一个<see cref="AjaxResult"/>类型的新实例
/// </summary>
public AjaxResult()
: this(null)
{ }

/// <summary>
/// 初始化一个<see cref="AjaxResult"/>类型的新实例
/// </summary>
public AjaxResult(string content, AjaxResultType type = AjaxResultType.Success, T data = null)
: this(content, data, type)
{ }

/// <summary>
/// 初始化一个<see cref="AjaxResult"/>类型的新实例
/// </summary>
public AjaxResult(string content, T data, AjaxResultType type = AjaxResultType.Success)
{
Type = type;
Content = content;
Data = data;
}

/// <summary>
/// 初始化一个<see cref="AjaxResult"/>类型的新实例
/// </summary>
public AjaxResult(T data, AjaxResultType type = AjaxResultType.Success, string content = null)
: this(content ?? type.ToDescription(), data, type)
{ }

/// <summary>
/// 初始化一个<see cref="AjaxResult"/>类型的新实例
/// </summary>
public AjaxResult(AjaxResultType type, string content, T data)
{
Type = type;
Content = content;
Data = data;
}

/// <summary>
/// 初始化一个<see cref="AjaxResult"/>类型的新实例
/// </summary>
public AjaxResult(AjaxResultType type, string content)
{
Type = type;
Content = content;
Data = null;
}

/// <summary>
/// 初始化一个<see cref="AjaxResult"/>类型的新实例
/// </summary>
public AjaxResult(string content)
{
Type = AjaxResultType.Success;
Content = content;
Data = null;
}

/// <summary>
/// 获取或设置 Ajax操作结果类型
/// </summary>
public AjaxResultType Type { get; set; }

/// <summary>
/// 获取或设置 消息内容
/// </summary>
public string Content { get; set; }

/// <summary>
/// 获取或设置 返回数据
/// </summary>
public T Data { get; set; }

/// <summary>
/// 是否成功
/// </summary>
public bool Succeeded()
{
return Type == AjaxResultType.Success;
}

/// <summary>
/// 是否错误
/// </summary>
public bool Error()
{
return Type == AjaxResultType.Error;
}

/// <summary>
/// 成功的AjaxResult
/// </summary>
public static AjaxResult Success(T data = null)
{
return new AjaxResult("操作执行成功", AjaxResultType.Success, data);
}
}
29 changes: 29 additions & 0 deletions src/OSharp.AspNetCore/UI/AjaxResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,35 @@ public static AjaxResult ToAjaxResult<T>(this OperationResult result, Func<T, ob
return new AjaxResult(content, type, data);
}

/// <summary>
/// 将业务操作结果转ajax操作结果,并处理强类型的 OperationResult.Data
/// </summary>
public static AjaxResult<T> ToAjaxResultEx<T>(this OperationResult<T> result, Func<T, T> dataFunc = null) where T : class
{
string content = result.Message ?? result.ResultType.ToDescription();
AjaxResultType type = result.ResultType.ToAjaxResultType();
T data = dataFunc == null ? result.Data : dataFunc(result.Data);
return new AjaxResult<T>(content, type, data);
}

/// <summary>
/// 将业务操作结果转ajax操作结果,会将 object 类型的 OperationResult.Data 转换为强类型 T,再通过 dataFunc 进行进一步处理
/// </summary>
public static AjaxResult<T> ToAjaxResultEx<T>(this OperationResult result, Func<T, T> dataFunc) where T : class
{
string content = result.Message ?? result.ResultType.ToDescription();
AjaxResultType type = result.ResultType.ToAjaxResultType();
T data = null;
if (result.Data != null)
{
if (dataFunc != null && result.Data is T resultData)
{
data = dataFunc(resultData);
}
}
return new AjaxResult<T>(content, type, data);
}

/// <summary>
/// 把业务结果类型<see cref="OperationResultType"/>转换为Ajax结果类型<see cref="AjaxResultType"/>
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="[6.0.11, 7.0.0)" />
Expand Down
11 changes: 11 additions & 0 deletions src/OSharp.EntityFrameworkCore/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// -----------------------------------------------------------------------


using Microsoft.EntityFrameworkCore.Diagnostics;

namespace OSharp.Entity;

/// <summary>
Expand Down Expand Up @@ -49,6 +51,15 @@ public static DbContextOptionsBuilder BuildDbContextOptionsBuilder<TDbContext>(t
throw new OsharpException($"无法找到数据上下文 {dbContextType.DisplayName()} 的配置信息");
}

builder.ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.DetachedLazyLoadingWarning)); ////忽略AsNoTracking 报错

#if DEBUG
builder.UseLoggerFactory((LoggerFactory.Create(options =>
{
options.AddConsole();//开启ef打印sql语句
})));
#endif

ILogger logger = provider.GetLogger(typeof(ServiceExtensions));
//启用延迟加载
if (osharpDbContextOptions.LazyLoadingProxiesEnabled)
Expand Down