Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I want to detach the LINQ to SQL / LINQ to Entity Data Context from my presentation layer

I want to detach the LINQ to SQL / LINQ to Entity Data Context from my presentation layer

Scheduled Pinned Locked Moved C#
databasequestioncsharpcsslinq
10 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nadia Monalisa
    wrote on last edited by
    #1

    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 class

    public 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.

    P N L 3 Replies Last reply
    0
    • N Nadia Monalisa

      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 class

      public 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.

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      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.

      N 1 Reply Last reply
      0
      • P PIEBALDconsult

        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.

        N Offline
        N Offline
        Nadia Monalisa
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • N Nadia Monalisa

          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 class

          public 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.

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          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 the Person 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

          N 1 Reply Last reply
          0
          • N N a v a n e e t h

            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 the Person 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

            N Offline
            N Offline
            Nadia Monalisa
            wrote on last edited by
            #5

            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) :(

            N 1 Reply Last reply
            0
            • N Nadia Monalisa

              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) :(

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              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

              N 1 Reply Last reply
              0
              • N N a v a n e e t h

                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

                N Offline
                N Offline
                Nadia Monalisa
                wrote on last edited by
                #7

                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 :)

                1 Reply Last reply
                0
                • N Nadia Monalisa

                  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 class

                  public 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.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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')

                  N 1 Reply Last reply
                  0
                  • L Lost User

                    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')

                    N Offline
                    N Offline
                    Nadia Monalisa
                    wrote on last edited by
                    #9

                    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 ?

                    1 Reply Last reply
                    0
                    • N Nadia Monalisa

                      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.

                      P Online
                      P Online
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups