c# - Use factory pattern when using different properties based on a property value -
in web api 2 project have model orderline (nested object of order model). client can post data , via ivalidatableobject
validate model. simplyfied, models looks this:
public class order { public int id { get; set; } public string description { get; set; } public icollection<orderline> orderlines { get; set; } } public class orderline : ivalidatableobject { public int id { get; set; } public string type { get; set; } [required] public string itemcode { get; set; } [required] public string description { get; set; } [requiredif("type == 'a'")] public string deliverytype { get; set; } public ienumerable<validationresult> validate(validationcontext validationcontext) { if(type.toupper() == "a") { var item = itemcode + deliverytype; using(mydbcontext db = new mydbcontext)) { var items = db.items.where(q => q.itemcode == item.tostring()).singleordefault(); if(items == null) { yield return new validationresult("the conbination of itemcode , confactiontype not allowed"); } } } if(type.toupper() == "b") { using(mydbcontext db = new mydbcontext)) { var items = db.items.where(q => q.itemcode == item.tostring()).singleordefault(); if(items == null) { yield return new validationresult("the field itemcode not contain valid code"); } } } } }
based on value of orderline.type property know kind of orderline type i'm going write database. in above class, have few properties, imagine that, depending on type, have multiple different properties , validation rules. based on this post wondering if should use factory this:
public class orderlinea : orderline { // properties // validation rules } public class orderlineb : orderline { // (other) properties // (other) validation rules }
Comments
Post a Comment