Solved

Entity Framework object edit/add dialog window

Posted on 2010-09-10
9
305 Views
Last Modified: 2012-05-10
I am using the Entity/Framework and with SQL & LINQ to create a new application.  I have run into a dilemma and after much searching around on the internet I haven’t been able to find the “best practice” that I am looking for.  Here is the situation.

Most of the EF examples keep it pretty simple.  
1)      Put a grid on your form
2)      Bind the grid to a basic linq query
3)      Allow the user to edit the data right in the grid

I am finding that I get into more complex areas of the application I need to add another step to this.  Instead of letting the user edit the data right in the grid, I want to have then double click it (or select it in some other method) and open a dialog window to edit the values for the object.   That would allow for more validation and  a better UI for end users.  

I have done this a couple different ways, usually by passing the EF Context object and the object to edit to the dialog.  I feel as though I am making it harder than it should be.  Does anyone have any good examples of the “best practice” for editing & adding EF objects via dialog windows rather than in the grids themselves?

Thanks in advance..
0
Comment
Question by:MRS
  • 6
  • 3
9 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
The following sample is done in WPF..

Window2.xaml
public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
            this.DataContext = this;
            Ent = new FinancialsEntities();
        }


        public FinancialsEntities Ent { get; set; }
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Window3 wind = new Window3();
            wind.DataContext = this.SelectedItem;
            wind.ShowDialog();
            if (wind.Cancelled)
            {
                Ent.Refresh(System.Data.Objects.RefreshMode.StoreWins, this.SelectedItem);
            }
        }

        public object SelectedItem { get; set; }

        public IQueryable Items
        {
            get
            {
                return from tetmp in Ent.Accounts select tetmp;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Ent.SaveChanges(true);
            this.Close();
        }
    }

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
Window2.xaml
<Window x:Class="Tes1234.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="24"/>
        </Grid.RowDefinitions>
        <ListView Name="listView1" SelectedValue="{Binding SelectedItem}" ItemsSource="{Binding Items}">
            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Edit" Click="MenuItem_Click"/>
                </ContextMenu>
            </ListView.ContextMenu>
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding AccountNo}"/>
                    <GridViewColumn DisplayMemberBinding="{Binding AccountName}"/>
                </GridView>
            </ListView.View>
        </ListView>

        <Button Width="120" Height="24" HorizontalAlignment="Right" Click="Button_Click" Content="Save" Grid.Row="1"/>
    </Grid>
</Window>

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
Window3.xaml
<Window x:Class="Tes1234.Window3"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window3" Width="350" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="26"/>

            <RowDefinition Height="26"/>

            <RowDefinition Height="26"/>

        </Grid.RowDefinitions>



        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="125"/>

            <ColumnDefinition/>

        </Grid.ColumnDefinitions>



        <TextBlock Text="Account No:"/>

        <TextBox Text="{Binding Path=AccountNo}" Grid.Column="1"/>

        

        <TextBlock Text="Account Name:" Grid.Row="1"/>

        <TextBox Text="{Binding Path=AccountName}" Grid.Column="1" Grid.Row="1"/>

        

        <Button Click="Button_Click" Width="75" Height="24" HorizontalAlignment="Right" Content="Ok" Grid.Row="2"/>

        <Button Click="Button_Click_1" Width="75" Height="24" HorizontalAlignment="Right" Content="Cancel" Grid.Row="2" Grid.Column="1"/>

    </Grid>

</Window>

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
Window3.xaml
public partial class Window3 : Window

    {

        public Window3()

        {

            InitializeComponent();

            this.Cancelled = true;

        }



        private void Button_Click(object sender, RoutedEventArgs e)

        {

            this.Cancelled = false;

            this.Close();

        }



        public bool Cancelled { get; set; }



        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            this.Close();

        }

    }

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:MRS
Comment Utility
I am not super familiar with WPF, what does the datacontext object encapsulate?  Is that a reference to the Context Object?
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
It is not the Context object. It just holds the selected item which in this case is Accounts. We are not passing the EntityContext object to the second window but just the Selected Account item.
0
 

Author Comment

by:MRS
Comment Utility
So in that can how would you go about using that dialog window for creating a new "Account" item.  Would i pass the a new instantiation of an "Account" item to the window and only add it to context object upon a successfull "OK" click?

If the above is the case it sounds like the "best practice" here is to make the dialog windows as dumb as possible and keep all of my hard logic in my parent form.  The dialog window is really only meant to allow the user to change values.  Committing to the database and other logic like that would remain on the parent window.

Does that sound right?
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
Comment Utility
Yes, thats how it is normally done. As you said, for adding a new account, we will create a dummy account and pass it to the second form and upon user confirmation (clicking ok), it gets added to the context. Also, as you have observed, this will make the child form relatively "dumb".
0
 

Author Closing Comment

by:MRS
Comment Utility
Thank you for taking the time..  
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now