예제 #1
0
        /// <summary>
        /// Builds the specified lazy load proxy for a concrete class with virtual method.
        /// </summary>
        /// <param name="dataMapper">The data mapper.</param>
        /// <param name="selectStatement">The mapped statement used to build the lazy loaded object.</param>
        /// <param name="param">The parameter object used to build lazy loaded object.</param>
        /// <param name="target">The target object which contains the property proxydied..</param>
        /// <param name="setAccessor">The proxified member accessor.</param>
        /// <returns>Return a proxy object</returns>
        public object CreateProxy(
            IDataMapper dataMapper,
            IMappedStatement selectStatement, object param, 
			object target, ISetAccessor setAccessor)
		{
			Type typeProxified = setAccessor.MemberType;

            if (logger.IsDebugEnabled)
            {
                logger.Debug(string.Format("Statement '{0}', create proxy for member {1}.", selectStatement.Id, setAccessor.MemberType));
            }

            // Build the proxy
            IInterceptor interceptor= new LazyLoadInterceptor(dataMapper, selectStatement, param, target, setAccessor);

            // if you want to proxy concrete classes, there are also 2 main requirements : 
            // 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.

            // The proxified type must also have an empty constructor
            object generatedProxy = proxyGenerator.CreateClassProxy(typeProxified, Type.EmptyTypes, interceptor);

            return generatedProxy;
        }
        /// <summary>
        /// Builds the specified lazy load proxy for a concrete class with virtual method.
        /// </summary>
        /// <param name="dataMapper">The data mapper.</param>
        /// <param name="selectStatement">The mapped statement used to build the lazy loaded object.</param>
        /// <param name="param">The parameter object used to build lazy loaded object.</param>
        /// <param name="target">The target object which contains the property proxydied..</param>
        /// <param name="setAccessor">The proxified member accessor.</param>
        /// <returns>Return a proxy object</returns>
        public object CreateProxy(
            IDataMapper dataMapper,
            IMappedStatement selectStatement, object param,
            object target, ISetAccessor setAccessor)
        {
            Type typeProxified = setAccessor.MemberType;

            if (logger.IsDebugEnabled)
            {
                logger.Debug(string.Format("Statement '{0}', create proxy for member {1}.", selectStatement.Id, setAccessor.MemberType));
            }

            // Build the proxy
            IInterceptor interceptor = new LazyLoadInterceptor(dataMapper, selectStatement, param, target, setAccessor);

            // if you want to proxy concrete classes, there are also 2 main requirements :
            // 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.

            // The proxified type must also have an empty constructor
            object generatedProxy = proxyGenerator.CreateClassProxy(typeProxified, Type.EmptyTypes, interceptor);

            return(generatedProxy);
        }