-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added lazy loading functionality Node Mapper using Castle Dynamic proxy.
- Loading branch information
Sony Arouje
committed
Feb 21, 2012
1 parent
d916c92
commit df8c77d
Showing
11 changed files
with
131 additions
and
26 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Reflection; | ||
using Castle.Core; | ||
using Castle.DynamicProxy; | ||
using Castle.Core.Internal; | ||
|
||
namespace Net.Graph.Neo4JD.EntityMapper | ||
{ | ||
public class LazyLoadInterceptor:IInterceptor | ||
{ | ||
public void Intercept(IInvocation invocation) | ||
{ | ||
bool isIntercepted = false; | ||
string propName = invocation.Method.Name.Replace("get_", ""); | ||
|
||
if (invocation.Method.Name.StartsWith("get_", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
Type type = invocation.TargetType.GetProperty(propName).PropertyType; | ||
if (MapperHelper.IsPrimitive(type) == false) | ||
{ | ||
isIntercepted = true; | ||
invocation.Proceed(); | ||
if (invocation.ReturnValue == null) | ||
invocation.ReturnValue = this.DoLazyLoad(type, invocation.Proxy, propName); | ||
|
||
Console.WriteLine(type.ToString()); | ||
} | ||
} | ||
if (isIntercepted == false) | ||
invocation.Proceed(); | ||
} | ||
|
||
private object DoLazyLoad(Type type, object entity, string propertyName) | ||
{ | ||
var castedEntity = entity as IProxyTargetAccessor; | ||
Type genericType = type.IsGenericType ? type.GetGenericArguments()[0] : type; | ||
MethodInfo method = typeof(NodeMapper).GetMethod("LoadRelatedEntitiesWithId"); | ||
MethodInfo generic = method.MakeGenericMethod(genericType); | ||
int id = MapperHelper.GetIdentity(castedEntity.DynProxyGetTarget()); | ||
|
||
object result = generic.Invoke(null, new object[] { id, propertyName }); | ||
|
||
if (type.Namespace != "System.Collections.Generic") | ||
result= ((IEnumerable)result).Cast<object>().ToArray().FirstOrDefault(); | ||
|
||
return result; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Castle.DynamicProxy; | ||
namespace Net.Graph.Neo4JD.EntityMapper | ||
{ | ||
public static class LazyLoader | ||
{ | ||
private static readonly ProxyGenerator _generator=new ProxyGenerator(); | ||
public static T EnableLazyLoading<T>() where T:class | ||
{ | ||
LazyLoadInterceptor interceptor = new LazyLoadInterceptor(); | ||
var proxy = _generator.CreateClassProxy<T>(interceptor); | ||
return proxy; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<ProjectView>ProjectFiles</ProjectView> | ||
</PropertyGroup> | ||
</Project> |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters