views:

269

answers:

4

I want to have a save button that is only enabled when the view isdirty. How should I approach this?

My particular situation is a WinForms application using .Net 2.0. I have a service layer that the presenter calls. The service layer returns a screen bound DTO.

Is it ok to bind the view to this DTO and have the DTO implement an isDirty property? Or should I unload the data from the DTO into another object specifically designed for presentation purposes e.g. viewmodel?

+5  A: 

The view should never be dirty. Only your model.

Then you can simply have an event that triggers when your model becomes dirty and one for when it becomes clean.

Pace
That was my initial thinking. The model is what would become dirty and the view could reflect that fact. So I would then assume that the model would have to implement a isdirty property.
David
A: 

Well could you make all of your controls have auto postback on changes enabled where they call _presenter.MarkDirty() or some similar method.

Or you could allow the save button to be used but if the object is clean to then respond with a no changes made dialog or similar.

Chris Marisic
Autopost back might not be the most correct word in WinForms but it should convey my point atleast.
Chris Marisic
A: 

I agree with @Pace that only the model should be dirty.

I just want to add here (as this seems to be about .net) that you can use CSLA for the implementation of your model, and you get IsDirty funcionality (and a lot of other things) for free.

Update:

Is CSLA not about the business layer?

Yes indeed, but I think an IsDirty method belongs in the business layer. You speak of implementing an IsDirty on your DTO, but the moment you do that, the object isn't a DTO anymore (as it does more than transferring data).

Also, you could use CSLA as an intermediate layer between your DTO's and your presentation layer, although this would be a little too much overhead if your intention is only to use the IsDirty functionality.

The point is: The moment you start putting 'functionality' into your DTO's, I don't see why you wouldn't expose CSLA objects from your business layer in stead of simple DTO's.

fretje
Is CSLA not about the business layer? The question I've posed is about presentation. My Business objects are not exposed to the presentation layer.
David
@fretje I agree that as soon as I put a isDirty property into a DTO then its no longer a DTO. So on this basis I would assume that doing so is indeed wrong, which is why I asked if it would then be a case of extracting the data from the DTO and pushing it into another object for presentation purposes. My particular implementation does not expose domain objects to the presentation layer as I quite liked this approach although I know there is much debate on this subject.
David
A: 

Here is what I do, the view itself can track if the user has attempted to modify the data. When this happens it can notify the presenter that the view data has changed and the presenter can act on this event to enable/disable the save button. This is the simplest solution I have found so far otherwise you have to have change tracking on the DTO/Data object itself.

rah