Example #1
0
        public static AdaptAttributeBuilder ApplyDefaultRule(this AdaptAttributeBuilder builder)
        {
            return(builder
                   .ForAllTypesInNamespace(typeof(Domain._IAssemblyMark).Assembly, "Domain.DomainModels")

                   .ExcludeTypes(type => type.IsEnum)
                   .AlterType(type => type.IsAssignableFrom(typeof(LookupBase)) == true, typeof(LookupDto))
                   .ShallowCopyForSameType(true).IgnoreNullValues(true));
        }
Example #2
0
 public static AdaptAttributeBuilder ApplyDefaultRule(this AdaptAttributeBuilder builder)
 {
     return(builder
            .ForAllTypesInNamespace(Assembly.GetExecutingAssembly(), "Sample.CodeGen.Domains")
            .ExcludeTypes(typeof(SchoolContext))
            .ExcludeTypes(type => type.IsEnum)
            .AlterType(type => type.IsEnum || Nullable.GetUnderlyingType(type)?.IsEnum == true, typeof(string))
            .ShallowCopyForSameType(true)
            .ForType <Enrollment>(cfg => cfg.Ignore(it => it.Course))
            .ForType <Student>(cfg => cfg.Ignore(it => it.Enrollments)));
 }
Example #3
0
 public static AdaptAttributeBuilder ApplyDefaultRule(this AdaptAttributeBuilder builder)
 {
     return(builder
            .ForAllTypesInNamespace(Assembly.GetAssembly(typeof(BaseEntity)), "QuizApp.DataAccess.Entities")
            .ExcludeTypes(typeof(BaseEntity), typeof(User))
            .ShallowCopyForSameType(true)
            .ForType <Subject>(cfg => cfg.Ignore(s => s.Lecturer))
            .ForType <Topic>(cfg => cfg.Ignore(t => t.Subject))
            .ForType <Question>(cfg => cfg.Ignore(q => q.Topic))
            .ForType <Answer>(cfg => cfg.Ignore(a => a.Question))
            .ForType <Attempt>(cfg => cfg.Ignore(at => at.Student)
                               .Ignore(a => a.Topic))
            .ForType <QuestionResult>(cfg => cfg.Ignore(qr => qr.Attempt)
                                      .Ignore(qr => qr.Question)));
 }
Example #4
0
 public static AdaptAttributeBuilder ApplyDefaultRule(this AdaptAttributeBuilder builder)
 {
     return(builder
            .ForAllTypesInNamespace(Assembly.GetAssembly(typeof(BaseEntity)), "DAL.Entities")
            .ExcludeTypes(typeof(BaseEntity), typeof(User))
            .ExcludeTypes(type => type.IsEnum)
            .ShallowCopyForSameType(true)
            .ForType <Consultation>(cfg => cfg.Ignore(c => c.Lecturer)
                                    .Ignore(c => c.Subject))
            .ForType <Queue>(cfg =>
     {
         cfg.Ignore(q => q.Consultation);
         cfg.Map(q => q.IssueCategory, typeof(IssueCategory));
     })
            .ForType <QueueMember>(cfg => cfg.Ignore(qm => qm.Student)
                                   .Ignore(qm => qm.Queue)));
 }
 public static AdaptAttributeBuilder ApplyDefaultRule(this AdaptAttributeBuilder builder)
 {
     return(builder
            .ForAllTypesInNamespace(Assembly.GetAssembly(typeof(Product)) !, "ProductService.Core.Entities")
            .ForAllTypesInNamespace(Assembly.GetAssembly(typeof(Customer)) !, "CustomerService.Core.Entities")
            .ForAllTypesInNamespace(Assembly.GetAssembly(typeof(Country)) !, "SettingService.Core.Entities")
            .ExcludeTypes(type => type.IsEnum)
            .AlterType(type => type.IsEnum || Nullable.GetUnderlyingType(type)?.IsEnum == true, typeof(string))
            .ShallowCopyForSameType(true)
            .ForType <Product>(cfg => cfg.Ignore(it => it.DomainEvents))
            .ForType <ProductCode>(cfg => cfg.Ignore(it => it.DomainEvents))
            .ForType <Return>(cfg => cfg.Ignore(it => it.DomainEvents).Ignore(it => it.Product))
            .ForType <Customer>(cfg => cfg.Ignore(it => it.DomainEvents).Ignore(it => it.CreditCards))
            .ForType <CreditCard>(cfg => cfg.Ignore(it => it.DomainEvents).Ignore(it => it.Customer))
            .ForType <Country>(cfg => cfg.Ignore(it => it.DomainEvents))
            );
 }
Example #6
0
 public static AdaptAttributeBuilder IgnoreNoModifyProperties(this AdaptAttributeBuilder builder)
 {
     return(builder
            .ForType <Enrollment>(cfg => cfg.Ignore(it => it.Student)));
 }
Example #7
0
        private static void CreateModel(ModelOptions opt, Type type, AdaptAttributeBuilder builder)
        {
            var segments    = GetSegments(type.Namespace, opt.BaseNamespace);
            var attr        = builder.Attribute;
            var definitions = new TypeDefinitions
            {
                Namespace         = CreateNamespace(opt.Namespace, segments, type.Namespace),
                TypeName          = attr.Name !.Replace("[name]", type.Name),
                PrintFullTypeName = opt.PrintFullTypeName,
                IsRecordType      = opt.IsRecordType,
                NullableContext   = GetTypeNullableContext(type),
            };
            var translator = new ExpressionTranslator(definitions);
            var isAdaptTo  = attr is AdaptToAttribute;
            var isTwoWays  = attr is AdaptTwoWaysAttribute;
            var side       = isAdaptTo ? MemberSide.Source : MemberSide.Destination;
            var properties = type.GetFieldsAndProperties().Where(it =>
                                                                 !it.SafeGetCustomAttributes().OfType <AdaptIgnoreAttribute>()
                                                                 .Any(it2 => isTwoWays || it2.Side == null || it2.Side == side));

            if (attr.IgnoreAttributes != null)
            {
                properties = properties.Where(it =>
                                              !it.SafeGetCustomAttributes()
                                              .Select(it2 => it2.GetType())
                                              .Intersect(attr.IgnoreAttributes)
                                              .Any());
            }

            if (attr.IgnoreNoAttributes != null)
            {
                properties = properties.Where(it =>
                                              it.SafeGetCustomAttributes()
                                              .Select(it2 => it2.GetType())
                                              .Intersect(attr.IgnoreNoAttributes)
                                              .Any());
            }

            if (attr.IgnoreNamespaces != null)
            {
                foreach (var ns in attr.IgnoreNamespaces)
                {
                    properties = properties.Where(it => getPropType(it).Namespace?.StartsWith(ns) != true);
                }
            }

            var propSettings = builder.TypeSettings.GetValueOrDefault(type);
            var isReadOnly   = isAdaptTo && attr.MapToConstructor;
            var isNullable   = !isAdaptTo && attr.IgnoreNullValues;

            foreach (var member in properties)
            {
                var setting = propSettings?.GetValueOrDefault(member.Name);
                if (setting?.Ignore == true)
                {
                    continue;
                }

                var adaptMember = member.GetCustomAttribute <AdaptMemberAttribute>();
                if (!isTwoWays && adaptMember?.Side != null && adaptMember.Side != side)
                {
                    adaptMember = null;
                }
                var propType = setting?.MapFunc?.ReturnType ??
                               setting?.TargetPropertyType ??
                               GetPropertyType(member, getPropType(member), attr.GetType(), opt.Namespace, builder);
                var nilAttr = member.GetCustomAttributesData()
                              .FirstOrDefault(it => it.AttributeType.Name == "NullableAttribute");
                var nilAttrArg = nilAttr?.ConstructorArguments.Count == 1 ? nilAttr.ConstructorArguments[0].Value : null;
                translator.Properties.Add(new PropertyDefinitions
                {
                    Name            = setting?.TargetPropertyName ?? adaptMember?.Name ?? member.Name,
                    Type            = isNullable ? propType.MakeNullable() : propType,
                    IsReadOnly      = isReadOnly,
                    NullableContext = nilAttrArg is byte b ? (byte?)b : null,
                    Nullable        = nilAttrArg is byte[] bytes ? bytes : null,
                });
Example #8
0
        private static void CreateModel(ModelOptions opt, Type type, AdaptAttributeBuilder builder)
        {
            var attr        = builder.Attribute;
            var definitions = new TypeDefinitions
            {
                Namespace         = opt.Namespace ?? type.Namespace,
                TypeName          = attr.Name !.Replace("[name]", type.Name),
                PrintFullTypeName = opt.PrintFullTypeName,
            };
            var translator = new ExpressionTranslator(definitions);
            var isAdaptTo  = attr is AdaptToAttribute;
            var isTwoWays  = attr is AdaptTwoWaysAttribute;
            var side       = isAdaptTo ? MemberSide.Source : MemberSide.Destination;
            var properties = type.GetFieldsAndProperties().Where(it =>
                                                                 !it.SafeGetCustomAttributes().OfType <AdaptIgnoreAttribute>()
                                                                 .Any(it2 => isTwoWays || it2.Side == null || it2.Side == side));

            if (attr.IgnoreAttributes != null)
            {
                properties = properties.Where(it =>
                                              !it.SafeGetCustomAttributes()
                                              .Select(it2 => it2.GetType())
                                              .Intersect(attr.IgnoreAttributes)
                                              .Any());
            }

            if (attr.IgnoreNoAttributes != null)
            {
                properties = properties.Where(it =>
                                              it.SafeGetCustomAttributes()
                                              .Select(it2 => it2.GetType())
                                              .Intersect(attr.IgnoreNoAttributes)
                                              .Any());
            }

            if (attr.IgnoreNamespaces != null)
            {
                foreach (var ns in attr.IgnoreNamespaces)
                {
                    properties = properties.Where(it => getPropType(it).Namespace?.StartsWith(ns) != true);
                }
            }

            var propSettings = builder.TypeSettings.GetValueOrDefault(type);
            var isReadOnly   = isAdaptTo && attr.MapToConstructor;
            var isNullable   = !isAdaptTo && attr.IgnoreNullValues;

            foreach (var member in properties)
            {
                var setting = propSettings?.GetValueOrDefault(member.Name);
                if (setting?.Ignore == true)
                {
                    continue;
                }

                var adaptMember = member.GetCustomAttribute <AdaptMemberAttribute>();
                if (!isTwoWays && adaptMember?.Side != null && adaptMember.Side != side)
                {
                    adaptMember = null;
                }
                var propType = setting?.MapFunc?.ReturnType ??
                               setting?.TargetPropertyType ??
                               GetPropertyType(member, getPropType(member), attr.GetType(), opt.Namespace, builder);
                translator.Properties.Add(new PropertyDefinitions
                {
                    Name       = setting?.TargetPropertyName ?? adaptMember?.Name ?? member.Name,
                    Type       = isNullable ? propType.MakeNullable() : propType,
                    IsReadOnly = isReadOnly
                });
            }

            var code = translator.ToString();
            var path = Path.Combine(Path.GetFullPath(opt.Output), definitions.TypeName + ".g.cs");

            WriteFile(code, path);