Databinding - Best Practice (object-oriented)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Anthony (anthony_at_netcourrier.com)
Date: 05/31/04

  • Next message: Tim A.: "Re: Databinding - Best Practice (object-oriented)"
    Date: 31 May 2004 11:19:32 -0700
    
    

    Hi,

    I would like to know your opinions about the best way of implementing
    databinding in an application.

    Let's take a simple example: we have a table called 'Employee' and we
    would like to display its content in a DataGrid.

    1st solution:
    -Export all the Employee records from the database into a datatable.
    -Bind the datatable to a datagrid.
    -The user can insert/delete/edit records of the datatable through the
    datagrid and we can easily save the data back to the database.

    2nd solution (object oriented). Here I consider than an employee is an
    object.
    -Create a class called 'Employee'.
    -Export all the Employee records from the database into a datatable.
    -Create an instance of the Employee class for each record of the
    datatable.
    -Store all the instances in a collection (eg. ArrayList).
    -Bind the collection to a datagrid

    The 1st solution works very well and is easy to implement but is not
    object-oriented.
    The 2nd one seems to be the logical way of doing it (creation of a
    'Employee' class etc) but involves developing much more code. Besides
    it is not possible to add new employee through the DataGrid unless my
    collection implements the IBindingList interface which is not
    straightforward to do and also involves developing even more code.

    I would tend to use the 1st solution as it's easy to implement and it
    works fine but I can't help but think that it's not the right way of
    doing it. For example if every time an employee is created or modified
    in the DataGrid we want to do some checks (for example that the
    FirstName and the LastName are different) in the 2nd solution I would
    put this business logic in the 'Employee' class and I would be sure
    that whoever uses my 'Employee' class will use the correct business
    logic. If I use the 1st solution, I would have to use something like
    the ColumnChanged event of the datatable to check what the user has
    entered and therefore this business logic will be mixed with the code
    of the client application which will make the code more difficult to
    maintain.

    My example is quite simple but you get the idea. What's the best way
    of doing the above in your opinions?

    Thanks,

    Anthony.


  • Next message: Tim A.: "Re: Databinding - Best Practice (object-oriented)"

    Relevant Pages

    • Re: Databinding - Best Practice (object-oriented)
      ... > provides all the databinding stuff you need to do exactly what you want. ... >> I would like to know your opinions about the best way of implementing ... >> datagrid and we can easily save the data back to the database. ... For example if every time an employee is created or modified ...
      (microsoft.public.dotnet.framework.windowsforms.databinding)
    • Re: Databinding - Best Practice (object-oriented)
      ... > provides all the databinding stuff you need to do exactly what you want. ... >> I would like to know your opinions about the best way of implementing ... >> datagrid and we can easily save the data back to the database. ... For example if every time an employee is created or modified ...
      (microsoft.public.dotnet.framework.windowsforms)
    • Databinding - Best Practice (object-oriented)
      ... I would like to know your opinions about the best way of implementing ... we have a table called 'Employee' and we ... would like to display its content in a DataGrid. ... -Export all the Employee records from the database into a datatable. ...
      (microsoft.public.dotnet.framework.windowsforms)
    • Re: DataGrid Row Searching
      ... would go through each DataRow and set the RowError property if it is invalid, ... How do I select a cell in a datagrid based on its primary key/ID, ... I have a parent table (Employee) with two child tables ...
      (microsoft.public.dotnet.framework.adonet)
    • Re: DataGrid Row Searching
      ... dim dv as DirectCast ... that index is the same as the row in a DataGrid. ... How do I select a cell in a datagrid based on its primary key/ID, ... I have a datagrid bound to the dataset, with datamember = Employee ...
      (microsoft.public.dotnet.framework.adonet)