diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/AutofacWihtAOP.csproj b/src/AutofacWihtAOP/AutofacWihtAOP/AutofacWihtAOP.csproj
index 76c9f48..f10616a 100644
--- a/src/AutofacWihtAOP/AutofacWihtAOP/AutofacWihtAOP.csproj
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/AutofacWihtAOP.csproj
@@ -56,8 +56,18 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/FieldAttribute.cs b/src/AutofacWihtAOP/AutofacWihtAOP/FieldAttribute.cs
new file mode 100644
index 0000000..d822eb0
--- /dev/null
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/FieldAttribute.cs
@@ -0,0 +1,9 @@
+using System;
+
+namespace AutofacWihtAOP
+{
+ public class FieldAttribute : Attribute
+ {
+ public string Name { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/FunctionAttribute.cs b/src/AutofacWihtAOP/AutofacWihtAOP/FunctionAttribute.cs
new file mode 100644
index 0000000..d5e895f
--- /dev/null
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/FunctionAttribute.cs
@@ -0,0 +1,9 @@
+using System;
+
+namespace AutofacWihtAOP
+{
+ public class FunctionAttribute : Attribute
+ {
+ public string Name { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/ILogService.cs b/src/AutofacWihtAOP/AutofacWihtAOP/ILogService.cs
new file mode 100644
index 0000000..e71a5d1
--- /dev/null
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/ILogService.cs
@@ -0,0 +1,8 @@
+namespace AutofacWihtAOP
+{
+ public interface ILogService
+ {
+ LogModel GetLastLog(LogFilter filter);
+ void AddLog(LogModel model);
+ }
+}
\ No newline at end of file
diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/IUserService.cs b/src/AutofacWihtAOP/AutofacWihtAOP/IUserService.cs
new file mode 100644
index 0000000..6685898
--- /dev/null
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/IUserService.cs
@@ -0,0 +1,7 @@
+namespace AutofacWihtAOP
+{
+ public interface IUserService
+ {
+ void ModifyUserInfo(UserModel model);
+ }
+}
\ No newline at end of file
diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/LogFilter.cs b/src/AutofacWihtAOP/AutofacWihtAOP/LogFilter.cs
new file mode 100644
index 0000000..ad7d3f7
--- /dev/null
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/LogFilter.cs
@@ -0,0 +1,9 @@
+namespace AutofacWihtAOP
+{
+ public class LogFilter
+ {
+ public string FunctionName { get; set; }
+ public string FieldName { get; set; }
+ public string UserCode { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/LogInterceptor.cs b/src/AutofacWihtAOP/AutofacWihtAOP/LogInterceptor.cs
new file mode 100644
index 0000000..540c8cf
--- /dev/null
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/LogInterceptor.cs
@@ -0,0 +1,114 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Runtime.InteropServices;
+using Castle.DynamicProxy;
+
+namespace AutofacWihtAOP
+{
+ public class LogInterceptor : IInterceptor
+ {
+ private ILogService _logService;
+
+ public LogInterceptor(ILogService logservice)
+ {
+ _logService = logservice;
+ }
+
+ public void Intercept(IInvocation invocation)
+ {
+ var models = invocation.Arguments.Where(Ext.IsAttributeType);
+
+ var selectMany = models.SelectMany(x=> x.GetType().GetPropertiesBy(),
+ (model,prop)=> new
+ {
+ CurrentValue = prop.GetValue(model),
+ FieldName = prop.GetAttributeValue((FieldAttribute z) => z.Name),
+ functionName = model.GetType()
+ .GetAttributeValue((FunctionAttribute attr) => attr.Name)
+ });
+
+ foreach (var prop in selectMany)
+ {
+
+ var lastLog = _logService.GetLastLog(new LogFilter()
+ {
+ FieldName = prop.FieldName,
+ FunctionName = prop.functionName
+ });
+
+ var logModel = new LogModel()
+ {
+ UserCode = "Dnaiel",
+ FunctionName = prop.functionName,
+ FieldName = prop.FieldName,
+ NewValue = prop.CurrentValue?.ToString()
+ };
+
+ if (lastLog != null)
+ logModel.OldValue = lastLog.NewValue;
+ else
+ logModel.OldValue = string.Empty;
+
+ _logService.AddLog(logModel);
+ }
+ }
+ }
+
+ public static class Ext
+ {
+ public static IEnumerable GetPropertiesBy
+ (this Type type,bool inherit = true)
+ where T : Attribute
+ {
+ if (type == null)
+ throw new Exception("type can't be null");
+
+
+ return type.GetProperties()
+ .Where(x => x.GetCustomAttribute(inherit) != null);
+ }
+
+ public static bool IsAttributeType(this object obj)
+ where TAttr : Attribute
+ {
+ return IsAttributeType(obj, true);
+ }
+
+ public static bool IsAttributeType(this object obj,bool inherit)
+ where TAttr : Attribute
+ {
+ return obj.GetType()
+ .GetCustomAttribute(inherit) != null;
+ }
+
+ public static TRtn GetAttributeValue(this PropertyInfo prop,
+ Func selector)
+ where TAttr : Attribute
+ {
+ TRtn result = default(TRtn);
+
+ var attr = prop.GetCustomAttribute();
+
+ if (selector == null || attr == null)
+ return result;
+
+ return selector(attr);
+ }
+
+ public static TRtn GetAttributeValue(this Type prop,
+ Func selector)
+ where TAttr : Attribute
+ {
+ TRtn result = default(TRtn);
+
+ var attr = prop.GetCustomAttribute();
+
+ if (selector == null || attr == null)
+ return result;
+
+ return selector(attr);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/LogModel.cs b/src/AutofacWihtAOP/AutofacWihtAOP/LogModel.cs
new file mode 100644
index 0000000..fe42d69
--- /dev/null
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/LogModel.cs
@@ -0,0 +1,19 @@
+using System;
+
+namespace AutofacWihtAOP
+{
+ public class LogModel
+ {
+ public DateTime CreateDate { get; set; } = DateTime.Now;
+
+ public string OldValue { get; set; }
+
+ public string NewValue { get; set; }
+
+ public string UserCode { get; set; }
+
+ public string FunctionName { get; set; }
+
+ public string FieldName { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/LogService.cs b/src/AutofacWihtAOP/AutofacWihtAOP/LogService.cs
new file mode 100644
index 0000000..0c04fc5
--- /dev/null
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/LogService.cs
@@ -0,0 +1,23 @@
+using System.Collections.Generic;
+using System.Linq;
+
+namespace AutofacWihtAOP
+{
+ public class LogService : ILogService
+ {
+ List _list = new List();
+
+ public LogModel GetLastLog(LogFilter filter)
+ {
+ return _list.OrderBy(x => x.CreateDate)
+ .FirstOrDefault(x=>
+ x.FieldName == filter.FieldName &&
+ x.FunctionName == filter.FunctionName );
+ }
+
+ public void AddLog(LogModel model)
+ {
+ _list.Add(model);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/Program.cs b/src/AutofacWihtAOP/AutofacWihtAOP/Program.cs
index f24a75c..42fb6ba 100644
--- a/src/AutofacWihtAOP/AutofacWihtAOP/Program.cs
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/Program.cs
@@ -11,6 +11,8 @@ using Autofac;
using Autofac.Builder;
using Autofac.Extras.DynamicProxy;
using Castle.DynamicProxy;
+using static System.Convert;
+
namespace AutofacWihtAOP
{
public interface ITimeService
@@ -69,6 +71,7 @@ namespace AutofacWihtAOP
class Program
{
+
static void Main(string[] args)
{
@@ -76,9 +79,17 @@ namespace AutofacWihtAOP
IPerson person = container.Resolve();
- Console.WriteLine(person.SaySomething());
- Thread.Sleep(5000);
- Console.WriteLine(person.SaySomething());
+ //Console.WriteLine(person.SaySomething());
+ //Thread.Sleep(5000);
+ //Console.WriteLine(person.SaySomething());
+
+ IUserService personService = container.Resolve();
+
+ personService.ModifyUserInfo(new UserModel()
+ {
+ Birthday = DateTime.Now,
+ Phone = "0911181212"
+ });
Console.ReadKey();
}
@@ -88,11 +99,20 @@ namespace AutofacWihtAOP
var builder = new ContainerBuilder();
builder.RegisterType(); //註冊攔截器
+ builder.RegisterType(); //註冊攔截器
+
+ builder.RegisterType()
+ .As();
builder.RegisterType()
.As()
.EnableInterfaceInterceptors();
+ builder.RegisterType()
+ .As()
+ .EnableInterfaceInterceptors();
+
+
//註冊時間Service
builder.RegisterType().As();
diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/UserModel.cs b/src/AutofacWihtAOP/AutofacWihtAOP/UserModel.cs
new file mode 100644
index 0000000..5a5e29c
--- /dev/null
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/UserModel.cs
@@ -0,0 +1,13 @@
+using System;
+
+namespace AutofacWihtAOP
+{
+ [Function(Name = "Login")]
+ public class UserModel
+ {
+ [Field(Name = "Phone")]
+ public string Phone { get; set; }
+ [Field(Name = "Birthday")]
+ public DateTime Birthday { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/AutofacWihtAOP/AutofacWihtAOP/UserService.cs b/src/AutofacWihtAOP/AutofacWihtAOP/UserService.cs
new file mode 100644
index 0000000..354d469
--- /dev/null
+++ b/src/AutofacWihtAOP/AutofacWihtAOP/UserService.cs
@@ -0,0 +1,13 @@
+using Autofac.Extras.DynamicProxy;
+
+namespace AutofacWihtAOP
+{
+ [Intercept(typeof(LogInterceptor))]
+ public class UserService:IUserService
+ {
+ public void ModifyUserInfo(UserModel model)
+ {
+
+ }
+ }
+}
\ No newline at end of file