Skip to content

Commit e6245b1

Browse files
committed
Extracted DelegateHelper from Log4NetLogger
- this will enable the implementation NH-3856 and NH-3857
1 parent a46fb12 commit e6245b1

File tree

3 files changed

+118
-62
lines changed

3 files changed

+118
-62
lines changed

src/NHibernate/Logging.cs

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Linq.Expressions;
6+
using NHibernate.Util;
67

78
namespace NHibernate
89
{
@@ -243,8 +244,8 @@ private static Func<TParameter, object> GetGetLoggerMethodCall<TParameter>()
243244
var method = LogManagerType.GetMethod("GetLogger", new[] { typeof(TParameter) });
244245
ParameterExpression resultValue;
245246
ParameterExpression keyParam = Expression.Parameter(typeof(TParameter), "key");
246-
MethodCallExpression methodCall = Expression.Call(null, method, new Expression[] { resultValue = keyParam });
247-
return Expression.Lambda<Func<TParameter, object>>(methodCall, new[] { resultValue }).Compile();
247+
MethodCallExpression methodCall = Expression.Call(null, method, resultValue = keyParam);
248+
return Expression.Lambda<Func<TParameter, object>>(methodCall, resultValue).Compile();
248249
}
249250
}
250251

@@ -280,66 +281,29 @@ public class Log4NetLogger: IInternalLogger
280281

281282
static Log4NetLogger()
282283
{
283-
IsErrorEnabledDelegate = GetPropertyGetter("IsErrorEnabled");
284-
IsFatalEnabledDelegate = GetPropertyGetter("IsFatalEnabled");
285-
IsDebugEnabledDelegate = GetPropertyGetter("IsDebugEnabled");
286-
IsInfoEnabledDelegate = GetPropertyGetter("IsInfoEnabled");
287-
IsWarnEnabledDelegate = GetPropertyGetter("IsWarnEnabled");
288-
ErrorDelegate = GetMethodCallForMessage("Error");
289-
ErrorExceptionDelegate = GetMethodCallForMessageException("Error");
290-
ErrorFormatDelegate = GetMethodCallForMessageFormat("ErrorFormat");
291-
292-
FatalDelegate = GetMethodCallForMessage("Fatal");
293-
FatalExceptionDelegate = GetMethodCallForMessageException("Fatal");
294-
295-
DebugDelegate = GetMethodCallForMessage("Debug");
296-
DebugExceptionDelegate = GetMethodCallForMessageException("Debug");
297-
DebugFormatDelegate = GetMethodCallForMessageFormat("DebugFormat");
298-
299-
InfoDelegate = GetMethodCallForMessage("Info");
300-
InfoExceptionDelegate = GetMethodCallForMessageException("Info");
301-
InfoFormatDelegate = GetMethodCallForMessageFormat("InfoFormat");
302-
303-
WarnDelegate = GetMethodCallForMessage("Warn");
304-
WarnExceptionDelegate = GetMethodCallForMessageException("Warn");
305-
WarnFormatDelegate = GetMethodCallForMessageFormat("WarnFormat");
306-
}
307-
308-
private static Func<object, bool> GetPropertyGetter(string propertyName)
309-
{
310-
ParameterExpression funcParam = Expression.Parameter(typeof(object), "l");
311-
Expression convertedParam = Expression.Convert(funcParam, ILogType);
312-
Expression property = Expression.Property(convertedParam, propertyName);
313-
return (Func<object, bool>)Expression.Lambda(property, funcParam).Compile();
314-
}
315-
316-
private static Action<object, object> GetMethodCallForMessage(string methodName)
317-
{
318-
ParameterExpression loggerParam = Expression.Parameter(typeof(object), "l");
319-
ParameterExpression messageParam = Expression.Parameter(typeof(object), "o");
320-
Expression convertedParam = Expression.Convert(loggerParam, ILogType);
321-
MethodCallExpression methodCall = Expression.Call(convertedParam, ILogType.GetMethod(methodName, new[] { typeof(object) }), messageParam);
322-
return (Action<object, object>)Expression.Lambda(methodCall, new[] { loggerParam, messageParam }).Compile();
323-
}
324-
325-
private static Action<object, object, Exception> GetMethodCallForMessageException(string methodName)
326-
{
327-
ParameterExpression loggerParam = Expression.Parameter(typeof(object), "l");
328-
ParameterExpression messageParam = Expression.Parameter(typeof(object), "o");
329-
ParameterExpression exceptionParam = Expression.Parameter(typeof(Exception), "e");
330-
Expression convertedParam = Expression.Convert(loggerParam, ILogType);
331-
MethodCallExpression methodCall = Expression.Call(convertedParam, ILogType.GetMethod(methodName, new[] { typeof(object), typeof(Exception) }), messageParam, exceptionParam);
332-
return (Action<object, object, Exception>)Expression.Lambda(methodCall, new[] { loggerParam, messageParam, exceptionParam }).Compile();
333-
}
334-
335-
private static Action<object, string, object[]> GetMethodCallForMessageFormat(string methodName)
336-
{
337-
ParameterExpression loggerParam = Expression.Parameter(typeof(object), "l");
338-
ParameterExpression formatParam = Expression.Parameter(typeof(string), "f");
339-
ParameterExpression parametersParam = Expression.Parameter(typeof(object[]), "p");
340-
Expression convertedParam = Expression.Convert(loggerParam, ILogType);
341-
MethodCallExpression methodCall = Expression.Call(convertedParam, ILogType.GetMethod(methodName, new[] { typeof(string), typeof(object[]) }), formatParam, parametersParam);
342-
return (Action<object, string, object[]>)Expression.Lambda(methodCall, new[] { loggerParam, formatParam, parametersParam }).Compile();
284+
IsErrorEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(ILogType, "IsErrorEnabled");
285+
IsFatalEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(ILogType, "IsFatalEnabled");
286+
IsDebugEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(ILogType, "IsDebugEnabled");
287+
IsInfoEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(ILogType, "IsInfoEnabled");
288+
IsWarnEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(ILogType, "IsWarnEnabled");
289+
ErrorDelegate = DelegateHelper.BuildAction<object>(ILogType, "Error");
290+
ErrorExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(ILogType, "Error");
291+
ErrorFormatDelegate = DelegateHelper.BuildAction<string, object[]>(ILogType, "ErrorFormat");
292+
293+
FatalDelegate = DelegateHelper.BuildAction<object>(ILogType, "Fatal");
294+
FatalExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(ILogType, "Fatal");
295+
296+
DebugDelegate = DelegateHelper.BuildAction<object>(ILogType, "Debug");
297+
DebugExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(ILogType, "Debug");
298+
DebugFormatDelegate = DelegateHelper.BuildAction<string, object[]>(ILogType, "DebugFormat");
299+
300+
InfoDelegate = DelegateHelper.BuildAction<object>(ILogType, "Info");
301+
InfoExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(ILogType, "Info");
302+
InfoFormatDelegate = DelegateHelper.BuildAction<string, object[]>(ILogType, "InfoFormat");
303+
304+
WarnDelegate = DelegateHelper.BuildAction<object>(ILogType, "Warn");
305+
WarnExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(ILogType, "Warn");
306+
WarnFormatDelegate = DelegateHelper.BuildAction<string, object[]>(ILogType, "WarnFormat");
343307
}
344308

345309
public Log4NetLogger(object logger)

src/NHibernate/NHibernate.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<Compile Include="Connection\DriverConnectionProvider.cs" />
142142
<Compile Include="Connection\IConnectionProvider.cs" />
143143
<Compile Include="Connection\UserSuppliedConnectionProvider.cs" />
144+
<Compile Include="Util\DelegateHelper.cs" />
144145
<Compile Include="Dialect\BitwiseFunctionOperation.cs" />
145146
<Compile Include="Dialect\DB2Dialect.cs" />
146147
<Compile Include="Dialect\Dialect.cs" />

src/NHibernate/Util/DelegateHelper.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using System.Reflection;
4+
5+
namespace NHibernate.Util
6+
{
7+
internal static class DelegateHelper
8+
{
9+
public static Func<object, T> BuildPropertyGetter<T>(System.Type type, string propertyName)
10+
{
11+
var parameter = Expression.Parameter(typeof (object), "x");
12+
var instance = Expression.Convert(parameter, type);
13+
var property = Expression.Property(instance, propertyName);
14+
return Expression.Lambda<Func<object, T>>(property, parameter).Compile();
15+
}
16+
17+
public static Action<object, T> BuildPropertySetter<T>(System.Type type, string propertyName)
18+
{
19+
var parameter = Expression.Parameter(typeof(object), "x");
20+
var instance = Expression.Convert(parameter, type);
21+
var property = Expression.Property(instance, propertyName);
22+
var value = Expression.Parameter(typeof (T), "value");
23+
var assign = Expression.Assign(property, value);
24+
return Expression.Lambda<Action<object, T>>(assign, parameter, value).Compile();
25+
}
26+
27+
public static Action<object> BuildAction(System.Type type, string methodName)
28+
{
29+
var parameter = Expression.Parameter(typeof (object));
30+
var instance = Expression.Convert(parameter, type);
31+
var methodCall = Expression.Call(
32+
instance,
33+
GetMethod(type, methodName));
34+
35+
return Expression.Lambda<Action<object>>(methodCall, parameter).Compile();
36+
}
37+
38+
public static Action<object, T> BuildAction<T>(System.Type type, string methodName)
39+
{
40+
var parameter = Expression.Parameter(typeof (object), "x");
41+
var instance = Expression.Convert(parameter, type);
42+
43+
var arg0 = Expression.Parameter(typeof (T), "arg0");
44+
45+
var methodCall = Expression.Call(
46+
instance,
47+
GetMethod(type, methodName, typeof (T)),
48+
arg0);
49+
50+
return Expression.Lambda<Action<object, T>>(methodCall, parameter, arg0).Compile();
51+
}
52+
53+
public static Action<object, T1, T2> BuildAction<T1, T2>(System.Type type, string methodName)
54+
{
55+
var parameter = Expression.Parameter(typeof (object), "x");
56+
var instance = Expression.Convert(parameter, type);
57+
58+
var arg0 = Expression.Parameter(typeof (T1), "arg0");
59+
var arg1 = Expression.Parameter(typeof (T2), "arg1");
60+
61+
var methodCall = Expression.Call(
62+
instance,
63+
GetMethod(type, methodName, typeof (T1), typeof (T2)),
64+
arg0,
65+
arg1);
66+
67+
return Expression.Lambda<Action<object, T1, T2>>(methodCall, parameter, arg0, arg1).Compile();
68+
}
69+
70+
public static Func<object, TReturn> BuildFunc<TReturn>(System.Type type, string methodName)
71+
{
72+
var parameter = Expression.Parameter(typeof (object));
73+
var instance = Expression.Convert(parameter, type);
74+
var methodCall = Expression.Call(
75+
instance,
76+
GetMethod(type, methodName));
77+
78+
return Expression.Lambda<Func<object, TReturn>>(methodCall, parameter).Compile();
79+
}
80+
81+
private static MethodInfo GetMethod(System.Type type, string methodName, params System.Type[] types)
82+
{
83+
return type.GetMethod(
84+
methodName,
85+
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
86+
null,
87+
types,
88+
null);
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)