I have an Inventory object that encapsulates all the behaviours relevant e.g. add stock; calculate average cost etc. Other than a readonly list, users will never directly interact with this object, rather it is used by other objects such as Customer Orders; Stock Adjustment; Stock Take etc.
Question is, should the Inventory Object be a child object or an editable root object. The actions on the Inventory table in the DB will need to be in the same transaction as the calling object (for example when the Customer Order Object records an order, the Inventory object needs to reduce stock).
This has stumped me for the moment
Thanks in advance
Bonio
Hello Bonio,
Sounds like you want to use the same business object for two different reasons. It might be better to have two business objects - one for the read-only list and another for the editable one.
I had a similar question here http://forums.lhotka.net/forums/thread/1658.aspx . There's some good information in that thread.
Mike
Hi Mike
Thanks for the reply.
Thinking about the use case, the Inventory object needs to sometimes take commands from other objects whilst on one occaision at least it needs to be directly accessible (for stock adjustments). Am looking at the Switchable object template and thinking this may be the solution.
Thanks again,
Bonio
Hello Bonio,
Take a look at that thread. In particular, the first response to my initial question.
"As soon as you say "sometimes I may want to..." you are almost certainly describing a different use case."
I used to think that one database table = one business object. That's not the case. It's perfectly acceptable to have multiple business objects that act on different tables in different ways.
Regards,
Mike
Having studied the use case a bit more carefully, the Inventory object will need to be used both in a Child situation (when performing stock actions for Customer Orders etc) and also as an Editable Root Object (for example when a stock adjustment needs to be performed). So, I think an switchable object might be the best solution.
Yours, ever confused...
Bonio
A further thought on this question:
In Rocky's book he uses the Assigment class to aide collaboration between two other classes. My issue is this: I have an Inventory class that contains methods etc that need to be accessed by other classes (such as order entry etc). I currently have the Inventory class configured as an editable root object as I need some of this functionality.
Being a noob to all this, my question is can I instantiate a editable root object [ERO] within the context of another ERO? For example, could I instantiate the Inventory class within the Order Entry class, use some of its methods to update average costs, stock levels etc and then re-use the object to support the Goods Receiving class or the Return to Vendor class [transactionally as well]?
Apologies if I am labouring the point, but this is really stressing me at the mo
Thanks in advance
Bonio
Hi Bonio,
I understand your confusion.
In Rocky's book (Expert C# 2005 BO, p. 50), there's a little piece of information that really helped me understand one of the basic proinciples behind CSLA.
Here Rocky describes a Customer class and an Invoice class, and brings up the point that an Invoice might need Customer data. He says that a "naive solution would be to have the Invoice object make use of the Customer object".
The prefered solution is to have the Invoice directly access the customer data.
Hope this helps.
Thanks for the reply Lukky.
Surely though, if I cannot get business objects to co-operate with each other, it will mean having to duplicate the same piece of code across multiple objects e.g. the same logic for calculating average cost will have to be reproduced in 5 or 6 objects - surely this isn't logical?
Thanks
Bonio
Bonio:Thanks for the reply Lukky.
Surely though, if I cannot get business objects to co-operate with each other, it will mean having to duplicate the same piece of code across multiple objects e.g. the same logic for calculating average cost will have to be reproduced in 5 or 6 objects - surely this isn't logical?
Thanks
Bonio
Bonio:Surely though, if I cannot get business objects to co-operate with each other, it will mean having to duplicate the same piece of code across multiple objects e.g. the same logic for calculating average cost will have to be reproduced in 5 or 6 objects - surely this isn't logical?
The whole point is precisely to normalize behavior in such a way that it is implemented only once in your application. In your case, the fact that you need that behavior in 5 or 6 places clearly indicates that you need to normalize it.
Tom and ajj's suggestion are different way to achieve this normalization. Just like Rocky's example (Assignement class), you need to figure out the comon behavior, and pull it out from these classes.
I have myself re-read chapter 6 today, and the more I read about this behavior normalization, the more I like it.
Regards
Copyright (c) Marimer LLC