2

I have a model class like below. I need to edit items, but the model has to know when any object has been modified. Also I need to provide very often with the objects inside to the view, which is who's going to eventually edit them. That's why I need an edit method but I don't know a proper way to do it.

public class Container {
    private List<Item> items;

    public Item get(int index) {
         return items.get(index);
    }

    //edit method here
    //notify edit
}
  • You want container to be aware that an an item in items has changed since last time it was accessed or you want container to be called when an item changes so you can do something then? – candied_orange Jun 7 '16 at 13:52
  • More like if the only way of editing an item were through a container's method, so that it can do actions when any item was edited like update the view or update another list inside it. – germanfr Jun 7 '16 at 17:14
  • I feel like I'm asking you what you need and instead of answering your telling me how your going to do it. Yes, if the items are fully encapsulated the container can force all attempts to change an item to go through the container. That's the decorator pattern. But I still don't have any reason to think that's what you need. It would help if I know why anything needs to know the items have been changed. – candied_orange Jun 7 '16 at 21:06
  • Because Item objects have one field that is in another collection in the model. If one changes the other must update, so that's why I need to know. – germanfr Jun 8 '16 at 10:01
1

The decision of whether something has been modified or not is best left to a collection of objects that can perform change-tracking or dirty-checking. Your view model is not the best place for this.

Having a collection of classes called your "Domain Model" containing business logic and utilizing an Object Relational Mapper (ORM) can do the dirty checking of the Domain Models instead of the view model.

Germán said:

It is not the view model. Its the model of the mvc.

Ruby on Rails first popularized this, but having one collection of "models" for your entire application has some drawbacks, mainly that display and formatting logic gets mixed in with business logic. This is why in recent years "view models" have become more popular, with "domain models" containing the business logic. If you do have this separation, the change tracking you desire can be accomplished with an ORM like Entity Framework or NHibernate.

Object Change Tracking and Optimistic Concurrency from Microsoft starts in the right direction, but is a bit much for your application. Really, searching for c# object change tracking in your search engine of choice can give you some good examples. After a quick search, the ChangeTracking NuGet package looks like a good candidate.

The main idea is that something else is monitoring the values of your object before and after an operation to see what has changed, so that your models/view models/domain models don't have to manually detect these changes.

Germán commented:

The model is based on events. A dirty check should be performed, but when? Something could go wrong if I try to access data between an edition and a dirty check.

In that case, reading the Microsoft article I posted above might be your best option. Judging by the names of your methods in your code example, you appear to be using Java. While the Microsoft article focuses on C#, the same basic principles apply to Java. A search for java object change tracking might also lend itself to more specific solutions for your technology stack.

  • It is not the view model. Its the model of the mvc. – germanfr Jun 7 '16 at 17:12
  • The model is based on events. A dirty check should be performed, but when? Something could go wrong if I try to access data between an edition and a dirty check. – germanfr Jun 8 '16 at 9:59
0

Because Item objects have one field that is in another collection in the model. If one changes the other must update, so that's why I need to know

That's the observer pattern. I'm not sure if you're dealing with a 1 to 1 or a 1 to many since you've indicated that there are two collections but the observer pattern is basically one thing keeping a collection of all things that care about it's state and telling those things when that state changes. Those things could just be one thing if 1 to 1 is what you need.

Nothing says you couldn't have three collections going by the time your done. Just be sure each has a good reason to exist.

  • So i'd have to add the model as observer to each item returned by get? – germanfr Jun 8 '16 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.