Wednesday, January 17, 2007
« Data binding issue in WPF: with solution... | Main | I've been triple tagged »

I recently had an email discussion where I was describing why I needed to solve the problem I described in this article for WPF and this article for Windows Forms.

 

In both cases the issue is that data binding doesn’t refresh the value from the data source after it updates the data source from the UI. This means that any changes to the value that occur in the property set code aren’t reflected in the UI.

 

The question he posed to me was whether it was a good idea to have a property set block actually change the value. In most programming models, goes the thought, assigning a property to a value can’t result in that property value changing. So any changes to the value that occur in the set block of a property are counter-intuitive, and so you simply shouldn’t change the value in the setter code.

 

Here’s my response:

 

The idea of a setter (which is really just a mutator method by another name) changing a value doesn't (or shouldn't) seem counter-intuitive at all.

 

If we were talking about assigning a value to a public field I’d agree entirely. But we are not. Instead we’re talking about assigning a value to a property, and that’s very different.

 

If all we wanted were public fields, we wouldn't need the concept of "property" at all. The concept of "property" is merely a formalization of the following:

 

  1. public fields are bad
  2. private fields are exposed through an accessor method
  3. private fields are changed through a mutator method
  4. creating and using accessor/mutator methods is awkward without a standard mechanism

 

So the concept of "property" exists to standardize and formalize the idea that we need controlled access to private fields, and a standard way to change their value through a mutator method.

 

Consider the business rule that says a document id must follow a certain form - like SOP433. The first three characters must be alpha and upper case, the last three must be numeric. This is an incredibly common scenario for document, product, customer and other user-entered id values.

 

Only a poor UI would force the user to actually enter upper case values. The user should be able to type what they want, and the software will fix it.

 

But putting the upper case rule in the UI is bad, because code in the UI isn't reusable, and tends to become obsolete very rapidly as technology and/or the UI design changes. There's nothing more expensive over the life of an application than a line of code in the UI. So while it is possible to implement this rule in a validation control, in JavaScript, in a button click event handler - none of those are good solutions to the real problem.

 

Yet if that rule is placed purely in the backend system, then the user can't get any sort of interactive response. The form must be "posted" or "transmitted" to the backend before the processing can occur. Users want to immediately see the value be upper case or they get nervous.

 

So then we're stuck. Many people implement the rule twice. Once in the UI to make the user happy, and once in the backend, which is the real rule implementation. And then they try to keep those rules in sync forever - the result being an expensive, unreliable and hard to maintain system.

 

I've watched this cycle occur for 20 years now, and it is the same time after time. And it sucks.

 

This, right here, is why VB got such a bad name through the 1990’s. The VB forms designer made it way too easy to write all the logic in the UI, and without any other clear alternative that's what happened. The resulting applications are very fragile and are impossible to upgrade to the next technology (like .NET). Today, as we talk, many thousands of lines of code are being written in Windows Forms and Web Forms in exactly the same way. Those poor people will have a hell of a time upgrading to WPF, because none of their code is reusable.

 

What's needed is one location for this rule. Business objects offer a workable solution here. If the object implements the rule, and the object runs on the client workstation, then (without code in the UI) the user gets immediate response and the rule is satisfied. And the rule is reusable, because the object is reusable - in a way that UI code never can be (or at least never has been).

 

That same object, with that same interactive rule, can be used behind Windows Forms, Web Forms, WPF and even a web services interface. The rule is always applied, because it is right there in the object. And for interactive UIs it is immediate, because it is in the field's mutator method (the property setter).

 

So in my mind the idea of changing a value in a setter isn't counter-intuitive at all - it is the obvious design purpose behind the property setter (mutator). Any other alternative is really just a ridiculously complex way of implementing public fields. And worse, it leaves us where we've been for 20+ years, with duplicate code and expensive, unreliable software.


Wednesday, January 17, 2007 6:37:19 PM (Central Standard Time, UTC-06:00)
Hmmm... perhaps I missed the last round of kool-aid, but, exactly why is not setting the internal variable in a property setter a bad idea?

Of course, you answered the question already. I would ask, if not the property setter, just where is it supposed to happen? I really do want to know! Heck, you just got me using property setters, rather than just having a public field, this year, and now this! My neck is hurting from the whiplash!
Wednesday, January 17, 2007 6:39:41 PM (Central Standard Time, UTC-06:00)
Ok - I get it. Should you literally change the value between what it comes in as, as a parameter, and what it sets the internal variable as (i.e. SSN comes in as AAA-BB-XXXX, you remove the dashes, and it gets set internally as AAABBXXXX).

Of course. That is part of the business logic. Next question. So long as the business meaning stays the same, not a problem.
Monday, January 22, 2007 9:27:43 AM (Central Standard Time, UTC-06:00)
I must say I agree with you on this topic; I don't know anyone who doesn't. I have developed a few applications in VB 6 (yes, VB version 6) and C# using this approach.

It IS very, very difficult to implement in any but the most simple of applications, however. Unless you are very careful, it is very easy to have all your layers working properly and then still find business rules being placed in screens, forms, and services. In the majority of business applications that I've put together, the same business object is used in multiple screens, forms, and services. The problem is that, usually, each one requires that different - conflicting - sets of business rules be placed on the same object. There are many design choices when faced with this situation.

1. You can wire each screen, form, or service with the rules. Obviously, some of the rules will be duplicated. This is exactly what we are trying to avoid. It is, however, the easiest to implement; especially since many UI actions occur based on the results of some of the business rules. It's a trap! Don't fall for it.
2. You can clutter the object with "condition" settings and apply rules based on the conditions. I've seen this done many times and I hate it. Talk about a maintenance nightmare...
3. If the screen, rule, or service requires a special set of business rules, you can create a separate business object for each; inheriting from the business object that provides the most complete set of rules for the designed purpose. This sounds good. Business Objects contain all the business rules. Screens, forms, and services don't have duplicate rules to maintain. Maintenance can still be a pain in some cases, though. When a new rule must be created, care must be taken to position it in the right business object so that it is inherited only where required. It is very easy to find duplicate rules in this setup after a few months of maintenance kicks in.

Of these options, I like number three the best. But I find that most people first choose option 2, then find themselves wishing for something different.

If anyone knows of other options or approaches, I'd love to hear about them.

Now all this said, I have to laugh at this statement from your post, Rocky:
"The resulting applications are very fragile and are impossible to upgrade to the next technology (like .NET). Today, as we talk, many thousands of lines of code are being written in Windows Forms and Web Forms in exactly the same way. Those poor people will have a hell of a time upgrading to WPF, because none of their code is reusable."

This strikes me as "funny" because every time I've upgraded an application to the next technology, it required a complete rewrite from the ground up! The rewrites aren't necessary because business rules are placed in forms, screens, and services - but because the code wasn't transferable to the new technology! Even upgrading from .NET 1.1 to .NET 2, or .NET 2 to .NET 3, how much of the code is going to transfer cleanly? That's just code. Usually an upgraded database is involved, too. SQL Server 2000 to 2003 or 2005, for example. Coding technology, databases, and even hardware are changing so rapidly that keeping up with it all is practically impossible.

Really, does ANY of this really matter?
Tuesday, January 30, 2007 9:00:16 PM (Central Standard Time, UTC-06:00)
You're saying that if I ask the user for his name

name = askUserForName(); // say user enters "r.lhotka"
user.name = name; // user.name is now set to "R. Lhotka"
assert(name == user.name); // may fail

You think that we should NOT expect such an assertion to pass.

Is this really a no-brainer? Well, it does seem to violate many of
our assumptions about assignment and equality. It also means that
the UI programmer needs to know whether .name is a property
or a field, and basically be a little more paranoid.

Why not just provide a method, like, say, void parseName(string), or even
a static method, like string cleanUpName(string). Why not separate
the model from the view with a thin presentation layer. Why not
provide the UI developer with a customized text box that does the conversion
automatically. There are lots of different ways of solving this. You could
even credit the UI developer with a little intelligence, and let him
add the functionallity himself, like in the browser, with javascript --
something a business object can't do.

Having the id be SOP123 might be a business rule, but having sop123
automagically convert to SOP123, might not be. In a model view
controller architecture, such a conversion may be viewed as the
responsibility of the controller, not the model.

If you insist that properties can change the value in the setter, so databinding
needs to account for this, and I insist that we should use model-view-presenter
to handle these conversions, so it irrelavent, where are we?
jsmith
Tuesday, January 30, 2007 9:18:56 PM (Central Standard Time, UTC-06:00)
What I'm saying is twofold.

First, if changing the value in a property setter is prohibited by convention, then it should be prohibited by compiler. Obviously, the fact that the compiler treats it like a mutator method (look at the resulting CIL) indicates that the property setter is merely a convenient formalization of the accessor/mutator pattern.

Second, if you want to require manual creating of code that sits between the presentation and model that's fine. But that is _exactly_ what data binding is all about. Microsoft has, with data binding, created a standard framework for a form of MVC/MVP. I personally think it is silly to write that code by hand when they've done the work for us.

Along that same train of thought, if you disallow data binding from reacting to changed values in a property setter, then you either disallow the use of data binding entirely, or disallow the idea of a responsive, interactive UI. Many applications DO need to change values as they are entered by the user. If data binding can't handle that common scenario, then either you give the user a non-interactive UI (think mainframe or web), or you abandon data binding and write your own code.

Again, I don't want to write my own data binding, and I _really_ don't want to write shim code between every page/form and every business object - that's just silly imo. So I want Microsoft's data binding to do what it should do, and support this common scenario.
Saturday, February 03, 2007 3:09:15 AM (Central Standard Time, UTC-06:00)
I've always liked your ideas because they have a real practical edge. That's clear here. Why not use the data binding mechanism that Microsoft has provided? Its probably the way to go.

But its not all gravy, is it. What if I don't feel like writing MyOwn.Web.MyOwnDataSourceView, and the other DataSource, DataSourceDesigner, etc., complex classes that are required to eat this particular _free_ lunch. Not all my projects are going to be CSLA applications.

While you stated: "if changing the value in a property setter is prohibited by convention, then it should be prohibited by compiler", I'm guessing that you don't really believe that "if it passes the compiler, it's ok" is a reasonable design heuristic. After all, the compiler allows public fields, which you feel are "bad".

The model-view-presenter is obviously a java type thing, and we really don't like java type things. But writing presenter classes is not that strenuous and can give a lot of flexibility. Its particularly useful for those test first twits. Sometimes if a business class is worth writing, its worth writing a presenter for. Whether its "just silly" depends on the perspective of the developer. A presentation layer, taken seriously, may enable richer UI screens without putting undue stress on the business object code or the UI code (e.g. MS composite ui application block). It also avoids the need to redefine presentation logic as "business rules."

If there is an impedance mismatch between the domain model and the database, surely there is a greater one between the user and the business layer. If I have a business object that can respond to a wide array of date formats, just because they are convenient for the user, am I not cluttering up my business objects with "business rules" that serve no other purpose than massaging the impedance mismatch between the UI and the biz layer. Is data binding really so compelling? At least the TableAdapter lets me avoid REAL work.

Properties use the syntax of assignment and appear, to me, to imply the semantics of assignment. When the following code passes
obj.type = "rx";
assert(obj.type == "prescription")
we may be violating the semantics of assignment, and, we may be putting presentation logic in our business layer. I may decide that the convenience of databinding justifies these "sins," but decide to go ahead with it anyway. Or, you may feel that properties do not imply assignment semantics, and believe that everything that does not go in the view is a business rule. You might even decide that you're just going to adopt this particular view within the confines of your CSLA goals. But perhaps we shouldn't pretend that property assignments that don't behave like assignments are, across contexts, obviously ok. This wasn't the case for the MS databinding guys. If it was, you wouldn't even be blogging about this. You'd be doing properties -- your way -- and would not really care if your way was the same as my way, and I wouldn't have spent the last hour on this comment, whose ultimate utility is exceedingly doubtful.
jsmith
Saturday, February 03, 2007 10:06:01 AM (Central Standard Time, UTC-06:00)
Why do you question the value of your post? If your ideas weren’t interesting to me, I certainly wouldn’t be taking the time to respond.

While it is true that the data binding in WPF doesn’t do exactly what I want, it is important to realize that I’ve been helping to shape data binding for the past 10+ years. I am (in all honestly) a part of the reason you can bind to objects AT ALL. I see no reason to stop trying to shape data binding now :)

The alternative, in my view, is to write my own data binding. My good friend Billy Hollis did exactly this, and has had excellent results. But I think it only made sense because he was working on a multi-year project. On any smaller effort, the time it takes to re-create data binding could never have enough payback.

Obviously the other alternative is to eschew such a formal abstraction layer altogether, and to write a presenter for each form/page yourself. And I wouldn’t even object to this too much if I could use a round-tripping code generator to write it for me.

You must understand that I don’t disagree with the CONCEPT of a presenter. If that’s what you heard then I apologize for the misunderstanding. I just want my presenter to be a pre-built, standardized, and highly tested technology. Ideally one that is used by thousands, if not millions, of developers. Data binding fits this bill, even if it isn’t quite perfect (yet).

And I agree that there’s a mismatch between a typical object model and the UI. This is an unfortunate side-effect of poor object design, but since most people design poor object models, that’s what most people end up dealing with. But if you follow the design concepts I prefer, ones from David West’s “Object Thinking” and Eric Evans’ “Domain Driven Design” for example, you’ll end up with an object model that matches the use case. Since the UI also tends to follow the use case (by definition), the mismatch SHOULD be small.

But given the prevalence of data-centric object models, and the way ORM tools and data-drive code generators help build them, you are right that most people are faced with this unfortunate situation.

Even so, I disagree with your particular example of date display. It is NOT the job of the object model to format data. That’s the job of the UI. The job of the object model is to enforce business rules. The fact that a product id must be all upper case is a business rule. The fact that users prefer to see their dates in a certain format is not.

Look at all three binding models: Windows, Web and WPF. Each of them automates the connection between the UI and the model. Each of them provides for separation of concerns, allowing specifically for the idea that the UI should handle things like formatting.

For example, Windows Forms has the Format/Parse event model and WPF has the ValueConverter model. There’s no doubt that the designers of this presentation framework are fully aware that data formatting is a UI concern.

And, having talked to people in each of these teams, I know full well that they understand the need for separation of business logic from the UI. What they DON’T all understand (especially on the web and WPF teams) is the need for user interactivity while retaining this separation.
Microsoft is currently on the “block mode UI” bandwagon. You can see it with many of their technologies (Web (pre-AJAX), WPF, Workflow, SOA, Infopath).

Have the user enter data into a form.
Send that data to a server.
Have the server process the data and return results.
Display the laundry list of problems to the user.

Very mainframe, very web, very poor user experience. But very testable, very manageable, very controlled. EXACTLY why the mainframe was so loved by IT pros, because it was a simpler model (even if the user experience pretty much sucked).

I want more. I want interactivity AND I want simplicity. And, frankly, data binding is THIS CLOSE to providing the right result.

In fact, Windows Forms is there. It does a great job – after 15 years of evolution. Web Forms is getting darn close, though it’ll never really get there due to the very nature of the web’s technology. WPF is merely a version 1.0 technology, and I am VERY pleased with how good it is for its age. I very much doubt it will take 15 years for WPF to get to where Windows Forms is. I’m guessing more like 5 years.

In the end though, I doubt you and I actually disagree on anything substantial. Obviously we both strongly believe in separation of concerns, and separation of the presentation and business layers. If I’m wrong there, then this conversation does become rather moot, but I doubt I’m wrong.

Where we disagree is in a fine point: how to manage that separation.

I have no problem with people choosing to write their own presentation layer if that gets them excited. And I understand that code you write is more testable than code that Microsoft writes.

But I do somewhat buy into the OSS view: that more widely used code is more tested code. Data binding is EXTREMELY widely used, and I’d rather use it as a pre-built (though less testable) presentation layer, because it is, almost by definition, tested far better than you are likely to test your own code.
Sunday, February 04, 2007 4:00:21 AM (Central Standard Time, UTC-06:00)
I think the central point in our disagreement is that I consider some of the calls you’re making to be context bound, that is, reasonable with a certain context, whereas you feel that their validity may extend further. I feel that assertions like “changing the value in properties in the setters according to business rules makes good design sense” is about as valid as asserting that it is wrong to do so. It is all context bound. I just don’t find these absolutes very useful. I never felt the need to have only one return statement, and I’ll use a goto statement maybe once a year without any guilt.

Yet the comedy plays out on a much larger scale beyond my conscience. The soa guys and the object guys, the test first guys and everyone else, the c# vs. vb jokes that are never funny. The oop crowd can be particularly annoying, as when, for example, someone tells you that a company and a person should have the same parent object since they both have phone numbers, satisfying the “is-a” rule for “contact.”

So, I’m reading your blog and come across this entry where you include the email from an oo guy, in full purist mode, grinding his teeth and frothing at the mouth because you’re both oo enthusiasts, yet csla is such a violation of good oo design. So, is the guy right? I think by the Westian Object Thinking view, he might be. But on the other hand, CSLA is a spectacular use of oo _technology_ (inheritance, polymorphism, and the other one, oh yeah, encapsulation), rather than an oo design (for behavior) show case. Well that’s the way it seems to me. Its not better, or worse, its different. Its solid, context bound design.

So, then, what is the context. Ahhhhhh. The context is “how to do object thinking in a Microsoft world”. One would think that, given all the oo rhetoric, there would be a legion of top guys trying to solve this problem. But there is only one. You. That’s why you’re so important in the push to have Microsoft data-binding work with objects. Why is that?

It’s because the visual studio and .net productivity tools are not really designed for object oriented programmers. What Microsoft wants to create and sell, and what business managers want to buy is tools for creating software that do not require a software developer. Well, the reason most oo designs (including mine) suck is because oo design is hard and requires talented developers who are expensive and probably require expensive managers and facilities.

So, if you’re doing web development, what do you do? You drag a grid on web form, and 10 clicks later you’ve got a page that allows you to handle a fair number of use cases. It takes 15 seconds and you’ve got a layered application [view -> data binding layer -> data-table layer with business logic -> data-adapter layer -> data store]. But, I say, its a crap design. I’m going to make a five layered [view -> presenter -> domain -> data access -> data store] object oriented version, with a fully oo compliant domain model, it will only take 6 months and cost $95,000. Not at this company pal, says my boss, and its over.

This is not indented as an anti Microsoft rant, its merely an explanation for why the data-binding to sql server with in memory data table technology (that is so sophisticated that it knocks your socks off) works so very well in visual studio, but why binding to objects, which is widely espoused as the only responsible data binding scenario, a trivial problem by comparison, has been so piss poor, even if we are getting real close. Its 2006 forchrisssakes. In a very real sense, oo design, and data-binding to objects just isn’t the vb way.

Enter Lhotka, the visual basic guy who likes oo. If you were a smalltalk guy, you would never have created csla. It takes a vb guy to do it, or at least that’s the way it seems to me. I was a vb user from vb 3 to vb 6.

So, to me, csla is like a wedge inserted into the ms workflow designed to create enough space for me to create a decent object model without loosing my job. How is this accomplished. The data store is fine - sql server. The data access layer is split, much of the orm goes into the stored procedures in the data store, and the db access, object creation, and hydration goes in the business layer. Is this business logic? Well, _I_ don’t think so. Does that mean I’m going to drop csla, or push all this logic out into a service or gateway layer, and implement my own data portal? Nope. The data portal mechanism is friggin cool and a use of oo technology that is beyond coolness. No, I’ll use csla, I’m all set. But I’m not going to pretend that data access and data gateway code is business logic, nor that static methods that trigger data access is consistent with oo dogma.

On the other side, the presentation layer is split, with much of it going into the business layer, and the rest going into the view. Now, all uppercase ID seems like a business rule to me. The conversion to uppercase is a business rule candidate. Shoot, its your business, you make the rules. It has a presentor/controller feel to me, assuming that the data is coming from a UI. But we already have presentation logic in our business model. Sure we do. n-level undo. Parent classes that support data-binding. In fact, as the data-binding from MS becomes more compelling, more presentation logic creeps into our business layer, and becomes “business rules” - like exposing data binding aware broken rules collections. Am I going to throw out csla because of all the presentation logic, and write presentation classes with undo capabilities? Nope. Am I going to have a presentation layer on my next java or non-csla .net project. Probably. Will I use data-binding? presentation classes? both? Maybe.

Data binding is cool, but it has been a double edged sword. It has not really been oo friendly. That might be changing. It is not friendly in web forms. I haven’t looked at WPF, but ValueConverter sounds smalltalk-ish (ValueAdapter), so maybe its more object friendly. A csla business object is chock-full of presentation and data access logic. Shoot, it’s got a DataSourceControl at one end and a SqlDataClient at the other end. Like a huge chunk of the code (behaviors) in my business object is determined before I even pick up a CRC card. In fact, if you want to do object thinking at all with csla, you kind of have to ignore the presence of all this data access and presentation logic.

So, yeah, data-binding is cool, and you can bind your objects directly to it -- as long as you’re ok with having presentation logic in your business layer. Static class data access methods and fully encapsulated data portal mechanisms are fine -- as long as you don’t want to use mock objects to avoid hitting and rehitting the database in your unit tests. A presentation layer is silly, but only as long as you have data binding on one side and csla business objects on the other, and are using the specialized csla data source control for your web apps. A data access layer is also silly, since you can inline data access code in your objects. And finally changing the values assigned to a property is ok, as long as it is in particular business object scenarios, but probably not ok if the property in question is a numerical property of some object involved in a numerical algorithm or simulation – if I put 3.14 into a property, it better be 3.14, not 3.141592, immediately after the assignment, on a single threaded object.

Although I’ve asserted that csla business objects contain presentation logic and data access logic, and this might appear as a criticism, I understand it as a strength. As you have suggested, I can put all the data access code in a separate assembly if I want to, and having UI compatible business objects really facilitates ui development. With Csla business objects, data-binding in front and sql server behind, I have a fighting chance of developing an object model without cutting off my nose to spite my face, and justifying my existence to my manager who can build a prototype of enterprise applications with data binding and sql data sources in a couple of hours thanks to the boys at Redmond.

Thanks Rocky. I hope I haven’t come across as disrespectful.

jsmith
Sunday, February 04, 2007 9:44:31 AM (Central Standard Time, UTC-06:00)
Not disrespectful at all. I think your characterization of CSLA .NET is pretty accurate, and you are absolutely correct that it is not “pure” in any sense. I prefer to think of it as “pragmatic”.

Yes, West has some great ideas, and I really enjoy his work. But he does presuppose you are writing code in a vacuum, and that you are creating _everything_ from scratch. That is not realistic in most environments, and it is REALLY unrealistic in the Microsoft environment where this incredibly rich and powerful set of framework technologies already exists.

I would pick a nit with how you portray the mixing of UI and data logic into business objects in CSLA though. I surely see what you mean, but I prefer to think that CSLA is implementing a set of abstract interfaces that are used for interaction with the UI and data layers.

On the UI side I think this is cleaner, largely because Microsoft has defined a pretty clear set of interfaces for data binding. I don’t think your objects contain UI logic just because they implement these interfaces. Rather, I think the opposite is true: they avoid having any substantial UI logic by implementing these interfaces, because the interfaces allow the UI logic to be externalized.

Clearly, having to create CslaDataSource for the web is not ideal. However, Microsoft also clearly anticipated this sort of scenario, because they defined interfaces and base classes specifically so people _could_ implement such a control. Heck, they do it themselves – look at the highly specialized site map data source control, which is entirely unneeded, but is really nice to have!

I do agree that on the data side things are less clear. In my mind this is largely because there’s no formal interface defined by Microsoft for a data access layer like there is for a UI layer. The result is that there are a wide array of competing interfaces/techniques: ADO.NET, nHibernate, Paul Wilson’s ORM mapper, LLBLGEN and soon ADO.NET EF and LINQ. And that’s the tip of the iceberg.

But the broader issue, on both ends, is elegance vs purity. Sure, a “pure” implementation on the UI side might have some sort of abstraction layer between data binding and the business objects. This layer would implement the _rest of_ the data binding interfaces so it could dynamically shape itself at runtime to “look like” your business object. Then data binding would be tricked into thinking it was talking to a CustomerEdit object, when it is really talking to the generic Csla.ObjectWrapper (this is entirely possible by the way). But it is far from clear that this extra layer of abstraction would provide enough value to offset the performance issues and complexity it would introduce.

And for me that’s the ultimate issue. I do want something that works. Specifically, something that enables the building of business applications in .NET that satisfies both the productivity needs of the business and the developers, and the architectural needs of your architecture group, your manager, etc. And I thought you summed all that up quite nicely in your post.
Sunday, February 04, 2007 7:01:51 PM (Central Standard Time, UTC-06:00)
That's why every dose of Fowler should be followed immediately by a dose of Lhotka, the “where the rubber hits the road” software theorist. You're a necessary voice in the MS world.

I'm going to pick your nit-pick regarding presentation logic in csla objects. You don't do it, and you don't advocate it. That's clear. The basic point I was making about binding to csla is that it is like the 'dark side', it draws the innocent and traps them. It takes inner-strength to use the power without being consumed by it. If you scroll up on this page, you'll find the following comment from a CSLA user:

> In the majority of business applications that I've put
> together, the same business object is used in multiple
> screens, forms, and services. The problem is that, usually,
> each one requires that different - conflicting - sets of
> business rules be placed on the same object.

Now, I’d wager that the root problem here -- the reason that these business rules are in conflict with one another -- is because they are not business rules on a business object. He’s got presentation logic in his domain model. I might even be ok with that, as long as it is understood. However, this snippet suggests that the developer is not even aware that these so called “business rules” may, after all, be presentation logic. If I bind my business object directly to three different screens, I have these semi-independent constraints that I have to satisfy. Well, they are “rules” related to the business objects. I guess they are business rules. I’ll put them in my customer object.

I know csla does not advocate this kind of shortcut, but it REALLY facilitates this type of infraction, and is _perhaps_ in some way responsible for it. In my mental space, this is as it should be – with great power comes great responsibility and all that. From an OO standpoint, these several conflicting sets of business rules imply several distinct behaviors. An OO developer should understand that, and recognize the need for several different objects to handle these several conflicting behaviors. Should he then have several customer csla objects. No, he should have several different presenters.

So, its possible that the rule here should not be: “don’t write a presenter, since data-binding will do a better job than a thin presenter layer and its free.” Perhaps it should be “use a presenter, unless you’re damn sure that you’re screens and objects don’t need them, and remember that data binding can be used, carefully, in conjunction with presenters.” I’d imagine that if a presenter was put in front of every business object, all kinds of “business rules” would suddenly transform themselves into “presentation logic” -- even, possibly, the toUpper on all IDs :).

Anyway, thanks for reading my comments, and responding. Its been fun for me, and, as always, educational.
jsmith
Tuesday, February 06, 2007 1:45:26 PM (Central Standard Time, UTC-06:00)
jsmith - You interpreted my comment fairly well with this comment: "If I bind my business object directly to three different screens, I have these semi-independent constraints that I have to satisfy. Well, they are 'rules' related to the business objects. I guess they are business rules. I'll put them in my customer object." But you missed the boat in your thinking that the rules are only presentation logic.

I do _not_ have presentation logic in my business layer, at least not in the example I had in mind when I wrote my response. <grin> The problem is the context in which the object is used. I'll use a very simple example.

Take a Contact, for example. Say the definition of a Contact is an object that holds someone's name, address, phone numbers, fax, employer's name, job title, etc. In the context of Sales Leads, the Contact's phone numbers are absolutely required. In the context of shipping, the address is required. The owner of the application says that Sales people and the shipping department must both be allowed to add Contacts. You see the problem right away. Sales people are not interested in, nor do they have the address. The Shipping department is not interested in, nor do they have, the phone number. Obviously, you cannot design the base Contact object to require either. Thus you run into the choices I outlined.

Are these UI rules? No, these are absolutely business rules. In my case, I would create separate SalesContact:Contact and Shipping:Contact objects to handle these rules. This way I can use only one form to handle the maintenance each. But I'm certainly not required to do so.

It is this type of scenario I was talking about. It happens on a regular basis to every business application developer. Of course, this is a very simple example. Generally what happens is that objects evolve over time so that the problem becomes much more difficult to see clearly. It takes a good deal of dedication and time on the part of the development team to avoid these issues.
Tuesday, February 06, 2007 5:23:13 PM (Central Standard Time, UTC-06:00)
Mark
I'm sorry. I thought the problem was one business object attached to three different "screens" -- which seems like a ui problem to me. However, now I see that you were concerned with several different business objects with a common base class -- a domain problem. I took "screen" as literally a WinForm. What you were talking about was more like a use case. My mistake.

jsmith
Wednesday, February 07, 2007 8:33:46 AM (Central Standard Time, UTC-06:00)
No, jsmith, you had it right. I _wanted_ to use the same business object on three screens. It's just that my example didn't go that far... And yes, it does _seem_ like a UI problem, but the fact is that conflicting business rules can apply to the same object because of the context in which it is being used. My answer to this is usually to create a different business object that inherits from the base. What I generally end up with is a "rules" layer in my architecture; which can get pretty wide for some objects. I hope this helps in understanding my point. But even creating objects this way, some rules can get duplicated quite easily. What's a developer to do? You want to do things "right," but you want to keep getting projects, too. As you pointed out, there's a balance between keeping development costs down and building a totally OO system.

I've always had the opinion that OO - for most business applications - just doesn't fit. No matter how hard I try, I nearly always end up with a mix of OO and precedural code; if for no other reason that it just makes more sense.
Wednesday, February 07, 2007 2:44:45 PM (Central Standard Time, UTC-06:00)
We do what we can. I find that I tend to under-use objects and over-use inheritance. I think its an unconscious attempt to minimize lines of code, and perhaps a slavish devotion to a bad interpretation of the Don't Repeat Yourself principle. So I end up with an essentially procedural line of execution in my applications, and my code gets procedural. As far as I can see, there is no way out without allowing objects (and classes) to proliferate, but this takes a different mind set, and oo patterns/tricks like inversion of control hide the lines of execution, making it difficult to follow the application logic. (Following application logic is probably a procedural mindset). If someone like, say, Martin Fowler, was to sit down with smalltalk, and I sat down with visual studio, each to solve the same problem, the solutions would be radically different. There is no doubt that Fowler's application would be better than mine. On the other hand, if I try to imitate Fowler, the software I write might be truely awful. Perhaps it is not the business application, but me, the business application developer, that is a misfit for writing pure oo apps.
jsmith
Comments are closed.