I want to detach the LINQ to SQL / LINQ to Entity Data Context from my presentation layer
-
Hello, I realize that, DatabaseContext class generated by LINQ to SQL / LINQ to ENTITY should not be a part of the Views and so I want to encapsulate the uses of DatabaseContext classes within my Data Access Layer. So, when I need to display a set of records in a GridView in Windows Form, I can simply let my Data Access Layer return the
List<Person>
of the records and display that records in GridView without any problem. Great! Here is my Person classpublic class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}Ok, now, I face the big problem when I need to save the changes back to the database. I may have 1000 records displaying in the Grid view where the GridView is bound to a BindingSource. My user can change only 2-3 records from the GridView and delete some of the records as well. How can I tell the LINQ to SQL / LINQ to Entity (data context) about the changed records so that when I pass the
List<Person>
object to my Data Access Layer, Data Access Layer can identify which records needs to be updated and finally the Data Context updates those records ? Can you please show me some pattern for this problem ? Regards. -
Hello, I realize that, DatabaseContext class generated by LINQ to SQL / LINQ to ENTITY should not be a part of the Views and so I want to encapsulate the uses of DatabaseContext classes within my Data Access Layer. So, when I need to display a set of records in a GridView in Windows Form, I can simply let my Data Access Layer return the
List<Person>
of the records and display that records in GridView without any problem. Great! Here is my Person classpublic class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}Ok, now, I face the big problem when I need to save the changes back to the database. I may have 1000 records displaying in the Grid view where the GridView is bound to a BindingSource. My user can change only 2-3 records from the GridView and delete some of the records as well. How can I tell the LINQ to SQL / LINQ to Entity (data context) about the changed records so that when I pass the
List<Person>
object to my Data Access Layer, Data Access Layer can identify which records needs to be updated and finally the Data Context updates those records ? Can you please show me some pattern for this problem ? Regards.Well, see, the smarter option is not to allow editing in a grid. Have a form that displays and allows updates to one record and if the user clicks the "Save" button, that one record gets saved to the database.
-
Well, see, the smarter option is not to allow editing in a grid. Have a form that displays and allows updates to one record and if the user clicks the "Save" button, that one record gets saved to the database.
I see ! So, if I want to expose an Excel Spreadsheet like facility to my User, I cannot follow n tier pattern :( thats sad. But anyway, thank you for giving me the idea.
-
Hello, I realize that, DatabaseContext class generated by LINQ to SQL / LINQ to ENTITY should not be a part of the Views and so I want to encapsulate the uses of DatabaseContext classes within my Data Access Layer. So, when I need to display a set of records in a GridView in Windows Form, I can simply let my Data Access Layer return the
List<Person>
of the records and display that records in GridView without any problem. Great! Here is my Person classpublic class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}Ok, now, I face the big problem when I need to save the changes back to the database. I may have 1000 records displaying in the Grid view where the GridView is bound to a BindingSource. My user can change only 2-3 records from the GridView and delete some of the records as well. How can I tell the LINQ to SQL / LINQ to Entity (data context) about the changed records so that when I pass the
List<Person>
object to my Data Access Layer, Data Access Layer can identify which records needs to be updated and finally the Data Context updates those records ? Can you please show me some pattern for this problem ? Regards.You need some kind of mapper to map the data properly. After making the changes on the grid, get the currently available
Person
instances from grid's datasource and compare it against the list that you load from database. You will need some unique identifier on thePerson
type which can be used to find out the proper domain object. Given the two lists, you can do a diff and find out newly added, updated and deleted records. :)Best wishes, Navaneeth
-
You need some kind of mapper to map the data properly. After making the changes on the grid, get the currently available
Person
instances from grid's datasource and compare it against the list that you load from database. You will need some unique identifier on thePerson
type which can be used to find out the proper domain object. Given the two lists, you can do a diff and find out newly added, updated and deleted records. :)Best wishes, Navaneeth
Thanks for your reply. Sounds like a lots of tasks, moreover maintenance can be nightmare if I keep changing my domain model and database table fields. I thought, if I implement INotifyPropertyChanged in my Business Object (Person Class) then, the .NET Framework can notify a data context about dirty rows automatically. I tried and that did not work. :( Looks like, I will have to compromise either Excel / Spreadsheet functionality or Decoupling of Presentation Layer + Data Access Layer (LINQ to SQL DataContext class) :(
-
Thanks for your reply. Sounds like a lots of tasks, moreover maintenance can be nightmare if I keep changing my domain model and database table fields. I thought, if I implement INotifyPropertyChanged in my Business Object (Person Class) then, the .NET Framework can notify a data context about dirty rows automatically. I tried and that did not work. :( Looks like, I will have to compromise either Excel / Spreadsheet functionality or Decoupling of Presentation Layer + Data Access Layer (LINQ to SQL DataContext class) :(
Nadia Monalisa wrote:
Sounds like a lots of tasks,
Not actually. You have all the information, you just need to match with proper objects. And finding diff, a single line LINQ can do it. :)
Best wishes, Navaneeth
-
Nadia Monalisa wrote:
Sounds like a lots of tasks,
Not actually. You have all the information, you just need to match with proper objects. And finding diff, a single line LINQ can do it. :)
Best wishes, Navaneeth
Thank you for showing the light. I will give a try. I just missed the idea about using LINQ to find the diff. now feeling easy :)
-
Hello, I realize that, DatabaseContext class generated by LINQ to SQL / LINQ to ENTITY should not be a part of the Views and so I want to encapsulate the uses of DatabaseContext classes within my Data Access Layer. So, when I need to display a set of records in a GridView in Windows Form, I can simply let my Data Access Layer return the
List<Person>
of the records and display that records in GridView without any problem. Great! Here is my Person classpublic class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}Ok, now, I face the big problem when I need to save the changes back to the database. I may have 1000 records displaying in the Grid view where the GridView is bound to a BindingSource. My user can change only 2-3 records from the GridView and delete some of the records as well. How can I tell the LINQ to SQL / LINQ to Entity (data context) about the changed records so that when I pass the
List<Person>
object to my Data Access Layer, Data Access Layer can identify which records needs to be updated and finally the Data Context updates those records ? Can you please show me some pattern for this problem ? Regards.HOw about a couple of extra properties on your Person Class.
bool IsUpdated;
bool IsDeleted;Your grid needs to be filtered to omit the IsDeleted items (or just highlight them in some way so the user can change their minds) Set the IsUpdated when anything is, erm, updated. Then your DAL just updates where IsUpated, and deletes where IsDeleted.
___________________________________________ .\\axxx (That's an 'M')
-
HOw about a couple of extra properties on your Person Class.
bool IsUpdated;
bool IsDeleted;Your grid needs to be filtered to omit the IsDeleted items (or just highlight them in some way so the user can change their minds) Set the IsUpdated when anything is, erm, updated. Then your DAL just updates where IsUpated, and deletes where IsDeleted.
___________________________________________ .\\axxx (That's an 'M')
Thank you for your reply. Actually I intended to use a boolean property 'IsDirty' for a dirty row. But, In order to set this 'IsDirty' property, I will need to write code which may not look clean and maintainable. I may need to handle some events of the GridView to set this IsDirty property. It can be more messy if i want to maintain IsDeleted property as well. But, I thought, INotifyPropertyChanged interface was invented for solving this problem. Am I wrong ? When I see some demo videos about Silverlight / WPF, I see the instructor is implementing INotifyPropertyChanged interface on business objects and tracking is being taken care by the framework. Moreover, when working with Silverlight, I am forced to use Business Objects instead of direct database access, as Silverlight is a client side tech. So, the framwork is offering solution for Silverlight ! Am I missing anything about it ?
-
I see ! So, if I want to expose an Excel Spreadsheet like facility to my User, I cannot follow n tier pattern :( thats sad. But anyway, thank you for giving me the idea.
Nadia Monalisa wrote:
expose an Excel Spreadsheet like facility
That is rarely a good idea, it's not very user-friendly, but this may be one of those rare times, just be sure, ask potential users for their opinion.