Wednesday, August 18, 2010

Design problems in our view model?

The following code is found in the view model. An object is serialized, marked exported and persisted to the repository.

private void Export(object obj)
{
    var item = obj as IItem;           
    _itemSerializer.CreateCustomerItemXML(item);
    item.IsExported = true;
    _repository.Save(item);
}

There might be a problem with this function that I want to discuss a little. There is also an ExportAll method which iterates through all items not exported and persists that to the database.

As you probably already noticed there are no mechanics in the Export functionality that captures the fact that the item is already exported before it saves it. That means that you can select any item and re-export it any time you like, even after you've done changes to the item.

But in my opinion this is just weird behavior and a recipe for disaster since

  • If you edit an item and invoke ExportAll the edited item updated but not exported.
  • If you edit an item and invoke Export the edited item overwrites the existing data.

Also this code is found in the view model and a RelayCommand is instantiated so that we can use this code in the xaml view. But what if we want to use this code somewhere outside of this view? Another service. Shouldn't this logic be deeper and available at a lower more common level?

If I do changes to the logic in this method the changes will only be valid in the view model, but maybe this is ok? I hoping that I'm not creating technical debt in doing modifications to this method to throw exception when trying to commit data to the repository when trying to export items already having been exported.

No comments:

Post a Comment