忽略
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Hncore.Infrastructure.Service
|
||||
{
|
||||
public static class IServiceCollectionExtension
|
||||
{
|
||||
public static void AddServiceClient(this IServiceCollection service, string baseUrl)
|
||||
{
|
||||
ServiceHttpClient._BaseUrl = baseUrl;
|
||||
|
||||
service.AddSingleton<ServiceHttpClient>();
|
||||
}
|
||||
}
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Hncore.Infrastructure.Service
|
||||
{
|
||||
public static class IServiceCollectionExtension
|
||||
{
|
||||
public static void AddServiceClient(this IServiceCollection service, string baseUrl)
|
||||
{
|
||||
ServiceHttpClient._BaseUrl = baseUrl;
|
||||
|
||||
service.AddSingleton<ServiceHttpClient>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,205 +1,205 @@
|
||||
using Hncore.Infrastructure.Data;
|
||||
using Hncore.Infrastructure.DDD;
|
||||
using Hncore.Infrastructure.EF;
|
||||
using Hncore.Infrastructure.EntitiesExtension;
|
||||
using Hncore.Infrastructure.WebApi;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Service
|
||||
{
|
||||
|
||||
public interface IFindService
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class ServiceBase<TEntity> : IFindService where TEntity : class
|
||||
{
|
||||
IHttpContextAccessor m_HttpContextAccessor;
|
||||
|
||||
public HttpContext HttpContext
|
||||
{
|
||||
get => m_HttpContextAccessor.HttpContext;
|
||||
}
|
||||
public ManageUserInfo ManagerInfo
|
||||
{
|
||||
get => HttpContext.Request.GetManageUserInfo();
|
||||
}
|
||||
protected DbContextBase m_DbContextBase { get; set; }
|
||||
public ServiceBase(DbContextBase dbContext, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
m_DbContextBase = dbContext;
|
||||
m_HttpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
|
||||
public virtual async Task<TEntity> GetById(object id)
|
||||
{
|
||||
return await m_DbContextBase.Set<TEntity>().FindByIdAsync(id);
|
||||
}
|
||||
|
||||
public virtual async Task<TEntity> Add(TEntity entity, bool autoSave = true)
|
||||
{
|
||||
var ret = await m_DbContextBase.Set<TEntity>().AddAsync(entity);
|
||||
if (autoSave)
|
||||
await m_DbContextBase.SaveChangesAsync();
|
||||
return ret.Entity;
|
||||
}
|
||||
public virtual async Task Adds(IEnumerable<TEntity> entitys, bool autoSave = true)
|
||||
{
|
||||
await m_DbContextBase.Set<TEntity>().AddRangeAsync(entitys);
|
||||
if (autoSave)
|
||||
await m_DbContextBase.SaveChangesAsync();
|
||||
}
|
||||
public virtual async Task<bool> DeleteById(object id, bool autoSave = true)
|
||||
{
|
||||
var entity = await this.GetById(id);
|
||||
return await Delete(entity, autoSave);
|
||||
}
|
||||
|
||||
public virtual async Task<bool> Delete(TEntity entity, bool autoSave = true)
|
||||
{
|
||||
//if (entity is ITenant)
|
||||
//{
|
||||
// var tenantId = HttpContext.Request.GetManageUserInfo()?.TenantId;
|
||||
// if (tenantId.HasValue && tenantId > 0 && (entity as ITenant).TenantId != tenantId)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
//}
|
||||
|
||||
if (entity is ISoftDelete)
|
||||
{
|
||||
(entity as ISoftDelete).DeleteTag = 1;
|
||||
m_DbContextBase.Set<TEntity>().Update(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_DbContextBase.Set<TEntity>().Remove(entity);
|
||||
}
|
||||
if (autoSave)
|
||||
await m_DbContextBase.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual async Task<bool> Deletes(IEnumerable<TEntity> entitys, bool autoSave = true)
|
||||
{
|
||||
if (entitys == null || entitys.Count() == 0)
|
||||
return false;
|
||||
|
||||
var entity = entitys.FirstOrDefault();
|
||||
|
||||
if (entity is ISoftDelete)
|
||||
{
|
||||
foreach(var item in entitys) { (entity as ISoftDelete).DeleteTag = 1; }
|
||||
m_DbContextBase.Set<TEntity>().UpdateRange(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_DbContextBase.Set<TEntity>().RemoveRange(entity);
|
||||
}
|
||||
if (autoSave)
|
||||
await m_DbContextBase.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public virtual async Task<bool> Update(TEntity entity, bool autoSave = true)
|
||||
{
|
||||
//if (entity is ITenant)
|
||||
//{
|
||||
// var tenantId = HttpContext.Request.GetManageUserInfo()?.TenantId;
|
||||
// if (tenantId.HasValue && tenantId > 0 && (entity as ITenant).TenantId != tenantId)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
//}
|
||||
m_DbContextBase.Set<TEntity>().Update(entity);
|
||||
if (autoSave)
|
||||
return (await m_DbContextBase.SaveChangesAsync()) > 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual async Task<bool> Update(IEnumerable<TEntity> list, bool autoSave = true)
|
||||
{
|
||||
m_DbContextBase.Set<TEntity>().UpdateRange(list);
|
||||
if (autoSave)
|
||||
await m_DbContextBase.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
public virtual IQueryable<TEntity> Query(bool noTracking = false)
|
||||
{
|
||||
var ret = m_DbContextBase.Set<TEntity>().AsQueryable();
|
||||
if (noTracking)
|
||||
{
|
||||
ret = ret.AsNoTracking();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
public async virtual Task<List<TEntity>> GetAll(bool noTracking = false)
|
||||
{
|
||||
var ret = m_DbContextBase.Set<TEntity>().AsQueryable();
|
||||
if (noTracking)
|
||||
{
|
||||
ret = ret.AsNoTracking();
|
||||
}
|
||||
return await ret.ToListAsync();
|
||||
}
|
||||
public virtual IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> exp=null, bool noTracking = false)
|
||||
{
|
||||
var ret = m_DbContextBase.Set<TEntity>().AsQueryable();
|
||||
if (exp != null)
|
||||
{
|
||||
ret = ret.Where(exp);
|
||||
}
|
||||
if (noTracking)
|
||||
{
|
||||
ret = ret.AsNoTracking();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
public virtual async Task<PageData<TEntity>> Page(int page, int limit, Expression<Func<TEntity, bool>> exp = null, bool noTracking = false)
|
||||
{
|
||||
var ret = await Query(exp, noTracking).ListPagerAsync(limit, page, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public virtual async Task<PageData<TEntity>> PageDesc<TOrderKey>(int page, int limit, Expression<Func<TEntity, bool>> exp = null, bool noTracking = false, Expression<Func<TEntity, TOrderKey>> order = null)
|
||||
{
|
||||
|
||||
var query = Query(exp, noTracking);
|
||||
if (order != null)
|
||||
query = query.OrderByDescending(order);
|
||||
|
||||
var ret = await query.ListPagerAsync(limit, page, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public virtual async Task<PageData<TEntity>> PageAsc<TOrderKey>(int page, int limit, Expression<Func<TEntity, bool>> exp = null, bool noTracking = false, Expression<Func<TEntity, TOrderKey>> order = null)
|
||||
{
|
||||
|
||||
var query = Query(exp, noTracking);
|
||||
if (order != null)
|
||||
query = query.OrderBy(order);
|
||||
|
||||
var ret = await query.ListPagerAsync(limit, page, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public virtual bool Exist(Expression<Func<TEntity, bool>> expr)
|
||||
{
|
||||
return this.Query(expr).Count() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> Save()
|
||||
{
|
||||
return (await m_DbContextBase.SaveChangesAsync()) > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
using Hncore.Infrastructure.Data;
|
||||
using Hncore.Infrastructure.DDD;
|
||||
using Hncore.Infrastructure.EF;
|
||||
using Hncore.Infrastructure.EntitiesExtension;
|
||||
using Hncore.Infrastructure.WebApi;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Service
|
||||
{
|
||||
|
||||
public interface IFindService
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class ServiceBase<TEntity> : IFindService where TEntity : class
|
||||
{
|
||||
IHttpContextAccessor m_HttpContextAccessor;
|
||||
|
||||
public HttpContext HttpContext
|
||||
{
|
||||
get => m_HttpContextAccessor.HttpContext;
|
||||
}
|
||||
public ManageUserInfo ManagerInfo
|
||||
{
|
||||
get => HttpContext.Request.GetManageUserInfo();
|
||||
}
|
||||
protected DbContextBase m_DbContextBase { get; set; }
|
||||
public ServiceBase(DbContextBase dbContext, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
m_DbContextBase = dbContext;
|
||||
m_HttpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
|
||||
public virtual async Task<TEntity> GetById(object id)
|
||||
{
|
||||
return await m_DbContextBase.Set<TEntity>().FindByIdAsync(id);
|
||||
}
|
||||
|
||||
public virtual async Task<TEntity> Add(TEntity entity, bool autoSave = true)
|
||||
{
|
||||
var ret = await m_DbContextBase.Set<TEntity>().AddAsync(entity);
|
||||
if (autoSave)
|
||||
await m_DbContextBase.SaveChangesAsync();
|
||||
return ret.Entity;
|
||||
}
|
||||
public virtual async Task Adds(IEnumerable<TEntity> entitys, bool autoSave = true)
|
||||
{
|
||||
await m_DbContextBase.Set<TEntity>().AddRangeAsync(entitys);
|
||||
if (autoSave)
|
||||
await m_DbContextBase.SaveChangesAsync();
|
||||
}
|
||||
public virtual async Task<bool> DeleteById(object id, bool autoSave = true)
|
||||
{
|
||||
var entity = await this.GetById(id);
|
||||
return await Delete(entity, autoSave);
|
||||
}
|
||||
|
||||
public virtual async Task<bool> Delete(TEntity entity, bool autoSave = true)
|
||||
{
|
||||
//if (entity is ITenant)
|
||||
//{
|
||||
// var tenantId = HttpContext.Request.GetManageUserInfo()?.TenantId;
|
||||
// if (tenantId.HasValue && tenantId > 0 && (entity as ITenant).TenantId != tenantId)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
//}
|
||||
|
||||
if (entity is ISoftDelete)
|
||||
{
|
||||
(entity as ISoftDelete).DeleteTag = 1;
|
||||
m_DbContextBase.Set<TEntity>().Update(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_DbContextBase.Set<TEntity>().Remove(entity);
|
||||
}
|
||||
if (autoSave)
|
||||
await m_DbContextBase.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual async Task<bool> Deletes(IEnumerable<TEntity> entitys, bool autoSave = true)
|
||||
{
|
||||
if (entitys == null || entitys.Count() == 0)
|
||||
return false;
|
||||
|
||||
var entity = entitys.FirstOrDefault();
|
||||
|
||||
if (entity is ISoftDelete)
|
||||
{
|
||||
foreach(var item in entitys) { (entity as ISoftDelete).DeleteTag = 1; }
|
||||
m_DbContextBase.Set<TEntity>().UpdateRange(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_DbContextBase.Set<TEntity>().RemoveRange(entity);
|
||||
}
|
||||
if (autoSave)
|
||||
await m_DbContextBase.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public virtual async Task<bool> Update(TEntity entity, bool autoSave = true)
|
||||
{
|
||||
//if (entity is ITenant)
|
||||
//{
|
||||
// var tenantId = HttpContext.Request.GetManageUserInfo()?.TenantId;
|
||||
// if (tenantId.HasValue && tenantId > 0 && (entity as ITenant).TenantId != tenantId)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
//}
|
||||
m_DbContextBase.Set<TEntity>().Update(entity);
|
||||
if (autoSave)
|
||||
return (await m_DbContextBase.SaveChangesAsync()) > 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual async Task<bool> Update(IEnumerable<TEntity> list, bool autoSave = true)
|
||||
{
|
||||
m_DbContextBase.Set<TEntity>().UpdateRange(list);
|
||||
if (autoSave)
|
||||
await m_DbContextBase.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
public virtual IQueryable<TEntity> Query(bool noTracking = false)
|
||||
{
|
||||
var ret = m_DbContextBase.Set<TEntity>().AsQueryable();
|
||||
if (noTracking)
|
||||
{
|
||||
ret = ret.AsNoTracking();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
public async virtual Task<List<TEntity>> GetAll(bool noTracking = false)
|
||||
{
|
||||
var ret = m_DbContextBase.Set<TEntity>().AsQueryable();
|
||||
if (noTracking)
|
||||
{
|
||||
ret = ret.AsNoTracking();
|
||||
}
|
||||
return await ret.ToListAsync();
|
||||
}
|
||||
public virtual IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> exp=null, bool noTracking = false)
|
||||
{
|
||||
var ret = m_DbContextBase.Set<TEntity>().AsQueryable();
|
||||
if (exp != null)
|
||||
{
|
||||
ret = ret.Where(exp);
|
||||
}
|
||||
if (noTracking)
|
||||
{
|
||||
ret = ret.AsNoTracking();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
public virtual async Task<PageData<TEntity>> Page(int page, int limit, Expression<Func<TEntity, bool>> exp = null, bool noTracking = false)
|
||||
{
|
||||
var ret = await Query(exp, noTracking).ListPagerAsync(limit, page, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public virtual async Task<PageData<TEntity>> PageDesc<TOrderKey>(int page, int limit, Expression<Func<TEntity, bool>> exp = null, bool noTracking = false, Expression<Func<TEntity, TOrderKey>> order = null)
|
||||
{
|
||||
|
||||
var query = Query(exp, noTracking);
|
||||
if (order != null)
|
||||
query = query.OrderByDescending(order);
|
||||
|
||||
var ret = await query.ListPagerAsync(limit, page, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public virtual async Task<PageData<TEntity>> PageAsc<TOrderKey>(int page, int limit, Expression<Func<TEntity, bool>> exp = null, bool noTracking = false, Expression<Func<TEntity, TOrderKey>> order = null)
|
||||
{
|
||||
|
||||
var query = Query(exp, noTracking);
|
||||
if (order != null)
|
||||
query = query.OrderBy(order);
|
||||
|
||||
var ret = await query.ListPagerAsync(limit, page, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public virtual bool Exist(Expression<Func<TEntity, bool>> expr)
|
||||
{
|
||||
return this.Query(expr).Count() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> Save()
|
||||
{
|
||||
return (await m_DbContextBase.SaveChangesAsync()) > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
using Hncore.Infrastructure.WebApi;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Hncore.Infrastructure.Service
|
||||
{
|
||||
public class ServiceHttpClient
|
||||
{
|
||||
private IHttpClientFactory _httpClientFactory;
|
||||
|
||||
internal static string _BaseUrl = "";
|
||||
|
||||
public string BaseUrl => _BaseUrl;
|
||||
|
||||
public ServiceHttpClient(IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
public HttpClient CreateHttpClient()
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
client.BaseAddress = new System.Uri(_BaseUrl);
|
||||
return client;
|
||||
}
|
||||
|
||||
public HttpClient CreateInternalClient()
|
||||
{
|
||||
var client = _httpClientFactory.CreateInternalAuthClient();
|
||||
client.BaseAddress = new System.Uri(_BaseUrl);
|
||||
return client;
|
||||
|
||||
}
|
||||
}
|
||||
using Hncore.Infrastructure.WebApi;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Hncore.Infrastructure.Service
|
||||
{
|
||||
public class ServiceHttpClient
|
||||
{
|
||||
private IHttpClientFactory _httpClientFactory;
|
||||
|
||||
internal static string _BaseUrl = "";
|
||||
|
||||
public string BaseUrl => _BaseUrl;
|
||||
|
||||
public ServiceHttpClient(IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
public HttpClient CreateHttpClient()
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
client.BaseAddress = new System.Uri(_BaseUrl);
|
||||
return client;
|
||||
}
|
||||
|
||||
public HttpClient CreateInternalClient()
|
||||
{
|
||||
var client = _httpClientFactory.CreateInternalAuthClient();
|
||||
client.BaseAddress = new System.Uri(_BaseUrl);
|
||||
return client;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,26 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Hncore.Infrastructure.Service
|
||||
{
|
||||
public static class ServiceIOCExt
|
||||
{
|
||||
public static IServiceCollection AutoAddService(this IServiceCollection service, Type fromType = null)
|
||||
{
|
||||
if (fromType == null)
|
||||
fromType = typeof(IFindService);
|
||||
|
||||
var types = Assembly.GetCallingAssembly().GetTypes();
|
||||
|
||||
types = types.Where(m => fromType.IsAssignableFrom(m)).ToArray();
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
service.AddScoped(type);
|
||||
}
|
||||
return service;
|
||||
}
|
||||
}
|
||||
}
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Hncore.Infrastructure.Service
|
||||
{
|
||||
public static class ServiceIOCExt
|
||||
{
|
||||
public static IServiceCollection AutoAddService(this IServiceCollection service, Type fromType = null)
|
||||
{
|
||||
if (fromType == null)
|
||||
fromType = typeof(IFindService);
|
||||
|
||||
var types = Assembly.GetCallingAssembly().GetTypes();
|
||||
|
||||
types = types.Where(m => fromType.IsAssignableFrom(m)).ToArray();
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
service.AddScoped(type);
|
||||
}
|
||||
return service;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user