Saturday, April 6, 2013

Submitting only selected entities


There are times when you have contact group of so many beautiful chicks (I am waiting for such time in my life too), and among that group you want to text only to selected ones who are more cute than others. What you do is to reject the ones who are less cute and whom you don’t like to text, resulting in a list of damn cute girls whom you like. Once you have the list, you are ready to text.

While dealing with data in silverlight, you have entities, you have Domain Context, you have lot of stuff to carry on with. Sometimes there are so many entities that have changes in them and you want to submit the changes of any selected entity instead of all, what you do is you reject the changes of the entity you want not to be submitted. To demonstrate this, let’s go through with a demo.

As you know, we always start from scratch, so let’s create a new silverlight project, RejectChanges. Also create a database TestingEntities, having two tables, Location and Food. Here is my tables structure:

image


 

 

 

 

 

image



 

 

 

 

 

Now in the silverlight project add an EntityDataModel based on the database created above. You can follow the step mentioned in Creating Entity Data Model from database. Once you have created the model, you need to create a domain service basing it on the model that you have added recently. The steps of creating a domain service class, you can get them from here. All set up now. We are ready to go.

As far as design is concerned, in mainpage.xaml I have added few textboxes. The XAML of which is below:

   1: <UserControl x:Class="RejectChanges.MainPage"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   5:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   6:     mc:Ignorable="d"
   7:     d:DesignHeight="300" d:DesignWidth="400">
   8:  
   9:     <Grid x:Name="LayoutRoot" Background="White">
  10:         <TextBlock HorizontalAlignment="Left" Margin="51,70,0,0" 
  11:             TextWrapping="Wrap" Text="City" VerticalAlignment="Top"/>
  12:         <TextBox HorizontalAlignment="Left" x:Name="txtCity" Height="23" Margin="51,86,0,0" 
  13:             TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="200"/>
  14:         <TextBlock HorizontalAlignment="Left" Margin="51,114,0,0" 
  15:             TextWrapping="Wrap" Text="Speciality" VerticalAlignment="Top"/>
  16:         <TextBox HorizontalAlignment="Left" x:Name="txtSpeciality" Height="23" Margin="51,130,0,0" 
  17:             TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="200"/>
  18:         <TextBlock HorizontalAlignment="Left" Margin="51,163,0,0" 
  19:             TextWrapping="Wrap" Text="Address" VerticalAlignment="Top"/>
  20:         <TextBox HorizontalAlignment="Left" x:Name="txtAddress" Height="50" Margin="51,179,0,0" 
  21:             TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="200"/>
  22:         <Button Content="SubmitAll" HorizontalAlignment="Left" Margin="223,242,0,0" 
  23:             VerticalAlignment="Top" Width="93" Click="SaveAll_Click"/>
  24:         <Button Content="Location Only" HorizontalAlignment="Left" Margin="124,242,0,0" 
  25:             VerticalAlignment="Top" Width="84" Click="LocationOnly_Click"/>
  26:         <Button Content="Speciality Only" HorizontalAlignment="Left" Margin="15,242,0,0" 
  27:             VerticalAlignment="Top" Width="99" Click="SpecialityOnly_Click"/>
  28:     </Grid>
  29: </UserControl>

To keep the concept vivid and distinct, I have added three buttons, SubmitAll, Location Only and Specialty Only. While SubmitAll will submit all the changes, Location Only and Specialty Only will submit the changes to the respective entities mentioned in their names. Awesome till now? Next we are going to write some code, some C#.

Here is the entire code of mainpage.cs I have:

   1: public partial class MainPage : UserControl
   2: {
   3:     RejectDomainContext _context = new RejectDomainContext();
   4:  
   5:     public MainPage()
   6:     {
   7:         InitializeComponent();
   8:     }
   9:           
  10:     private void AddItemsToContext()
  11:     {
  12:         var city = txtCity.Text.Trim();
  13:         var speciality = txtSpeciality.Text.Trim();
  14:         var address = txtAddress.Text.Trim();
  15:         var foodEn = new Food();
  16:         var locationEn = new Location();
  17:         locationEn.City = city;
  18:         locationEn.Address = address;
  19:         foodEn.Speciality = speciality;
  20:         _context.Foods.Add(foodEn);
  21:         _context.Locations.Add(locationEn);      
  22:     }
  23:           
  24:     private void SubmitOnlyFood()
  25:     {
  26:         ((IRevertibleChangeTracking)_context.Locations).RejectChanges();
  27:         SubmitAll();
  28:     }
  29:  
  30:     private void SubmitOnlyLocation()
  31:     {
  32:         ((IRevertibleChangeTracking)_context.Foods).RejectChanges();
  33:         SubmitAll();
  34:     }
  35:  
  36:     private void SubmitAll()
  37:     {
  38:         _context.SubmitChanges();
  39:     }
  40:  
  41:     private void SaveAll_Click(object sender, RoutedEventArgs e)
  42:     {
  43:         AddItemsToContext();
  44:         SubmitAll();
  45:     }
  46:  
  47:     private void SpecialityOnly_Click(object sender, RoutedEventArgs e)
  48:     {
  49:         AddItemsToContext();                       
  50:         SubmitOnlyFood();
  51:     }
  52:  
  53:     private void LocationOnly_Click(object sender, RoutedEventArgs e)
  54:     {
  55:         AddItemsToContext();
  56:         SubmitOnlyLocation();
  57:     }
  58: }

The whole code is self explanatory, the only functions of our interest are SubmitOnlyFood() and SubmitOnlyLocation(). IRevertibleChangeTracking is an interface that track whether the mentioned entity has any changes or not. It possess two methods,

  • AcceptChanges
  • RejectChanges

As it is clear from the name itself what each method stands for. In the two functions above, what we are doing is keeping only the changes of entity of interest and rejecting the changes from other. In this way when we commit submitchange operation in the domain context, the entity whose changes are not rejected get submitted.
So it is possible to submit entity specific changes.

Happy Reading!!!

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top