Designing a Database Application with OOP
Posted
by
Tim C
on Programmers
See other posts from Programmers
or by Tim C
Published on 2012-10-17T16:03:24Z
Indexed on
2012/10/17
17:19 UTC
Read the original article
Hit count: 414
I often develop SQL database applications using Linq, and my methodology is to build model classes to represent each table, and each table that needs inserting or updating gets a Save() method (which either does an InsertOnSubmit() or SubmitChanges(), depending on the state of the object). Often, when I need to represent a collection of records, I'll create a class that inherits from a List-like object of the atomic class.
ex.
public class CustomerCollection : CoreCollection<Customer>
{
}
Recently, I was working on an application where end-users were experiencing slowness, where each of the objects needed to be saved to the database if they met a certain criteria. My Save() method was slow, presumably because I was making all kinds of round-trips to the server, and calling DataContext.SubmitChanges() after each atomic save.
So, the code might have looked something like this
foreach(Customer c in customerCollection)
{
if(c.ShouldSave())
{
c.Save();
}
}
I worked through multiple strategies to optimize, but ultimately settled on passing a big string of data to a SQL stored procedure, where the string has all the data that represents the records I was working with - it might look something like this:
CustomerID:34567;CurrentAddress:23 3rd St;CustomerID:23456;CurrentAddress:123 4th St
So, SQL server parses the string, performs the logic to determine appropriateness of save, and then Inserts, Updates, or Ignores.
With C#/Linq doing this work, it saved 5-10 records / s. When SQL does it, I get >100 records / s, so there is no denying the Stored Proc is more efficient; however, I hate the solution because it doesn't seem nearly as clean or safe.
My real concern is that I don't have any better solutions that hold a candle to the performance of the stored proc solution. Am I doing something obviously wrong in how I'm thinking about designing database applications? Are there better ways of designing database applications?
© Programmers or respective owner