How to use Generic Class as Service

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomaneagle
    New Member
    • Nov 2008
    • 8

    How to use Generic Class as Service

    Hi,

    My question could be silly but this is what I am trying to do here with my Generic class.

    I have a Data Access Layer, Business Logic Layer and a presentation Layer(Windows forms). Everytime I have to retrieve a List of records, I have to call a method (GetList()) in Bussiness Logic class which calls SQL query in Data access class. I create a new instance of business Logic class everytime a user LogIn. So in this way each of the client computers have their own instance of Business Logic class.

    What Im trying to achieve here is to have a single instance of Business Logic Class running on the server all the time which gets updated with latest records in every 60 seconds. So whenever my presentation layer request data from business class, Bussiness Logic class just return the latest records to presentation layer without calling Data Access layer. Is there any way that I can do it?

    Thanks in advance.
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    What you're talking about is caching. This can be done a number of ways, the simplest of which is to have the caching built directly in to the DAL.

    In the simplest cases, I would probably use some architecture similar to this:

    BLL would call the Data Layer, which is no longer actually the data access layer, but a caching mechanism that holds the current view of the data in memory - though, this could potentially be a large memory overhead depending on the size of the application/data store.

    The data layer in turn has a timer that will pull updates from the database on a configurable schedule - as you suggest 60 seconds, or whatever is appropriate.

    If I understood you rightly, your BLL sits as part of the application on the client machine. This will be configured to access your data cache instead of your DAL.

    In more complex cases, I may get a bit more complicated and have my cache auto-update as data is queried only if the table has been updated. A trigger on my table could record the last time data was updated in the table, I'd keep a small table of timestamps for last updates on my tables, every time an update occurs, the timestamp for that table would be updated. When I query for data, my DAL checks the timestamp of the cached data against the timestamp for the table, if it's the same, it returns the cached data, if it's different it updates the cache and returns the data to me. This way the first user to query "new data" suffers the penalty, but everyone else gets up-to-date data without it potentially being 60 seconds out of date.

    You could combine these fairly easily - have a small call from your cache to the DAL to find out if anything's updated, if it has been updated, then get the new data. You'd need to configure it such that if you didn't get an almost immediate response you don't hang around waiting, but instead return the cached data.

    The trick here is to understand what forces are at play and what will cause performance issues. The 2 biggest causes of client server app performance issues are disk i/o on the server and network latency.

    You can alleviate the disk i/o by querying the least amount of data from the database unless it's necessary, but you obviously want up-to-date data. The easiest trick for that is to create a method of knowing if the data has been modified since you last checked, without having to check all the data itself.

    You can mask network latency by caching data on the client side, but this will increase either memory requirements or if you're dumping the data to disk will incur additional i/o overhead which may hinder the performance of your application. So you need to make sure that these aren't going to have a greater detriment to your application than the network latency would.

    Just food for thought...

    P.S. Using generics often entails the need for reflection. There are great performance penalties when using reflection if you don't use it properly. Of course, if you're not using reflection, you don't need to worry about this.
    Last edited by balabaster; Aug 10 '10, 03:47 PM.

    Comment

    • nomaneagle
      New Member
      • Nov 2008
      • 8

      #3
      I like your idea of updating generic class with latest records only if new changes are available in the table.

      I do have datetime stamp for each row in the table. So in this way before I retrieve thousands of records using DAL and update them into generic class BLL with loop, I will check the existance of new records first in the table. But even in this case, if I find one new row in the table I will have to update all thousands of records again.

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        I'd update a mini-table with the last-updated timestamp and each time a table is updated, update the timestamp. This way you've got an ultra-lightweight table that will allow you to determine if a table's been updated without having to query all the rows in the table checking for new timestamps to figure that out.

        If your table has a couple of million rows in it, it could get expensive to figure out if it's got any new rows since any given date if you've gotta query every row to figure that out.

        Better to query a table with just a few rows to find out if any given table's been updated since the last refresh, then just query the rows from those updated tables that have been added or updated from each updated table.

        So if you've only got one updated table, you don't even need to query the other tables at all, just the updated one. This saves querying a whole bunch of tables that haven't had any edits since our cache was last refreshed.
        Last edited by balabaster; Aug 10 '10, 05:37 PM.

        Comment

        • nomaneagle
          New Member
          • Nov 2008
          • 8

          #5
          Fantastic. I can use triggers to update the stamp table with new stamp at the time of new, edit or delete operations. Thanks very much for your help.

          Comment

          • Sfreak
            New Member
            • Mar 2010
            • 64

            #6
            I dont know if you are using an application service (which is a good idea in your case, once you have your application distributed in layers) like WCF or Webservices as your application service. If you are using it, I think that you can do it easily using SESSION. You can save a state of an object in a session and retrieve it whenever you want, without calling your database, just working with the server´s memory. Its an idea... i dont know if it applies to your necessity.

            I hope it can help you,

            Fernando Mello

            Comment

            • balabaster
              Recognized Expert Contributor
              • Mar 2007
              • 798

              #7
              No problem, any time

              Comment

              • nomaneagle
                New Member
                • Nov 2008
                • 8

                #8
                Thanks Fernando Mello,

                Actually this is exactly what I was looking for. But I done know much about Web Services or WCF. Also it is not a web based application. Should I use my DAC/BLL as Contract/Server? I provide you the structure of my classes so that you can have better idea about the structure of my application:

                Data Access Layer:

                Code:
                Public Function GetList() As DataSet
                
                        Try
                            Dim par(10) As SqlClient.SqlParameter
                            par(0) = New SqlClient.SqlParameter("@act", 1)
                
                            Return SqlHelper.ExecuteDataset(ConString, CommandType.StoredProcedure, "SP_Departments", par)
                
                'Gets all the records from Department table and return it as dataset
                
                        Catch ex As Exception
                            Throw
                        End Try
                
                    End Function
                Business Logic Layer:

                Code:
                 Public Function GetList() As List(Of Departments)
                        Try
                            Dim cls As New DataAccess.Departments
                            Dim ds As DataSet = cls.GetList()
                            Dim row As DataRow
                            Dim list As New List(Of Departments)
                
                            For Each row In ds.Tables(0).Rows
                
                                Dim kk As New Departments
                                kk.ID = row.Item("id")
                                kk.Department = row.Item("department")
                                kk.User = row.Item("updatedby")
                
                                list.Add(kk)
                
                            Next
                
                            Return list
                
                        Catch ex As Exception
                            Throw ex
                        End Try
                
                    End Function
                Presentation Layer :

                Code:
                Private JTList As List(Of BusinessLogic.Departments)
                JTList = cls.GetList()
                dg.DataSource = New BusinessLogic.SortableBindingList(Of BusinessLogic.Departments)(JTList)
                Last edited by Curtis Rutland; Aug 10 '10, 08:43 PM. Reason: Please use [CODE][/CODE] tags when posting code.

                Comment

                • Sfreak
                  New Member
                  • Mar 2010
                  • 64

                  #9
                  Yes. Once you developed your application in layers (MVC) It will be very easy to distribute your application in services (read about SoA Architecture). Webservices are thousand times easier to develop and maintain than WCF and for intermediate and small applications we dont see a real lack of performance. The problem is: Microsoft VS 2010 dont have the WS template anymore (We really dont know why they do that... was a comercial act to promote WCF) so you must create in VS 2008 and then import the application to 2010 (or create your own template)

                  Here we maintain both technologies and I can assure you that webservices works pretty fine when we talk about Services Oriented Applications. I recommend you start with Webservices because is pretty much easier than WCF and you are not losing anything important.

                  If you want a direction to a start feel free to send me an email and we can discuss this later on.

                  Fernando Mello

                  Comment

                  • nomaneagle
                    New Member
                    • Nov 2008
                    • 8

                    #10
                    I just recently deployed the latest version of my application. So Im thinking that I should deployee the next version in the form of Web Services. Do you think its a good idea?

                    Comment

                    • Sfreak
                      New Member
                      • Mar 2010
                      • 64

                      #11
                      If you want your application running as a service yes... it is a good idea. Do you use any ORM framework? (NHibernate, Entity etc)

                      Comment

                      • nomaneagle
                        New Member
                        • Nov 2008
                        • 8

                        #12
                        No I don't use any ORM framework. I will be using BLL and DAL as web services while my windows and web interfaces will be using them for data transmission.

                        Comment

                        • Sfreak
                          New Member
                          • Mar 2010
                          • 64

                          #13
                          Ok. Im asking because if you were using an ORM framework you should be careful about some issues... like Circular Reference... both services dont work properly serializing ORM classes which contains circular reference.

                          So... good luck with your webservice project. Its a nice solution when we talk about SoA. Feel free to contact me if you have further questions.

                          <email removed>
                          Fernando Mello
                          Last edited by Frinavale; Aug 12 '10, 06:11 PM. Reason: Email address removed.

                          Comment

                          Working...