예제 #1
0
        public void GenerateCode(IConceptInfo conceptInfo, ICodeBuilder codeBuilder)
        {
            ReferencePropertyInfo info = (ReferencePropertyInfo)conceptInfo;

            var referenceGuid = new PropertyInfo {
                DataStructure = info.DataStructure, Name = info.Name + "ID"
            };

            PropertyHelper.GenerateCodeForType(referenceGuid, codeBuilder, "Guid?");
            PropertyHelper.GenerateStorageMapping(referenceGuid, codeBuilder, "System.Data.SqlDbType.UniqueIdentifier");

            if (DslUtility.IsQueryable(info.DataStructure) && DslUtility.IsQueryable(info.Referenced))
            {
                DataStructureQueryableCodeGenerator.AddNavigationPropertyWithBackingField(codeBuilder, info.DataStructure,
                                                                                          csPropertyName: info.Name,
                                                                                          propertyType: "Common.Queryable." + info.Referenced.Module.Name + "_" + info.Referenced.Name,
                                                                                          additionalSetterCode: info.Name + "ID = value != null ? (Guid?)value.ID : null;");
            }

            if (ReferencePropertyDbConstraintInfo.IsSupported(info) &&
                info.DataStructure is IOrmDataStructure &&
                info.Referenced is IOrmDataStructure)
            {
                var    ormDataStructure           = (IOrmDataStructure)info.DataStructure;
                var    referencedOrmDataStructure = (IOrmDataStructure)info.Referenced;
                string systemMessage = $"DataStructure:{info.DataStructure.FullName},Property:{info.Name}ID,Referenced:{info.Referenced.FullName}";

                if (info.DataStructure is IWritableOrmDataStructure)
                {
                    string onEnterInterpretSqlError = @"if (interpretedException is Rhetos.UserException && Rhetos.Utilities.MsSqlUtility.IsReferenceErrorOnInsertUpdate(interpretedException, "
                                                      + CsUtility.QuotedString(referencedOrmDataStructure.GetOrmSchema() + "." + referencedOrmDataStructure.GetOrmDatabaseObject()) + @", "
                                                      + CsUtility.QuotedString("ID") + @", "
                                                      + CsUtility.QuotedString(ReferencePropertyConstraintDatabaseDefinition.GetConstraintName(info)) + @"))
                        ((Rhetos.UserException)interpretedException).SystemMessage = " + CsUtility.QuotedString(systemMessage) + @";
                    ";
                    codeBuilder.InsertCode(onEnterInterpretSqlError, WritableOrmDataStructureCodeGenerator.OnDatabaseErrorTag, info.DataStructure);

                    if (info.Referenced == info.DataStructure)
                    {
                        codeBuilder.InsertCode($@"if (entity.{info.Name}ID != null && entity.{info.Name}ID != entity.ID) yield return entity.{info.Name}ID.Value;
            ", WritableOrmDataStructureCodeGenerator.PersistenceStorageMapperDependencyResolutionTag, info.DataStructure);
                    }
                }

                if (info.Referenced is IWritableOrmDataStructure)
                {
                    string onDeleteInterpretSqlError = @"if (interpretedException is Rhetos.UserException && Rhetos.Utilities.MsSqlUtility.IsReferenceErrorOnDelete(interpretedException, "
                                                       + CsUtility.QuotedString(ormDataStructure.GetOrmSchema() + "." + ormDataStructure.GetOrmDatabaseObject()) + @", "
                                                       + CsUtility.QuotedString(info.GetSimplePropertyName()) + @", "
                                                       + CsUtility.QuotedString(ReferencePropertyConstraintDatabaseDefinition.GetConstraintName(info)) + @"))
                        ((Rhetos.UserException)interpretedException).SystemMessage = " + CsUtility.QuotedString(systemMessage) + @";
                    ";
                    codeBuilder.InsertCode(onDeleteInterpretSqlError, WritableOrmDataStructureCodeGenerator.OnDatabaseErrorTag, info.Referenced);
                }
            }
        }
예제 #2
0
        public void GenerateCode(IConceptInfo conceptInfo, ICodeBuilder codeBuilder)
        {
            ReferencePropertyInfo info = (ReferencePropertyInfo)conceptInfo;

            var referenceGuid = new PropertyInfo {
                DataStructure = info.DataStructure, Name = info.Name + "ID"
            };

            PropertyHelper.GenerateCodeForType(referenceGuid, codeBuilder, "Guid?");

            if (DslUtility.IsQueryable(info.DataStructure) && DslUtility.IsQueryable(info.Referenced))
            {
                DataStructureQueryableCodeGenerator.AddNavigationPropertyWithBackingField(codeBuilder, info.DataStructure,
                                                                                          csPropertyName: info.Name,
                                                                                          propertyType: "Common.Queryable." + info.Referenced.Module.Name + "_" + info.Referenced.Name,
                                                                                          additionalSetterCode: info.Name + "ID = value != null ? (Guid?)value.ID : null;");
            }

            if (info.DataStructure is IOrmDataStructure && info.Referenced is IOrmDataStructure)
            {
                codeBuilder.InsertCode(
                    info.OrmMappingSnippet(),
                    DomInitializationCodeGenerator.EntityFrameworkOnModelCreatingTag);
            }
            else if (info.DataStructure is IOrmDataStructure)
            {
                codeBuilder.InsertCode(
                    string.Format("modelBuilder.Entity<Common.Queryable.{0}_{1}>().Ignore(t => t.{2});\r\n            ",
                                  info.DataStructure.Module.Name, info.DataStructure.Name, info.Name),
                    DomInitializationCodeGenerator.EntityFrameworkOnModelCreatingTag);
            }

            if (ReferencePropertyConstraintDatabaseDefinition.IsSupported(info) &&
                info.DataStructure is IOrmDataStructure &&
                info.Referenced is IOrmDataStructure)
            {
                var    ormDataStructure           = (IOrmDataStructure)info.DataStructure;
                var    referencedOrmDataStructure = (IOrmDataStructure)info.Referenced;
                string systemMessage = "DataStructure:" + info.DataStructure + ",Property:" + info.Name + "ID,Referenced:" + info.Referenced;

                if (info.DataStructure is IWritableOrmDataStructure)
                {
                    string onEnterInterpretSqlError = @"if (interpretedException is Rhetos.UserException && Rhetos.Utilities.MsSqlUtility.IsReferenceErrorOnInsertUpdate(interpretedException, "
                                                      + CsUtility.QuotedString(referencedOrmDataStructure.GetOrmSchema() + "." + referencedOrmDataStructure.GetOrmDatabaseObject()) + @", "
                                                      + CsUtility.QuotedString("ID") + @", "
                                                      + CsUtility.QuotedString(ReferencePropertyConstraintDatabaseDefinition.GetConstraintName(info)) + @"))
                    ((Rhetos.UserException)interpretedException).SystemMessage = " + CsUtility.QuotedString(systemMessage) + @";
                ";
                    codeBuilder.InsertCode(onEnterInterpretSqlError, WritableOrmDataStructureCodeGenerator.OnDatabaseErrorTag, info.DataStructure);
                }

                if (info.Referenced is IWritableOrmDataStructure)
                {
                    string onDeleteInterpretSqlError = @"if (interpretedException is Rhetos.UserException && Rhetos.Utilities.MsSqlUtility.IsReferenceErrorOnDelete(interpretedException, "
                                                       + CsUtility.QuotedString(ormDataStructure.GetOrmSchema() + "." + ormDataStructure.GetOrmDatabaseObject()) + @", "
                                                       + CsUtility.QuotedString(info.Name + "ID") + @", "
                                                       + CsUtility.QuotedString(ReferencePropertyConstraintDatabaseDefinition.GetConstraintName(info)) + @"))
                    ((Rhetos.UserException)interpretedException).SystemMessage = " + CsUtility.QuotedString(systemMessage) + @";
                ";
                    codeBuilder.InsertCode(onDeleteInterpretSqlError, WritableOrmDataStructureCodeGenerator.OnDatabaseErrorTag, info.Referenced);
                }
            }
        }