Index: MappedStatements/PropertStrategy/SelectObjectStrategy.cs =================================================================== --- MappedStatements/PropertStrategy/SelectObjectStrategy.cs (revision 431958) +++ MappedStatements/PropertStrategy/SelectObjectStrategy.cs (working copy) @@ -64,8 +64,16 @@ postSelect.Target = target; postSelect.ResultProperty = mapping; - postSelect.Method = PostBindind.ExecuteMethod.ExecuteQueryForObject; - request.QueueSelect.Enqueue(postSelect); + if (mapping.IsLazyLoad) + { + object values = mapping.LazyFactory.CreateProxy(selectStatement, keys, target, mapping.SetAccessor); + mapping.SetAccessor.Set(target, values); + } + else + { + postSelect.Method = PostBindind.ExecuteMethod.ExecuteQueryForObject; + request.QueueSelect.Enqueue(postSelect); + } } #endregion Index: Proxy/LazyLoadInterceptor.cs =================================================================== --- Proxy/LazyLoadInterceptor.cs (revision 431958) +++ Proxy/LazyLoadInterceptor.cs (working copy) @@ -31,7 +31,9 @@ using System.Reflection; using Castle.DynamicProxy; using IBatisNet.Common.Logging; +using IBatisNet.Common.Utilities.Objects; using IBatisNet.Common.Utilities.Objects.Members; +using IBatisNet.Common.Utilities.Proxy; using IBatisNet.DataMapper.MappedStatements; #if dotnet2 using System.Collections.Generic; @@ -51,10 +53,11 @@ private object _param = null; private object _target = null; private ISetAccessor _setAccessor= null; - private ISqlMapper _sqlMap = null; + private ISqlMapper _sqlMap = null; private string _statementName = string.Empty; private bool _loaded = false; - private IList _innerList = null; + private object _lazyLoadedItem = null; + //private IList _innerList = null; private object _loadLock = new object(); private static ArrayList _passthroughMethods = new ArrayList(); @@ -69,7 +72,6 @@ static LazyLoadInterceptor() { _passthroughMethods.Add("GetType"); - _passthroughMethods.Add("ToString"); } /// @@ -80,13 +82,13 @@ /// The proxified member accessor. /// The target object which contains the property proxydied. internal LazyLoadInterceptor(IMappedStatement mappedSatement, object param, - object target, ISetAccessor setAccessor) + object target, ISetAccessor setAccessor) { _param = param; _statementName = mappedSatement.Id; _sqlMap = mappedSatement.SqlMap; _target = target; - _setAccessor = setAccessor; + _setAccessor = setAccessor; } #endregion @@ -113,13 +115,41 @@ { _logger.Debug("Proxyfying call, query statement " + _statementName); } - _innerList = _sqlMap.QueryForList(_statementName, _param); + + Type typeOfProxified = _setAccessor.MemberType;// _target.GetType().GetProperty(_propertyName).PropertyType; + bool isReturnValueACollection = (typeOfProxified.GetInterface(typeof(ICollection).FullName) != null); + + // //First try load in current session + // IDalSession session = null; + // bool closeConnectionAtEnd = true; + + // if (_mapper.IsSessionStarted) + // { + // session = _mapper.LocalSession; + // closeConnectionAtEnd = false; + // } + // else + // { + // session = _mapper.OpenConnection(); + // closeConnectionAtEnd = true; + // } + + //Perform load + if (isReturnValueACollection) + { + _lazyLoadedItem = _sqlMap.QueryForList(_statementName, _param); + } + else + { + _lazyLoadedItem = _sqlMap.QueryForObject(_statementName, _param); + } + _loaded = true; - _setAccessor.Set(_target, _innerList); + _setAccessor.Set(_target, _lazyLoadedItem); } } - object returnValue = invocation.Method.Invoke( _innerList, arguments); + object returnValue = invocation.Method.Invoke( _lazyLoadedItem, arguments); if (_logger.IsDebugEnabled) { Index: Proxy/LazyLoadProxyFactory.cs =================================================================== --- Proxy/LazyLoadProxyFactory.cs (revision 431958) +++ Proxy/LazyLoadProxyFactory.cs (working copy) @@ -97,7 +97,9 @@ // the class can not be sealed and only virtual methods can be intercepted. // The reason is that DynamicProxy will create a subclass of your class overriding all methods // so it can dispatch the invocations to the interceptor. - proxy = ProxyGeneratorFactory.GetProxyGenerator().CreateClassProxy(typeProxified, handler, Type.EmptyTypes); + //proxy = ProxyGeneratorFactory.GetProxyGenerator().CreateClassProxy(typeProxified, handler, Type.EmptyTypes); + proxy = ProxyGeneratorFactory.GetProxyGenerator().CreateClassProxy(typeProxified, handler,CreateArgumentsForConstructor(typeProxified,param)); + // } //} //else @@ -154,5 +156,34 @@ return returnedType; } + private static object[] CreateArgumentsForConstructor(Type type,Object lazyLoadParam) + { + object[] argumentsForConstructor = null; + + if (type.GetInterface(typeof(ICollection).FullName) != null) + { + //the collection build whitout arguments + argumentsForConstructor = Type.EmptyTypes; + } + else + { + if (lazyLoadParam == null) + { + argumentsForConstructor = Type.EmptyTypes; + } + else if (lazyLoadParam is object[]) + { + //Multiple primary key + argumentsForConstructor = (object[])lazyLoadParam; + } + else + { + argumentsForConstructor = new object[]{lazyLoadParam}; + } + } + + return argumentsForConstructor; + } + } }