0

Is it possible, to replace lost attributes, by extending a class from a Web Reference.

For example, previously a class containing a property called 'Amount` was part of the same project as defined below:

public class Test
{
    [Required]
    [Display(Name = "Amount", Description = "Amount")]
    [DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
    [Range(1000, int.MaxValue)]
    public decimal Amount { get; set; }
}

The class containing the property as written above was then moved to an external project and referenced as a Web Reference. The class imported has the property 'Amount' however is simply as below:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3190.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public partial class Test {

    private decimal amountField;

    /// <remarks/>
    public decimal Amount {
        get {
            return this.amountField;
        }
        set {
            this.amountField = value;
        }
    }
}

As you can see, attributes such as [Required], [Display(Name = "Amount", Description = "Amount")], [Display(Name = "Amount", Description = "Amount")], [DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)] and [Range(1000, int.MaxValue)] have been obmitted.

These attributes are still required for Model Validation, how can I extend the imported class by adding the missing attributes, so that Form Validation is not affected?

iggyweb
  • 2,373
  • 12
  • 47
  • 77
  • 1
    how do you determine what is valid and what is not from the webservice class? how would you know what validation to use? – mahlatse May 10 '19 at 12:22
  • Apologies, I made a slight typo, which I have corrected. Previously the class was referenced as WebApplication1.Models.Test, that class was moved in full as above to another project and is now referenced as WebApplication1.TestService.Test. – iggyweb May 10 '19 at 12:30
  • Would not adding those values to the attributes affect the webservice class should it ever change? You can just make a class that inherits from your service class and just use the new keyword to override the values and add those attributes – mahlatse May 10 '19 at 12:34
  • Though the biggest issue is what happens when the service class changes? your whole class will now throw errors(which is correct), unless you have some type of rule to validate the values( it should work out if you have a validate method) – mahlatse May 10 '19 at 12:35
  • Intrigued as to the inheritance approach, would it be possible for you to provide an example? – iggyweb May 10 '19 at 12:38
  • I realise I could rename my original class, so both exist, for example Test2 which has all the attributes and bind the form to that so model validation is unaffected and if the form passes model validation, simply declare the new external Test class and map values from Test2 to Test, but this would be significate code duplication effectively. – iggyweb May 10 '19 at 12:54
  • If you can somehow implement an Ivalidator interface, and maybe validate your values there, then just create a simple class that extends your service class and call the validate method, that you can still have the same logic – mahlatse May 10 '19 at 12:59
  • A workaround was to add back the original model and reference that in the form, re the initial populating of the form, from the TestService, I used AutoMapper. – iggyweb May 10 '19 at 14:58

0 Answers0