locked
Refactoring Property values RRS feed

  • Question

  • User-1256377279 posted

    Hi All,

    Is there any alternative way to refactor below property value or best way to set the values

        public class Level : ArchiveDescription
        {
            public Level()
            {
               
            }
                    
            public Level(ArchiveDescription p_archDesc)
            {
    
                if (p_archDesc != null)
                {
                    MDARK = p_archDesc.MDARK;
                    RecordId = p_archDesc.RecordId;
                    MDARK = p_archDesc.MDARK;
                    LARK = p_archDesc.LARK;
                    SLARK = p_archDesc.SLARK;
                    Repository = p_archDesc.Repository;
                    CollectionArea = p_archDesc.CollectionArea;
                    ProjectCollection = p_archDesc.ProjectCollection;
                    Reference = p_archDesc.Reference;
                    Title = p_archDesc.Title;
                    DateRange = p_archDesc.DateRange;
                    StartDate = p_archDesc.StartDate;
                    EndDate = p_archDesc.EndDate;
                    CreationDateEra = p_archDesc.CreationDateEra;
                    CreationDateCalendar = p_archDesc.CreationDateCalendar;
                    Extent = p_archDesc.Extent;
                    PhysicalCharacteristics = p_archDesc.PhysicalCharacteristics;
                    CustodialHistory = p_archDesc.CustodialHistory;
                    ImmSourceAcquisition = p_archDesc.ImmSourceAcquisition;
                    Arrangement = p_archDesc.Arrangement;
                    PlaceOfOrigin = p_archDesc.PlaceOfOrigin;
                    AdministrativeContext = p_archDesc.AdministrativeContext;
                    ScopeContent = p_archDesc.ScopeContent;                
                    AccessCondition = p_archDesc.AccessCondition;
                    AccessDate = p_archDesc.AccessDate;
                    UserConditions = p_archDesc.UserConditions;
                    Appraisal = p_archDesc.Appraisal;
                    Accruals = p_archDesc.Accruals;
                    CopiesInformation = p_archDesc.CopiesInformation;
                    OrigInfo = p_archDesc.OrigInfo;
                    FindingAids = p_archDesc.FindingAids;
                    RelatedMaterial = p_archDesc.RelatedMaterial;
                    RelatedArchDesc = p_archDesc.RelatedArchDesc;
                    RelatedArchiveDescriptionNamedAuthority = p_archDesc.RelatedArchiveDescriptionNamedAuthority;
                    RelatedArchiveDescriptionPlace = p_archDesc.RelatedArchiveDescriptionPlace;
                    RelatedArchiveDescriptionSubject = p_archDesc.RelatedArchiveDescriptionSubject;
                    PublicationNote = p_archDesc.PublicationNote;
                    LegalStatus = p_archDesc.LegalStatus;
                    Note = p_archDesc.Note;
                    PrivateNotes = p_archDesc.PrivateNotes;
                    MaterialLanguage = p_archDesc.MaterialLanguage;
                    MaterialScript = p_archDesc.MaterialScript;
                    DescriptionLanguage = p_archDesc.DescriptionLanguage;
                    DescriptionScript = p_archDesc.DescriptionScript;   
                    LevelOfDetail = p_archDesc.LevelOfDetail;
                    Visibility = p_archDesc.Visibility;
                    ObjectURL = p_archDesc.ObjectURL;
                    DecimalLatitude = p_archDesc.DecimalLatitude;
                    DecimalLongitude = p_archDesc.DecimalLongitude;
                    DecimalCoordinates = p_archDesc.DecimalCoordinates;
                    Latitude = p_archDesc.Latitude;
                    Longitude = p_archDesc.Longitude;
                    DegreeCoordinates = p_archDesc.DegreeCoordinates;
                    Orientation = p_archDesc.Orientation;
                    Scale = p_archDesc.Scale;
                    ScaleDesignator = p_archDesc.ScaleDesignator;
                    Projection = p_archDesc.Projection;
                    Exhibition = p_archDesc.Exhibition;
                    Style = p_archDesc.Style;
                    Technique = p_archDesc.Technique;
                    DigitisationDate = p_archDesc.DigitisationDate;
                    Digitiser = p_archDesc.Digitiser;
                    MaterialType = p_archDesc.MaterialType;
                    BornDigital = p_archDesc.BornDigital;
                    LogicalLabel = p_archDesc.LogicalLabel;
                    LogicalType = p_archDesc.LogicalType;
                    AdditionalTitle = p_archDesc.AdditionalTitle;
                    TitleType = p_archDesc.TitleType;
                    BiographyRefRules = p_archDesc.BiographyRefRules;
                    CountriesRules = p_archDesc.CountriesRules;
                    DatesRules = p_archDesc.DatesRules;
                    DescriptionRules = p_archDesc.DescriptionRules;
                    InstitutionIdentifierRules = p_archDesc.InstitutionIdentifierRules;
                    LanguagesRules = p_archDesc.LanguagesRules;
                    RelationshipCatagoriesRules = p_archDesc.RelationshipCatagoriesRules;
                    RelationshipDescriptionsRules = p_archDesc.RelationshipDescriptionsRules;
                    ScriptRules = p_archDesc.ScriptRules;
                    StructureRules = p_archDesc.StructureRules;
                    ProcessedFile = p_archDesc.ProcessedFile;
                    ExternalIdentifier = p_archDesc.ExternalIdentifier;
                    FrmExtReference = p_archDesc.FrmExtReference;
                    FrmIntReference = p_archDesc.FrmIntReference;
                    SiblingOrder = p_archDesc.SiblingOrder;
                    Labels = p_archDesc.Labels;
                    Sequence = p_archDesc.Sequence;               
                }
    
            }       
    
        }

    Many Thanks,

    Shabbir

    Tuesday, May 28, 2019 1:29 PM

All replies

  • User197322208 posted

    1. AutoMapper:

    http://docs.automapper.org/en/stable/Construction.html

    2. Reflection - find properties and iterate

    Tuesday, May 28, 2019 1:34 PM
  • User-1256377279 posted

    How to use in .NET framework, I assume automapper works on .NET core 

    Tuesday, May 28, 2019 1:49 PM
  • User-1256377279 posted

    Sorry than i can't use Automapper, because my application is purely in .NET framework 

    Tuesday, May 28, 2019 2:14 PM
  • User765422875 posted

    It's supported in .NET too. It was originally written for .NET. You can use it.

    https://github.com/AutoMapper/AutoMapper

    Tuesday, May 28, 2019 2:15 PM
  • User303363814 posted

    Are you sure you have the modelling correct?  You haven't given us enough background but this sort of code is often indicative of a confusion between IsA and HasA.  Are you sure that the correct model is not

    Level HasA ArchiveDescription ?

    Your current model is

    Level IsA ArchiveDescription but you are creating an object from a very closely related oject.  This seems odd.

    It is quite common in books to see Manager is a subtype of Employee

    public class Manager: Employee

    but then you discover that an Employee gets a promotion and becomes a manager.  Now we have to create a new Manager object from the Employee object which does not reflect reality - they are still the same Employee, just with additional responsibilities.(Later they become a DepartmentManager and then a ProductManager and the problem expands).  It is usually better to model these scenarios so that an Employee has Manager properties, they can also have FireWarden properties or ExpensiveApproval properties.

    Bottom line - are you sure that you shouldn't be modelling this as

    public class Level {
       ArchiveDescription archive;
    
       public Level(ArchiveDescription ad) {
            archive = ad;
       }
    }
    Wednesday, May 29, 2019 11:00 PM
  • User-1256377279 posted

    Hi Paul,

    You have raised very important point, I tried but my Level object is empty after implement and serializing the object.

     public class LevelSubject
        {
            ATSubject subject;
    
            public LevelSubject() { }
    
            public LevelSubject(ATSubject p_atsubject)
            {
                
                 subject = p_atsubject;
               
            }
        }

    This is how i serialise

       LevelSubject slItem = new LevelSubject(SubjectItem);
    
                    TcItem.Xob = oComm.getXmlString(slItem, subjectList[sl].Level);
    
    
    
        public string getXmlString(object o, string level)
            {
                StringWriter l_StringWriter = new StringWriter();
                XmlTextWriter l_TextWriter = new XmlTextWriter(l_StringWriter);
                XmlDocument l_Doc = getXmlDocument(o, level);
    
                l_Doc.WriteTo(l_TextWriter);
    
                return l_StringWriter.ToString();
    
            }
    
    
      public virtual XmlDocument getXmlDocument(object o, string Level)
            {
                MemoryStream l_Stream = new MemoryStream();
                XmlDocument l_Doc = new XmlDocument();
    
    
                try
                {
                    //Read level in CamelCase when fetching data from the XOB
                    Level = GetLevelforXob(Level);
    
                    XmlSerializer l_Serializer = new XmlSerializer(o.GetType(), new XmlRootAttribute(Level));
                    XmlSerializerNamespaces l_SerializerNamespaces
                                                  = new XmlSerializerNamespaces();
    
                    // Remove the default namespaces.
                    l_SerializerNamespaces.Add("", "");
                    l_Serializer.Serialize(l_Stream, o, l_SerializerNamespaces);
                    l_Stream.Position = 0;
                    l_Doc.Load(l_Stream);
                    l_Stream.Close();
                    return l_Doc;
                }
                catch (Exception ex)
                {
    
                    throw ex.InnerException;
                }
               
            }

    Output 

    <?xml version=\"1.0\"?><Subject />

    Expected Output

    <Subject RecordID="049-003809810">
      <MDARK />
      <LARK />
      <Repository RepositoryId="10" CountryId="10">British Library</Repository>
      <Type SubjectTypeId="15">Building</Type>
      <Entry>PreferredTestByShabbir</Entry>
      <Authority SubjectAuthorityId="12">UKAT</Authority>
      <IsPreferred>Preferred</IsPreferred>
      <Longitude>E 12°30’41”</Longitude>
      <DecimalLongitude>12.51133</DecimalLongitude>
      <Latitude>N 41°53’31”</Latitude>
      <DecimalLatitude>41.89193</DecimalLatitude>
      <ExternalIdentifiers>
        <ExternalIdentifier>
          <Value>1234</Value>
          <Type Id="16">LCSH</Type>
        </ExternalIdentifier>
        <ExternalIdentifier>
          <Value>12345</Value>
          <Type Id="18">MeSH</Type>
        </ExternalIdentifier>
      </ExternalIdentifiers>
      <ArchiveDescriptionSubjectRelationships>
        <RelatedArchiveDescriptionSubject TargetNumber="040-003418663">
          <TargetRecordId>040-003418663</TargetRecordId>
        </RelatedArchiveDescriptionSubject>
        <RelatedArchiveDescriptionSubject TargetNumber="036-003477277">
          <TargetRecordId>036-003477277</TargetRecordId>
        </RelatedArchiveDescriptionSubject>
      </ArchiveDescriptionSubjectRelationships>
      <SubjectRelationships>
        <RelatedSubject RelationshipNumber="049-000091543">
          <TargetRecordId>049-000091543</TargetRecordId>
          <TargetType>Title of Work</TargetType>
        </RelatedSubject>
      </SubjectRelationships>
    </Subject>
    
    

    Thanks,

    Shabbir

    Thursday, May 30, 2019 11:55 AM
  • User765422875 posted

    Your original question seemed like it was a refactoring issue in which you wanted to simplify how you set your class properties. Hence the recommendation to look into AutoMapper. Now it seems you are having serialization issues with the XML serializer.

    Does the class you are trying to serialize have the correct attributes applied?

    Thursday, May 30, 2019 1:05 PM
  • User-1256377279 posted

    Sorry for confusion deepal, Yes i have applied correct attribute

        public class ATSubject
        {
            [XmlElement(ElementName = "MDARK")]
            public string MDARK { get; set; } = string.Empty;
    
            [XmlElement(ElementName = "LARK")]
            public string LARK { get; set; } = string.Empty;
    
            [XmlAttribute(AttributeName = "RecordID")]
            public string RecordId { get; set; }
    
            [XmlElement(ElementName = "Repository")]
            public IAMSRepositoryCV Repository { get; set; }
    
    
            [XmlElement(ElementName = "Type")]
            public IAMSSubjectTypeCV SubjectType { get; set; }
    
            [XmlElement(ElementName = "Entry")]
            public string SubjectEntry { get; set; }
    
            [XmlElement(ElementName = "Authority")]
            public IAMSSubjectAuthorityCV SubjectAuthority { get; set; }
    
            [XmlElement(ElementName = "IsPreferred")]
            public string IsPreferred { get; set; }
    
            [XmlElement(ElementName = "Longitude")]
            public string Longitude { get; set; }
    
            [XmlElement(ElementName = "DecimalLongitude")]
            public string DecimalLongitude { get; set; }
    
            [XmlElement(ElementName = "Latitude")]
            public string Latitude { get; set; }
    
            [XmlElement(ElementName = "DecimalLatitude")]
            public string DecimalLatitude { get; set; }
    
    
            [XmlArray("ExternalIdentifiers")]
            [XmlArrayItem("ExternalIdentifier")]
            public IAMSExternalIdentifierCV[] ExternalIdentifier { get; set; }
    
            [XmlArray("ArchiveDescriptionSubjectRelationships")]
            [XmlArrayItem("RelatedArchiveDescriptionSubject")]
            public IAMSRelatedArchiveDescriptionSubjectCV[] RelatedArchiveDescriptionSubject { get; set; }
    
            [XmlArray("SubjectRelationships")]
            [XmlArrayItem("RelatedSubject")]
            public IAMSRelatedSubjectCV[] RelatedSubject { get; set; }
        }
     public class LevelSubject : ATSubject
        {
            public LevelSubject() { }
    
            public LevelSubject(ATSubject p_atsubject)
            {
                if (p_atsubject != null)
                {
    
                    RecordId = p_atsubject.RecordId;
                    MDARK = p_atsubject.MDARK;
                    LARK = p_atsubject.LARK;
                    Repository = p_atsubject.Repository;
                    SubjectType = p_atsubject.SubjectType;
                    SubjectEntry = p_atsubject.SubjectEntry;
                    SubjectAuthority = p_atsubject.SubjectAuthority;
                    IsPreferred = p_atsubject.IsPreferred;
                    Longitude = p_atsubject.Longitude;
                    DecimalLongitude = p_atsubject.DecimalLongitude;
                    Latitude = p_atsubject.Latitude;
                    DecimalLatitude = p_atsubject.DecimalLatitude;
                    ExternalIdentifier = p_atsubject.ExternalIdentifier;
                    RelatedArchiveDescriptionSubject = p_atsubject.RelatedArchiveDescriptionSubject;
                    RelatedSubject = p_atsubject.RelatedSubject;
                }
            }
    
        }

    Thursday, May 30, 2019 1:13 PM
  • User765422875 posted

    Few things, you do not have an XmlRootAttribute defined. Are you trying to LevelSubject?

    If so, then you do XmlSerializer serializer = new XmlSerializer(typeof(LevelSubject));

    Thursday, May 30, 2019 1:36 PM
  • User-1256377279 posted

    I do define in below method

    public virtual XmlDocument getXmlDocument(object o, string Level)
            {
                MemoryStream l_Stream = new MemoryStream();
                XmlDocument l_Doc = new XmlDocument();
    
    
                try
                {
                    //Read level in CamelCase when fetching data from the XOB
                    Level = GetLevelforXob(Level);
    
                    XmlSerializer l_Serializer = new XmlSerializer(o.GetType(), new XmlRootAttribute(Level));
                    XmlSerializerNamespaces l_SerializerNamespaces
                                                  = new XmlSerializerNamespaces();
    
                    // Remove the default namespaces.
                    l_SerializerNamespaces.Add("", "");
                    l_Serializer.Serialize(l_Stream, o, l_SerializerNamespaces);
                    l_Stream.Position = 0;
                    l_Doc.Load(l_Stream);
                    l_Stream.Close();
                    return l_Doc;
                }
                catch (Exception ex)
                {
    
                    throw ex.InnerException;
                }
               
            }

    Thursday, May 30, 2019 1:39 PM
  • User765422875 posted

    I missed that. Its better to define it on the class itself, but it probably doesn't matter.

    Why are you doing o.GetType() vs.  XmlSerializer serializer = new XmlSerializer(typeof(LevelSubject))?

    Thursday, May 30, 2019 1:42 PM
  • User-1256377279 posted

    I can't because method is dynamic as the root can be subject, place, person or anything. I can't create separate method for every root element.

    Hope it make sense

    Regards,

    Shabbir

    Thursday, May 30, 2019 1:54 PM
  • User765422875 posted

    Ok, can you post sample data? It will be hard to help you debug this without it.

    Thursday, May 30, 2019 2:07 PM