473,473 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to use Generic Class as Service

8 New Member
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.
Aug 10 '10 #1
12 1400
balabaster
797 Recognized Expert Contributor
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.
Aug 10 '10 #2
nomaneagle
8 New Member
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.
Aug 10 '10 #3
balabaster
797 Recognized Expert Contributor
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.
Aug 10 '10 #4
nomaneagle
8 New Member
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.
Aug 10 '10 #5
Sfreak
64 New Member
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
Aug 10 '10 #6
balabaster
797 Recognized Expert Contributor
No problem, any time
Aug 10 '10 #7
nomaneagle
8 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. Public Function GetList() As DataSet
  2.  
  3.         Try
  4.             Dim par(10) As SqlClient.SqlParameter
  5.             par(0) = New SqlClient.SqlParameter("@act", 1)
  6.  
  7.             Return SqlHelper.ExecuteDataset(ConString, CommandType.StoredProcedure, "SP_Departments", par)
  8.  
  9. 'Gets all the records from Department table and return it as dataset
  10.  
  11.         Catch ex As Exception
  12.             Throw
  13.         End Try
  14.  
  15.     End Function
Business Logic Layer:

Expand|Select|Wrap|Line Numbers
  1.  Public Function GetList() As List(Of Departments)
  2.         Try
  3.             Dim cls As New DataAccess.Departments
  4.             Dim ds As DataSet = cls.GetList()
  5.             Dim row As DataRow
  6.             Dim list As New List(Of Departments)
  7.  
  8.             For Each row In ds.Tables(0).Rows
  9.  
  10.                 Dim kk As New Departments
  11.                 kk.ID = row.Item("id")
  12.                 kk.Department = row.Item("department")
  13.                 kk.User = row.Item("updatedby")
  14.  
  15.                 list.Add(kk)
  16.  
  17.             Next
  18.  
  19.             Return list
  20.  
  21.         Catch ex As Exception
  22.             Throw ex
  23.         End Try
  24.  
  25.     End Function
Presentation Layer :

Expand|Select|Wrap|Line Numbers
  1. Private JTList As List(Of BusinessLogic.Departments)
  2. JTList = cls.GetList()
  3. dg.DataSource = New BusinessLogic.SortableBindingList(Of BusinessLogic.Departments)(JTList)
Aug 10 '10 #8
Sfreak
64 New Member
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
Aug 10 '10 #9
nomaneagle
8 New Member
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?
Aug 10 '10 #10
Sfreak
64 New Member
If you want your application running as a service yes... it is a good idea. Do you use any ORM framework? (NHibernate, Entity etc)
Aug 10 '10 #11
nomaneagle
8 New Member
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.
Aug 10 '10 #12
Sfreak
64 New Member
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
Aug 10 '10 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: RYoung | last post by:
Hello all, Given this C# generic class definition: class Command<Key, Data> { BerkeleyParameter<Key> key; BerkeleyParameter<Data> data; public void AddParameters(BerkeleyParameter<Key>...
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
2
by: RFOG | last post by:
Hi all! I need a class that will be capable to enumerate some internal data, then my idea is to implement "for each" loop access. Doc says that I've to inherit form IEnumerable, but then...
2
by: Rune Vistnes | last post by:
Hey, I am trying to wrap an unmanaged library in managed c++ so that I can use this library in other .NET languages, such as C#. I've been successful for the most part this far, but I'm having a...
2
by: AdawayNoSpam | last post by:
Said that I have the following class Class MyRootClass(Of T) End Class Class MySubClass1(Of T) Inherits MyRootClass(Of T) End Class
4
by: Hyun-jik Bae | last post by:
Is that not allowed to assume generic type numeric type? As far as I've tried, I got an error with the following code: public class AAA<T> { public int Foo(T a) { return a; // error: Cannot...
7
by: Andrus | last post by:
public class BusinessObjectGeneric<EntityType: BusinessObject where EntityType : BusinessEntity, new() { public BusinessObjectGeneric<EntityType() { } ..... causes error in constructor...
10
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
1
by: raylopez99 | last post by:
Here is an example of a home grown generic class, representing a pair of values. Adapted from Jon Skeet's book "C# In Depth". The generic class is "sealed" for some reason (I think for...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.