views:

66

answers:

1

Hey guys, new to WPF and really struggling with 'the right way' to do things...

public void Save(CompanyContact entityToSave)
{
  try
  {

    var saveEntity = (from cc in db.CompanyContacts
        where cc.CompanyContactId == entityToSave.CompanyContactId
        select cc).SingleOrDefault();

    if (saveEntity == null)
    {
      //INSERT logic                    
      entityToSave.CreatedById = new CompanyPersonRepository().GetCompanyPerson(DataContext.Default.LoginUsername).CompanyPersonId;
      entityToSave.ModifiedById = entityToSave.CreatedById;

      db.CompanyContacts.InsertOnSubmit(entityToSave);
      db.CompanyContacts.Context.SubmitChanges();
    }
    else
    {
      //UPDATE logic            
      saveEntity.ModifiedById = new CompanyPersonRepository().GetCompanyPerson(DataContext.Default.LoginUsername).CompanyPersonId;
      saveEntity.CompanyId = entityToSave.Company.CompanyId;
      saveEntity.FirstName = entityToSave.FirstName;
      saveEntity.LastName = entityToSave.LastName;
      saveEntity.CompanyContactTypeId = entityToSave.CompanyContactTypeId;

      db.CompanyContacts.Context.SubmitChanges();

    }

...

if not can you please provide some comments on why it is not, or provide an example of a better way to write LINQ functions if I am not on the right path?? (thanks)

A: 

Hey,

That would work; you can also store the record in the form as a variable, and if its null, the record doesn't exist, but if it does, an update needs to occur. That way, you can minimize your queries, and you can simply assign form values to one object rather than dealing with two.

Alternatively, if you bind a LINQ object to the form, since it implements INotifyPropertyChanged, any changes are immediately bound to the object if you setup the bindings within the form and assign the object as the datacontext.

HTH.

Brian
Thanks for the comments... I am still really new to this so hope you can shed some light on some issues for me:What do you do if there is a violation of a database rule? I identified a problem when I ANSWERed my QUESTION posted here:http://stackoverflow.com/questions/2300159/wpf-binding-issue-unique-constraint-violation-on-update-how-to-reject-changesIs there a setting value I can provide to LINQ so I don't need to catch SqlException and Refresh the collection of objects if a database rule is violated... it seems silly that I need to write code to catch and handle db rule violations...
Skyguard
Hey well what I've done in the past is that each LINQ column has a [Column()] definition (of type ColumnAttribute) that you can use reflection to pull the fields down and check things like length, type, nullability, etc... if you don't want to go that route, you can setup your own custom validation before you save and flag the users of those errors. Other than that, yes, you have to catch the exception. What I do is create a context solely for that request, try to save, if an error occurs, notify the user and let that context die.
Brian
Continuing... that way, when they go to save again, you just reconstruct the objects and work where you left off. After all, the form still has the underlying data.
Brian