Creation of object

Creation of object

Old forum URL: forums.lhotka.net/forums/t/571.aspx


kaspyanand posted on Saturday, July 08, 2006

The requirement in my project is creation of itinerary ..which will have a name,category(like adventure,classisc),noofdays(eg 6 day itinerary),and activities assigned to each day..so..to design this situation when user creates an itinerary,i need to create an itinerary object with name,category and noofdays(my understnding is that these are invariant of class)..my question is where to check if data supplied by user is incorrect(eg 0 nofodays is invalid)..in constructor..if so,if data is invalid..what needs to be done.. raise exception and destroy object? or use a factory class to create object..which will check data provided and if everything is ok,then create itinerary object..I am using C#..
Does CSLA has any guideline for such a scenario?
thks

Jurjen replied on Saturday, July 08, 2006

You should add some 'rules' to the brokenrules collection within the object. When the object is created all rules will be checked, when as you say NoOfDays must be greater than 0 it will invalid a rule, making the object invalid. Invalid objects can't be saved. You may want to look it up in the book to see how it works, here's some code that might help.

Jurjen.

 

Protected Overrides Sub AddBusinessRules()

MyBase.AddBusinessRules()

With BrokenRules

.AddRule(AddressOf  CodeRequired, "Code", New RuleArgs("Code"))

End With

End Sub

 

<Description("{0} is a required field.")> _

Private Function CodeRequired(ByVal target As Object, ByVal e As RuleArgs) As Boolean

Return Len(CallByName(target, e.PropertyName, CallType.Get)) > 0

End Function

 

 

Copyright (c) Marimer LLC