c# - What design pattern to use for validating data and creating an object -
i come across situations want create instance of object passing given data or maybe object data or object needs valid or in right state. bit unclear on 'correct' way of doing this. here example:
given class:
class businessobject() { const threshold = 10; public businessobject(setofdata<sometype> setofdata) { // example of validation if (setofdata.count > threshold) { // performance business logic // set properties } } }
it possible run problems if this:
var setofdata = new setofdata<sometype>(); // if data not valid object created incorrectly var businessobject = new businessobject(setofdata);
so solutions have been either:
class businessobjectbuilder() { public businessobject build(setofdata<sometype> setofdata) { // example of validation if (setofdata.count > threshold) return new businessobject(setofdata); } else { return null; } } }
or make constructor private , add static factory method:
class businessobject() { const threshold = 10; public static create(setofdata<sometype> setofdata) { if (setofdata.count > threshold) { return new businessobject(setofdata); } else { return null; } } private businessobject(setofdata<sometype> setofdata) { // performance business logic // set properties } }
ideally not throw exception if data invalid there might multiple business objects being created in 1 process , don't want whole process fail if 1 validation fails , catching , suppressing exceptions not good.
also examples read of abstract factory or factory method involve passing in type or enum , correct object being built , returned. never seem cover scenario.
so conventions in scenario? advice appreciated.
imho constructor validation best many situation need make sure no object can created unless specified parameter being set.
public class businessobject { const threshold = 10; public businessobject(setofdata<sometype> setofdata) { // example of validation if (setofdata.count > threshold) { throw new invalidoperationexception("set data must above treshold"); } } }
however, has bad implementation when:
- you may have invalid object such when in draft status, etc
- used in orm when default constructor needed
- if heavy validation logic occurs.
for point no.1 , 2, cannot suggest other option except request - validate - submit mechanism.
for point no.3, reason is, class validation , creating monolithic code. if there validation logic, suggest implement builder pattern injected validator, , make constructor of businessobject
internal.
public class businessobjectbuilder { public businessobjectbuilder(ibusinessobjectvalidator validator){ this.validator = validator; } ibusinessobjectvalidator validator; public businessobject build(setofdata<sometype> setofdata) { // example of validation if (validator.isvalid(setofdata)) return new businessobject(setofdata); } else { throw new //exception } } }
this enforce modular programming , prevent monolithic code.
both of code is:
- easy test
- easy review
- extendable
Comments
Post a Comment