Пример #1
0
        public override void Serialize(IDatabaseSettings settings, EntitySerializer serializer, Relation rel, object instanceEntity, PropertyAccessor pInfo, EntityMap entityMap, EntityAccessor entityAccessor, DbDataReader dbReader)
        {
            var relMap  = settings.Map.GetEntityMap(rel.ReferenceEntityName);
            var relCols = new List <string>();
            var session = serializer.SessionCreator();

            int iteration = 0;
            int recursion = 0;

            var relQueryMap = new TableQueryMap(relMap.FullName, ref recursion, ref iteration);

            QueryBuilder q = new QueryBuilder(session, relCols);

            relQueryMap.LoadColumns(relMap, session, q, relCols);
            var prop = entityMap.FirstOrDefault(c => c.ColumnName == rel.ColumnName);

            if (prop == null)
            {
                throw new GoliathDataException(string.Format("{0}: Reference {1} does not have matching property.", entityMap.FullName, rel.PropertyName));
            }

            var valAccessor = entityAccessor.GetPropertyAccessor(prop.PropertyName);
            var val         = valAccessor.GetMethod(instanceEntity);

            var queryBuilder = q.From(relMap.TableName, relQueryMap.Prefix)
                               .Where(rel.ReferenceColumn).EqualToValue(val);

            IProxyHydrator hydrator = new ProxyHydrator(queryBuilder as QueryBuilder, pInfo.PropertyType, relMap, serializer, session);

            ProxyBuilder pbuilder  = new ProxyBuilder();
            var          proxyType = pbuilder.CreateProxyType(pInfo.PropertyType, relMap);
            object       proxyobj  = Activator.CreateInstance(proxyType, new object[] { pInfo.PropertyType, hydrator });

            pInfo.SetMethod(instanceEntity, proxyobj);
        }
Пример #2
0
        private TypeDefinition CreateProxy(ProxyBuilder proxyBuilder,
            Action<TypeCodeGenInfo, TypeDefinition> onTypeGenerated,
            TypeCodeGenInfo typeInfo,
            Dictionary<TypeCodeGenInfo, TypeDefinition> generatedTypeDict)
        {
            var targetType = typeInfo.TransformedType;
            var name = targetType.Name;

            TypeDefinition baseTypeDef = null;
            var tt = typeInfo.TransformedType;
            var rt = typeInfo.TransformedType as ResourceType;
            if (rt != null && rt.UriBaseType != null && rt.UriBaseType != rt)
            {
                var baseTypeInfo = this.clientTypeInfoDict[tt.BaseType];
                baseTypeDef = generatedTypeDict.GetOrCreate(baseTypeInfo,
                    () =>
                        CreateProxy(proxyBuilder,
                            onTypeGenerated,
                            baseTypeInfo,
                            generatedTypeDict));
            }
            var proxyType = proxyBuilder.CreateProxyType(name, typeInfo.InterfaceType.WrapAsEnumerable(), baseTypeDef);

            if (onTypeGenerated != null)
                onTypeGenerated(typeInfo, proxyType);

            return proxyType;
        }