Exemplo n.º 1
0
        private void FillDataSets()
        {
            var datasetProps = GetType()
                               .GetProperties()
                               .Where(x =>
                                      x.CanRead &&
                                      x.CanWrite &&
                                      ReflectionHelper.IsPropertyGenericInterfaceDefinition(x, typeof(IDataSet <, ,>)));

            foreach (var prop in datasetProps)
            {
                var genericArgs = prop.PropertyType.GetGenericArguments();
                if (genericArgs[2] != typeof(TKey))
                {
                    continue;
                }

                var datasetType = typeof(DataSet <, ,>).MakeGenericType(genericArgs);

                var dataset = _proxyGeneratorFactory.GetProxyGenerator()
                              .CreateClassProxy(datasetType, new object[] { this, prop.Name }, new DataSetInterceptor(RunConfiguration));

                prop.SetValue(this, dataset);
                _dataSets.Add((IDataSet <TKey>)dataset);
            }
        }
        public object CreateProxy(IMappedStatement selectStatement, object param, object target, ISetAccessor setAccessor)
        {
            Type memberType = setAccessor.MemberType;

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug(string.Format("Statement '{0}', create proxy for member {1}.", selectStatement.Id, setAccessor.MemberType));
            }
            IInterceptor interceptor = new LazyLoadInterceptor(selectStatement, param, target, setAccessor);

            return(ProxyGeneratorFactory.GetProxyGenerator().CreateClassProxy(memberType, interceptor, Type.EmptyTypes));
        }
        /// <summary>
        /// Builds the specified lazy load proxy for a concrete class with virtual method.
        /// </summary>
        /// <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(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 handler = new LazyLoadInterceptor(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 proxy = ProxyGeneratorFactory.GetProxyGenerator().CreateClassProxy(typeProxified, handler, Type.EmptyTypes);

            return(proxy);
        }