Commit 84fdcc16 authored by 黄奎's avatar 黄奎

新增页面

parent 9ecc86f5
using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Text;
namespace Mall.AOP
{
/// <summary>
/// AOP帮助类
/// </summary>
public class AOPHelper
{
/// <summary>
/// 生成代理类对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T CreateAOPObject<T>() where T : class
{
ProxyGenerator generator = new ProxyGenerator();
IOCInterceptor interceptor = new IOCInterceptor();
T t = generator.CreateClassProxy<T>(interceptor);
return t;
}
}
}
using Castle.DynamicProxy;
using Mall.AOP.CustomerAttribute;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Transactions;
using System.Linq;
namespace Mall.AOP
{
/// <summary>
/// AOP属性基类
/// </summary>
public abstract class BaseInterceptorAttribute : Attribute
{
/// <summary>
/// 具体执行的方法
/// </summary>
public abstract Action Do(IInvocation invocation,Action action);
}
}
using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Text;
namespace Mall.AOP.CustomerAttribute
{
/// <summary>
/// 日志属性
/// </summary>
public class LogAttribute : BaseInterceptorAttribute
{
public override Action Do(IInvocation invocation, Action action)
{
return () =>
{
Console.WriteLine("LogAttribute1");
action.Invoke();
Console.WriteLine("LogAttribute2");
};
}
}
}
using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
namespace Mall.AOP.CustomerAttribute
{
/// <summary>
/// 事务属性
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = true)]
public class TransactionCallHandlerAttribute : BaseInterceptorAttribute
{
/// <summary>
/// 超时时间
/// </summary>
public int Timeout { get; set; }
/// <summary>
/// 事务范围
/// </summary>
public TransactionScopeOption ScopeOption { get; set; }
/// <summary>
/// 事务隔离级别
/// </summary>
public IsolationLevel IsolationLevel { get; set; }
/// <summary>
/// 构造函数
/// </summary>
public TransactionCallHandlerAttribute()
{
Timeout = 60;
ScopeOption = TransactionScopeOption.Required;
IsolationLevel = IsolationLevel.ReadCommitted;
}
/// <summary>
/// 执行方法
/// </summary>
/// <param name="invocation"></param>
/// <param name="action"></param>
/// <returns></returns>
public override Action Do(IInvocation invocation, Action action)
{
return () =>
{
Console.WriteLine("TransactionCallHandlerAttribute1");
TransactionOptions transactionOptions = new TransactionOptions();
//设置事务隔离级别
transactionOptions.IsolationLevel = this.IsolationLevel;
//设置事务超时时间为60秒
transactionOptions.Timeout = new TimeSpan(0, 0, this.Timeout);
using (TransactionScope scope = new TransactionScope(this.ScopeOption, transactionOptions))
{
try
{
//实现事务性工作
action.Invoke();
var result = invocation.ReturnValue;
bool.TryParse((result != null ? result.ToString() : ""),out bool IsSuccess);
if (IsSuccess)
{
scope.Complete();
}
}
catch (Exception ex)
{
// 记录异常
throw ex;
}
}
Console.WriteLine("TransactionCallHandlerAttribute2");
};
}
}
}

using System;
using System.Collections.Generic;
namespace Mall.AOP
{
/// <summary>
/// 拦截器基类
/// </summary>
public class IOCInterceptor : Castle.DynamicProxy.StandardInterceptor
{
/// <summary>
/// 方法调用前执行
/// </summary>
/// <param name="invocation"></param>
protected override void PreProceed(Castle.DynamicProxy.IInvocation invocation)
{
Console.WriteLine("调用前的拦截器, 方法名是: {0}", invocation.Method.Name);
}
/// <summary>
/// 方法执行时调用
/// </summary>
/// <param name="invocation"></param>
protected override void PerformProceed(Castle.DynamicProxy.IInvocation invocation)
{
var method = invocation.Method;
Action action = () => base.PerformProceed(invocation);
if (method.IsDefined(typeof(BaseInterceptorAttribute), true))
{
foreach (var attribute in method.GetCustomAttributes(typeof(BaseInterceptorAttribute), true))
{
var attr = (BaseInterceptorAttribute)attribute;
action= attr.Do(invocation,action);
}
}
action.Invoke();
Console.WriteLine("拦截的方法返回时调用的拦截, 方法名是: {0}", invocation.Method.Name);
}
/// <summary>
/// 方法执行后调用
/// </summary>
/// <param name="invocation"></param>
protected override void PostProceed(Castle.DynamicProxy.IInvocation invocation)
{
Console.WriteLine("调用后的拦截器, 方法名是: {0}", invocation.Method.Name);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Castle.Core" Version="4.4.1" />
</ItemGroup>
</Project>
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using StackExchange.Redis;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mall.CacheManager.Base
{
/// <summary>
/// ConnectionMultiplexer对象管理帮助类
/// </summary>
public static class RedisConnectionHelp
{
/// <summary>
/// 系统自定义Key前缀
/// </summary>
public static readonly string SysCustomKey = "";
/// <summary>
/// 连接字符串
/// </summary>
private static readonly string RedisConnectionString = $@"{new ConfigurationBuilder().Add(new JsonConfigurationSource { Path = "appsettings.json" }).Build().GetSection("RedisSetting")["RedisServer"]}:{new ConfigurationBuilder().Add(new JsonConfigurationSource { Path = "appsettings.json" }).Build().GetSection("RedisSetting")["RedisPort"]},allowadmin=true,password={new ConfigurationBuilder().Add(new JsonConfigurationSource { Path = "appsettings.json" }).Build().GetSection("RedisSetting")["RedisPwd"]}";
private static readonly object Locker = new object();
private static ConnectionMultiplexer _instance;
/// <summary>
/// 线程安全的字典
/// </summary>
private static readonly ConcurrentDictionary<string, ConnectionMultiplexer> ConnectionCache = new ConcurrentDictionary<string, ConnectionMultiplexer>();
/// <summary>
/// 单例获取
/// </summary>
public static ConnectionMultiplexer Instance
{
get
{
if (_instance == null)
{
lock (Locker)
{
if (_instance == null || !_instance.IsConnected)
{
_instance = GetManager();
}
}
}
return _instance;
}
}
/// <summary>
/// 缓存获取
/// </summary>
/// <param name="connectionString"></param>
/// <returns></returns>
public static ConnectionMultiplexer GetConnectionMultiplexer(string connectionString)
{
if (!ConnectionCache.ContainsKey(connectionString))
{
ConnectionCache[connectionString] = GetManager(connectionString);
}
return ConnectionCache[connectionString];
}
/// <summary>
/// 获取连接
/// </summary>
/// <param name="connectionString"></param>
/// <returns></returns>
private static ConnectionMultiplexer GetManager(string connectionString = null)
{
connectionString = connectionString ?? RedisConnectionString;
var connect = ConnectionMultiplexer.Connect(connectionString);
//注册如下事件
connect.ConnectionFailed += MuxerConnectionFailed;
connect.ConnectionRestored += MuxerConnectionRestored;
connect.ErrorMessage += MuxerErrorMessage;
connect.ConfigurationChanged += MuxerConfigurationChanged;
connect.HashSlotMoved += MuxerHashSlotMoved;
connect.InternalError += MuxerInternalError;
connect.ConfigurationChangedBroadcast += MuxerConfigurationChangedBroadcast;
return connect;
}
#region 事件
/// <summary>
/// 重新配置广播时(通常意味着主从同步更改)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerConfigurationChangedBroadcast(object sender, EndPointEventArgs e)
{
Console.WriteLine($"{nameof(MuxerConfigurationChangedBroadcast)}: {e.EndPoint}");
}
/// <summary>
/// 配置更改时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
{
Console.WriteLine("Configuration changed: " + e.EndPoint);
}
/// <summary>
/// 发生内部错误时(主要用于调试)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
{
Console.WriteLine("ErrorMessage: " + e.Message);
}
/// <summary>
/// 重新建立连接之前的错误
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
{
Console.WriteLine("ConnectionRestored: " + e.EndPoint);
}
/// <summary>
/// 连接失败 , 如果重新连接成功你将不会收到这个通知
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
{
Console.WriteLine("重新连接:Endpoint failed: " + e.EndPoint + ", " + e.FailureType + (e.Exception == null ? "" : (", " + e.Exception.Message)));
}
/// <summary>
/// 更改集群时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
{
Console.WriteLine("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint);
}
/// <summary>
/// redis类库错误
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerInternalError(object sender, InternalErrorEventArgs e)
{
Console.WriteLine("InternalError:Message" + e.Exception.Message);
}
#endregion
}
}
This diff is collapsed.
using System;
using System.Collections.Generic;
using System.Text;
using VT.FW.DB;
namespace Mall.Repository
{
public class BaseRepository<T> : RepositoryBase<T> where T : class
{
/// <summary>
/// 数据库连接Key
/// </summary>
public string ConnKey { get; set; }
public BaseRepository(string connKey = "DefaultConnection")
{
try
{
var classAttribute = (DBAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(DBAttribute));
connKey = classAttribute.ConnectionName ?? "DefaultConnection";
}
catch (Exception ex)
{
throw ex;
}
ConnKey = connKey;
}
public string _ConnectionStr;
public string _ProviderName;
public override string ConnectionStr
{
get
{
_ConnectionStr = Common.Config.GetConnectionString(ConnKey);
return _ConnectionStr;
}
set
{
_ConnectionStr = value;
}
}
public override string ProviderName
{
get
{
_ProviderName = "MySql.Data.MySqlClient";
return _ProviderName;
}
set
{
_ProviderName = value;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment