views:

262

answers:

5

This is really a VB.NET/C# question. Ok, so I essentially have a database layer that is totally isolated from any business logic. This means that whenever I get ready to commit some business data to a database, I have to pass all of the business properties into the data method's parameter. For example:

Public Function Commit(foo as object) as Boolean

This works fine, but when I get into commits and updates that take dozens of parameters, it can be a lot of typing. Not to mention that two of my methods--update and create--take the same parameters since they essentially do the same thing. What I'm wondering is, what would be an optimal solution for passing these parameters so that I don't have to change the parameters in both methods every time something changes as well as reduce my typing :) I've thought of a few possible solutions. One would be to move all the sql parameters to the class level of the data class and then store them in some sort of array that I set in the business layer. Any help would be useful! Thanks!

A: 

So essentially you want to pass in a List of Parameters?

Why not redo your Commit function and have it accept a List of Parameter objects?

Joe Philllips
I'm thinking this is the way to go, but I'm gonna wait and see if anyone comes up with something else.
Austin
A: 

If your on SQL 2008 you can use merge to replace insert / update juggling. Sometimes called upsert.

jms
Thanks. That's a really cool feature. Unfortunately I may have to use 2005 or even 2000.
Austin
A: 

You could create a struct to hold the parameter values.

Jeremy
A: 

Thanks for the responses, but I think I've figured out a better way for what I'm doing. It's similar to using the upsert, but what I do is have one method called Commit that looks for the given primary key. If the record is found in the database, then I execute an update command. If not, I do an insert command. Since the parameters are the same, you don't have to worry about changing them any.

Austin
A: 

For your problem I guess Iterator design pattern is the best solution. Pass in an Interface implementation say ICommitableValues you can pass in a key pair enumeration value like this. Keys are the column names and values are the column commitable values. A property is even dedicated as to return the table name in which to insert these value and or store procedures etc.

To save typing you can use declarative programming syntax (Attributes) to declare the commitable properties and a main class in middleware can use reflection to extract the values of these commitable properties and prepare a ICommitableEnumeration implementation from it.

S M Kamran