A
Anthony
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.
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.