Exemplo n.º 1
0
        public static Type CreateView(BelongsToAttribute bta) => Cache.GetOrAdd(bta /*jo kulcsnak*/, () =>
        {
            Type dataTable = bta.OrmType;

            PropertyInfo
            pk     = dataTable.GetPrimaryKey(),
            column = bta.OrmType.GetProperty(bta.Column) ?? throw new MissingMemberException(bta.OrmType.Name, bta.Column);

            //
            // [View(Base = typeof(Table)), MapFrom(nameof(Column))]
            // class Table_Column_View
            // {
            //   [BelongsTo(typeof(Table))]
            //   public int Id {get; set;}
            //   [BelongsTo(typeof(Table))]
            //   public ValueType Column {get; set;}
            // }
            //

            return(CreateView
                   (
                       new MemberDefinition
                       (
                           //
                           // A hash kod kell a tipus nevebe mivel ugyanazon oszlophoz tartozo erteklista szerepelhet tobb nezetben is
                           // kulonbozo "required" ertekkel.
                           //

                           $"{dataTable.Name}_{column.Name}_View_{bta.GetHashCode()}", // TODO: FIXME: bta.GetHashCode() gyanusan sokszor ad vissza 0-t
                           dataTable,
                           CustomAttributeBuilderFactory.CreateFrom <MapFromAttribute>(new[] { typeof(string) }, new object[] { column.Name })
                       ),
                       new[]
            {
                //
                // [BelongsTo(typeof(Table))]
                // public int Id {get; set;}
                //

                new MemberDefinition
                (
                    pk.Name,
                    pk.PropertyType,
                    CustomAttributeBuilderFactory.CreateFrom(new BelongsToAttribute(dataTable, bta.Required))
                ),

                //
                // [BelongsTo(typeof(Table), column: "Column")]
                // public ValueType Column {get; set;}
                //

                new MemberDefinition
                (
                    column.Name,
                    column.PropertyType,
                    CustomAttributeBuilderFactory.CreateFrom(new BelongsToAttribute(dataTable, bta.Required))
                )
            }
                   ));
        }, nameof(UnwrappedValueTypeView));
Exemplo n.º 2
0
        private static IEnumerable <MemberDefinition> GetKeyMembers()
        {
            IReadOnlyList <ColumnSelection> effectiveColumns = typeof(TUnwrappedView).GetColumnSelections();

            //
            // Vesszuk az eredeti nezet NEM lista tulajdonsagait
            //

            foreach (ColumnSelection column in typeof(TView).GetColumnSelections())
            {
                //
                // Ha kicsomagolas soran a tulajdonsag at lett nevezve, akkor azt hasznaljuk.
                //

                ColumnSelection effectiveColumn = effectiveColumns.SingleOrDefault(ec => ec.ViewProperty.IsRedirectedTo(column.ViewProperty))
                                                  ?? effectiveColumns.Single(ec => ec.ViewProperty.CanBeMappedIn(column.ViewProperty));

                yield return(new MemberDefinition
                             (
                                 effectiveColumn.ViewProperty.Name,
                                 effectiveColumn.ViewProperty.PropertyType,
                                 effectiveColumn.ViewProperty
                                 .GetCustomAttributes()
                                 .OfType <IBuildableAttribute>()
                                 .Select(attr => CustomAttributeBuilderFactory.CreateFrom(attr))
                                 .ToArray()
                             ));
            }
        }
Exemplo n.º 3
0
        internal protected static Type CreateView(MemberDefinition viewDefinition, IEnumerable <MemberDefinition> columns)
        {
            ClassFactory core = new
                                (
                viewDefinition.Name,
                viewDefinition
                .CustomAttributes

                //
                // Hogy a GetQueryBase() mukodjon a generalt nezetre is, ezert az uj osztalyt megjeloljuk nezetnek.
                //

                .Append(CustomAttributeBuilderFactory.CreateFrom <ViewAttribute>(new[] { typeof(Type) }, new object?[] { viewDefinition.Type }))
                .ToArray()
                                );

            //
            // Uj property-k definialasa.
            //

            foreach (MemberDefinition column in columns)
            {
                core.AddProperty(column.Name, column.Type, column.CustomAttributes.ToArray());
            }

            Type result = core.CreateType();

#if DEBUG
            StringBuilder sb = new();
            sb.AppendLine("View created:");
            foreach (Attribute attr in result.GetCustomAttributes())
            {
                sb.AppendLine($"[{attr}]");
            }
            sb
            .AppendLine(result.Name)
            .AppendLine("{");

            foreach (PropertyInfo prop in result.GetProperties())
            {
                foreach (Attribute attr in prop.GetCustomAttributes())
                {
                    sb.AppendLine($"  [{attr}]");
                }
                sb.AppendLine($"  {prop}");
            }
            sb.AppendLine("}");
            Debug.WriteLine(sb);
#endif
            return(result);
        }
Exemplo n.º 4
0
        private static IEnumerable <CustomAttributeBuilder> GetClassAttributes()
        {
            Type
                view      = typeof(TView),
                unwrapped = typeof(TUnwrappedView);

            //
            // Ha az eredeti nezet rendelkezik MapFrom attributummal akkor azt masolni kell, viszont elofordulhat h
            // az oszlop a kicsomagolas soran at lett nevezve ezert kicsit trukkos
            //

            MapFromAttribute?mapFrom = view.GetCustomAttribute <MapFromAttribute>();

            if (mapFrom is not null)
            {
                PropertyInfo mapFromProp = view.GetProperty(mapFrom.Property) ?? throw new MissingMemberException(view.Name, mapFrom.Property);
                mapFromProp = unwrapped
                              .GetProperties()
                              .Single(prop => prop.CanBeMappedIn(mapFromProp));

                yield return(CustomAttributeBuilderFactory.CreateFrom <MapFromAttribute>(new[] { typeof(string) }, new object?[] { mapFromProp.Name }));
            }
        }
Exemplo n.º 5
0
        private static IEnumerable <MemberDefinition> GetMembers()
        {
            foreach (IGrouping <string, ColumnSelection> grp in typeof(TView).GetColumnSelectionsDeep().GroupBy(sel => sel.ViewProperty.Name))
            {
                if (grp.Count() == 1)
                {
                    //
                    // Ha a property csak egyszer szerepel a kicsomagolt nezetben akkor nem kell modositani a nevet
                    //

                    ColumnSelection sel = grp.Single();

                    yield return(new MemberDefinition
                                 (
                                     grp.Key,
                                     sel.ViewProperty.PropertyType,
                                     CustomAttributeBuilderFactory.CreateFrom(sel.Reason)
                                 ));
                }

                else
                {
                    //
                    // Kulonben egyedive kell tenni a tulajdonsag nevet, valamint jelezni kell (MapToAttribute) h mely nezet-tulajdonsaghoz tartozik
                    //

                    int i = 0;

                    foreach (ColumnSelection sel in grp)
                    {
                        //
                        // [BelongsTo(typeof(TTable), column: "Column", ...), MapTo("TView.Column")]
                        // public TValue Column_i {get; set;}
                        //

                        yield return(new MemberDefinition
                                     (
                                         $"{grp.Key}_{i++}",
                                         sel.ViewProperty.PropertyType,
                                         CustomAttributeBuilderFactory.CreateFrom
                                         (
                                             sel.Reason,

                                             //
                                             // A "Column" tulajdonsagot meg ha az eredeti nezet nem is tartalmazta most be kell allitsuk
                                             // (mivel a tulajdonsag uj nevet kapott)
                                             //

                                             new KeyValuePair <PropertyInfo, object>
                                             (
                                                 typeof(ColumnSelectionAttribute).GetProperty(nameof(ColumnSelectionAttribute.Column)),
                                                 grp.Key
                                             )
                                         ),

                                         //
                                         // Mappolaskor az eredeti tulajdonsagba kerul vissza a tartalom
                                         //

                                         CustomAttributeBuilderFactory.CreateFrom <MapToAttribute>(new[] { typeof(string) }, new object[] { sel.ViewProperty.FullName() })
                                     ));
                    }
                }
            }
        }