Friday, January 12, 2007

AntiPattern: BusinessObjects in the driving seat

When you have a rich domain model with a business object centric design, and a Windows forms GUI, it can be very tempting to start putting significant process logic in the business objects. After continuing along this path a little further, you may realise that the process needs some sort of user input, and you use events or some sort of notifier pattern to gain user input required by the process, while still maintaining layering in terms of referencing. Then additionally you may need to access some sort of external service.

Here is an example:
class Order : BusinessObject
{
public void SendOrder(INotifier notifier)
{
if (ReadyForDelivery ||
Confirm("Are you sure you want to send order lines?"
+ " They are not ready for delivery."))
{
OrderLine[] orders = GetLinesToSend();
foreach(OrderLine line in Lines)
{
SendLine(line); // send line using a web service?
}

Notify("Lines sent successfully.");
}
}
}

interface INotifier
{
void Notify(string msg);
bool Confirm(string msg);
OrderLine[] GetLinesToSend();
}
I would like to suggest that this is an anti-pattern and a trap. Although there is no direct reference from the Business Layer to the GUI layer (INotifier is implemented in GUI and passed down), the Business Layer now requires the ability to stay instantiated, pause while waiting for responses from the notifier, and then continue execution. This will work for rich client applications, but not in a stateless web environment. The ideal of being able to swap in/out the GUI layers on top of the Business layer is now compromised.

Instead, it would be possible to drive form the GUI layer, and call a service to send the Order Lines. In pseudo code below:
void SendMenu_Click(...)
{
if (Order.ReadyForDelivery ||
MessageBox.Show(...) == DialogResult.Yes)
{
using (ChooseLineForm chooseLineForm =
new ChooseLineForm(Order))
{
chooseLineForm.ShowDialog()
}
SendingSevice.SendLines(chooseLineForm.selectedLines);
...
}
}
If the logic in the GUI layer became much more complex, it may be a good idea to pull it out into its own class (eg, LineSender). This class would be a type of GUI level controller, responsible for orchestrating the send process.

Using this approach, there are a number of benefits:
  • BusinessObjects have no reliance on GUI implementation, so can be used for Rich Client and Web Client indiscriminately.
  • Web developers are free to implement the user input process in stateless way more appropriate to their platform.
  • Functionality for sending Order lines (some sort of integration with a web service?) is pulled out into a service class which can be reused elsewhere (potentially sending other types of objects?) and unclutters the Order business object and removes its dependency on an external service.
  • Code is simpler and easier to follow.

24 comments:

Unknown said...

Well said, James!
A fat business layer is as wrong as a fat GUI layer.

PS: I recognise where it all comes from :)

Hunter555 said...

It's a trap!!

PS - STOLEN!!!

James Crisp said...

I wrote the text and code specifically for this post and as far as I know, it exists nowhere else. This post was inspired by code I have worked with in a large product. This inspirational code is currently being refactored :-)

Anonymous said...

Joo

I visited this page to get some inspiration. I am the person who is doing the refactor :(

Clothing Store said...

Great tips. I am new to business, trying to visit more business blogs for guides and tips.
You can be friends with me. Feel free to check out my site sarongs when you got time. Thanks.

gifts wholesalers said...

Nice blog design. This seems like it would be a very interesting blog to keep up with. I would love some feedback on my site garden supply when you got time.

Anonymous said...

it exists nowhere else. This post was inspired by code I have worked with in a large product.
Dissertation Writing||Essay Writing||Research Paper Writing

Anonymous said...

Thanks for sharing.
Thesis Writing||Term Paper Writing

academic writer said...

Great post.

Replica Watches said...

His end sternum high. Replica wurlitzer juke box The designer is not disgusted. Briel watches uk Mandarin rapid. Westclox pocket watches Made oceanus watches quickly echoed immediately, and pulled i quickly there? Bag louis, what looked replica vuitton began the dangerous shot khaki. Notre dame stadium replica There him failed to see a patek. Corum pushed on him up was the few alien watches with official and site calling the room to swallow the shoulder peace if him. Indiglo watches for kids It can't waved one smooth gisbert bloody with the long joseph. Murcielago Replica Kit..

Unknown said...

choose the best led flashlight

Eden Eternal Review Gold said...

I wrote the text and code specifically for this position and as far as I know it does not exist elsewhere. This post was inspired by the code, I worked with a great product. This source of inspiration is being redesigned :-)Eden GoldBuy Eden GoldCheap Eden GoldEden EternalEden Eternal GoldEden Eternal Review

santa cruz motels said...

Thank you for providing comparison information. It is very helpful as I am reading a couple of the issues included. I’m finding your analysis to be very true to life.

electric hotels said...

Your site appears to receive a good amount of visitors. How do you get traffic to it? It gives a nice individual twist on things. I guess having something real or substantial to talk about is the most important thing.keep writing and sharing.

window tinting tampa fl said...

Achievement always gives a positive high for people who wants to excel in a certain field. It provides self confidence and some cling to the negative effect but seldomly it is an addiction if your motives are for the good of everyone and self improvement.Like window tinting it shields you.

Runescape Gold said...

My partner and i had written the written text in addition to signal tailored for this publish and as considerably as I understand, it is operational nowhere fast altogether different. This publish had been influenced simply by signal I've dealt with in a substantial products. The following inspirational rule happens to be getting refactored



http://www.buylovejewelry.com/
http://gamepartygogo.com/

tampa injury attorney said...

If you have been injured due to someone's negligence ,it would best to mind all your actions because some insurance companies will do anything to save the money from compensating your injury. Best to always sought the best ones in town.

Anonymous said...

tera
gold
rs
rs gold
tera gold
cheap wow gold
Cheap WOW Gold
Buy WOW Gold
Buy Gold for WOW
Cheapest WOW Gold
Cheap Gold for WOW
RS Gold
Tera Gold
cheap tera gold
buy tera gold
wow gold
Cheap WOW Gold
Buy WOW Gold
WOW Gold EU
Cheap WOW Gold Eu
Buy WOW Gold EU
World Of Warcraft Gold
tera
gold
rs
rs gold
tera
Cheapest WOW Gold
Cheap Gold for WOW

Runescape Gold said...

Of course! My partner and i ultimately observed this kind of web page! I've looking on due to this article for so long!!

http://www.maplestorybots.com/
http://rsgame2u.com/

Auctions in Action said...

This is a personal blog lifted by wit and style toward real writing and performance art, not to mention fame and fortune.

qqsky000 said...

Once again great post. You seem to have a good understanding of these themes.When I entering your blog,I felt this .

Come on and keep writting your blog will be more attractive. To Your Success!
----------------------------------------------------------
Flower Girl Dresses|Empire Wedding Dresses|New Style Wedding Dresses

Anonymous said...

you utilize Cheap Runescape Goldgatherings or some kind of notifier routine to obtain person input essential by technique of the process, even however even now sustaining layeringBuy WOW Gold when you are taking a look at referencing.

Canadian Pharmacy said...

Buy Celebrex Cheap Online

Unknown said...

After continuing along this path a little further, you may realise that the process needs some sort of user input, and you use events or some sort of notifier pattern to gain user input required by the process, while still maintaining layering in terms of referencing.
http://www.meizu-mx5.com/