Exemple #1
0
        internal static void Proxy(object instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var type = instance.GetType();

            if (EntityUtil.IsPagingResult(type))
            {
                var         ta    = TypeAccessor.GetAccessor(type);
                IEnumerable items = (IEnumerable)ta.GetProperty("Items", instance);
                foreach (var item in items)
                {
                    Proxy(item);
                }
            }
            else if (EntityUtil.IsGenericList(type))
            {
                IEnumerable items = (IEnumerable)instance;
                foreach (var item in items)
                {
                    Proxy(item);
                }
            }
            else if (RepositoryFramework.GetDefineMetadata(type) != null)
            {
                ProxyJoins(instance);
            }
        }
Exemple #2
0
        static void Fetch(object entity, Func <object, bool> cancelFetchFunc)
        {
            if (entity == null)
            {
                return;
            }

            var type     = entity.GetType();
            var metadata = RepositoryFramework.GetDefineMetadata(type);

            if (metadata != null)
            {
                if (metadata.FetchableObject != null)
                {
                    (metadata.FetchableObject).Fetch(entity);
                }

                var joins = metadata.ClassJoinDefines.Values.Cast <ClassJoinDefineMetadata>().Where(o => o.JoinType == MethodJoinType.PropertyGet);
                var ta    = TypeAccessor.GetAccessor(metadata.EntityType);

                foreach (var join in joins)
                {
                    var value = ta.GetProperty(join.JoinName, entity);

                    if (cancelFetchFunc != null && cancelFetchFunc(value))
                    {
                        return;
                    }

                    var entityType = join.Method.ReturnType;

                    if (EntityUtil.IsGenericList(entityType))
                    {
                        var items = (IList)value;
                        for (var i = 0; i < items.Count; i++)
                        {
                            var v  = items[i];
                            var sv = GetSource(v);
                            Fetch(sv, cancelFetchFunc);

                            if (sv != v)
                            {
                                items[i] = sv;
                            }
                        }
                    }
                    else
                    {
                        var sv = GetSource(value);
                        Fetch(sv, cancelFetchFunc);

                        if (value != sv)
                        {
                            ta.SetProperty(join.JoinName, entity, sv);
                        }
                    }
                }
            }
        }
Exemple #3
0
        internal static void ProxyJoins(object entity)
        {
            ClassDefineMetadata metadata = RepositoryFramework.GetDefineMetadataAndCheck(entity.GetType());

            var joins = metadata.ClassJoinDefines.Values.Cast <ClassJoinDefineMetadata>().Where(o => o.JoinType == MethodJoinType.PropertyGet);
            var ta    = TypeAccessor.GetAccessor(metadata.EntityType);

            foreach (var join in joins)
            {
                var value = ta.GetProperty(join.JoinName, entity);

                //已经为代理对象,直接忽略
                if (IsProxy(value))
                {
                    ((IProxy)value).Reset();
                    continue;
                }

                var type = join.Method.ReturnType;

                if (EntityUtil.IsGenericList(type))
                {
                    var itemType           = type.GetGenericArguments()[0];
                    var proxyType          = typeof(ProxyCollection <>).MakeGenericType(itemType);
                    IProxyCollection proxy = (IProxyCollection)FastActivator.Create(proxyType);
                    proxy.Init(entity, join);

                    ta.SetProperty(join.JoinName, entity, proxy);
                }
                else
                {
                    var ei    = new EntityProxyInterceptor(entity, join);
                    var proxy = ProxyProvider.GetCreateFunc(type)(new IInterceptor[] { ei });
                    ei.IsConstructed = true;
                    ta.SetProperty(join.JoinName, entity, proxy);
                }
            }
        }