New Posts New Posts RSS Feed: Default Item
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Default Item

 Post Reply Post Reply
Author
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Topic: Default Item
    Posted: 22-Jun-2012 at 6:34am
I have a entity with a related items list. If there are no related items, I want to create a default one.

For example, I have a Contact entity. There is a related list of email addresses. If there is no related email addresses, I want to create a dummy so that the GUI shows an empty item. That way, the user can start typing data in directly.

If the user never inputs anything, then it doesn't need to be saved.

This would be similiar to the Enumerable.DefaultIfEmpty Method.

Any suggestions appreciated.

Greg

Edited by gregweb - 22-Jun-2012 at 6:39am
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 22-Jun-2012 at 10:15am
First off, you don't want to add any of this logic to the entities. This is purely UI logic, so this should be handled in a ViewModel.

So, off the top of my head, once the ViewModel finishes loading the related entities, preferably asynchronously, and the collection is empty, the ViewModel can simply add an empty email address entity. Before you save you need to remove it again if the user hasn't entered anything. One way to do that is in the Saving event of the EntityManager you go through the list of entities to be saved and simply remove any empty email address entity. That will leave the empty email address in the cache, so it's still there in the UI, but it won't save it.

Now, the problem with this is that you'll constantly have pending changes in the cache, that are not real. You'll be tripping over them for example when you want to enable the Save button on the UI at the right time. Your UI may think that there's something to save, when in fact there is not.

So, perhaps the better way is to keep the email addresses in a separate ObservableCollection in the ViewModel. Then, if the user actually enters something, you add an entity corresponding to the item in the UI. That way you keep this stuff completely in the UI and don't start mucking with the cache and dirty states that may cause trouble down the road.
Back to Top
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Posted: 22-Jun-2012 at 10:56am
Thanks. Good point about it being a GUI issue. What I did was in the SelectedItemChanged method, I check the status of the related items and add one if it is empty. Then the GUI always has at least one to dipslay. I then do a save right away, so the Save button lights up only if the user has actually entered something after that.

That leaves an empty record, but that's really not a problem.

Greg
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 22-Jun-2012 at 11:35am
Your system tolerates empty records?! What kind of validation do you do? You are creating extra work for everybody down the line having to filter out those bogus empty records. Is that really what you want to happen?
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down