Link to home
Start Free TrialLog in
Avatar of MRS
MRSFlag for United States of America

asked on

Entity Framework object edit/add dialog window

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..
Avatar of Gautham Janardhan
Gautham Janardhan

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

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

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

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

Avatar of MRS

ASKER

I am not super familiar with WPF, what does the datacontext object encapsulate?  Is that a reference to the Context Object?
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.
Avatar of MRS

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MRS

ASKER

Thank you for taking the time..