views:

124

answers:

4

I have a Silverlight 4 application for a simple 'TODO' list. The problem I'm having is that databinding is hooking up relationships on my TODO object, which causes the RIA data context to add it to the DataContext.TODOs list before I want it there. I want to treat the object as new and detached until I'm explicitly ready to add it to the datacontext.


Here's how it works : I've got my TODO entity which is associated with a Status (RIA services entity relationship).

I create a new TODO() entity which is passed to a ChildWindow popup. Notice that I don't add this new entity to my datacontext.

 new CreateTODOPopup(new TODO()).Show();

In the DataForm in my ChildWindow I have a combobox for Status which is databound to DataContext.Statuses.

The problem is that the action of selecting a Status from the dropdown actually associates the entity to the context for me - ending up giving it a state of EntityState.New and actually adding it to the DataContext.TODOs colleciton.

This would be fine except that it now appears in the main TODO list in the main frame. I don't want this becasue it hasn't been committed by the ChildWindow yet.

How can I solve this? Either by preventing the entity from becoming attached - or by somehow hiding it from any controls it is bound to until it has been added.

A: 

Have you tried using Context.Detach on the object so that you clearly specify that it shouldn't be in the context? Then you can Context.Attach it again before you save it.

Einarsson
i never explititly attach it, it just attaches itself. i'm not sure when i would detach it
Simon_Weaver
+1  A: 

You should be able to get the behavior you want by adding another 'MyProject' property to the TODO entity using a partial class. In your popup you could set the 'MyProject' property instead of the 'Project' property. When you save your TODO, you could then apply the 'MyProject' value directly to 'Project'. A little circuitous, but it should give you the behavior you'd like.

Kyle

Kyle McClellan
+1 for sneaky. certainly not scalable but interesting an thought
Simon_Weaver
An alternate approach might be to create the association using StatusId instead of Status. I've seen different behaviors from the two approaches.
Kyle McClellan
+1  A: 

I guess one way would be to use a PagedCollectionView and filter out 'New' entities - but there has to be something obvious you're missing.

    // data bind list to this ICollectionView
    private PagedCollectionView _projects;
    public PagedCollectionView Projects
    {
        get
        {
            if (_projects == null)
            {
                _projects = new PagedCollectionView(_todoDomainContext.TODOProjects)
                {
                    Filter = i => {

                        DM.TODOProject proj = (DM.TODOProject)i;

                        // hide New entities
                        if (proj.EntityState == EntityState.New)
                        {
                            return false;
                        }

                        return true;
                    }
                };
            }
            return _projects;
        }
    }
Simon_Weaver
oops. i wrote this in the third person... to myself... :-/
Simon_Weaver
A: 

IMPORTANT: After 3 days of struggling with a horrible race type condition it turned out to be directly related to this problem. Basically the conclusion is - if you're trying to create entities and not add them to the data context then don't if you don't want race conditions and unexpected behavior. I'm pretty sure this is a RIA services bug.

What I was doing :

  • Creating a new TODO() and passing it to my view
  • Allowing the RIA services framework to associate my TODO with all the foreign key tables, such as Status and AssignedTo
  • On 'save' adding the TODO() to the list if it wasn't already present in the DataContext.TODOs entity set.

What the framework did by itself:

  • When the View sets the foreign keys on the object (via a comobox) it would automatically add the TODO to the DataContext.TODOS collection. This is just the way entities work.

Why this is bad:

  • When navigating around my UI some horrible race condition was occuring.
  • Preexisting rows (even those that existed before my app started) were marked as New - sometimes up to 20 of them and then getting resaved as new duplicate rows.

How i fixed it :

  • Always add created entities to the data context immediately on creation.

Here's some sample code for adding entities immediately on creation - no race condition:

for (int i = 0; i < 3; i++) 
{
    var entity = new DM.TODO();
    _todoDomainContext.TODOs.Add(entity);

    entity.TODOStatu = pendingStatus;
    entity.TODOProject = project;
    entity.TODOCompany = company;

    entity.CreateDt = DateTime.Now;

    entity.Title = "generated todo " + DateTime.Now.ToString();
    entity.Details = "12345"; 
}

This code did NOT work, and causes race conditions - adding entities after foreign key constraints have already been set:

for (int i = 0; i < 3; i++) 
{
    var entity = new DM.TODO();

    entity.TODOStatu = pendingStatus;
    entity.TODOProject = project;
    entity.TODOCompany = company;

    entity.CreateDt = DateTime.Now;

    entity.Title = "generated todo " + DateTime.Now.ToString();
    entity.Details = "12345"; 

    _todoDomainContext.TODOs.Add(entity);
}
Simon_Weaver