tags:

views:

272

answers:

6

I have a windows form app written in C#. the main_form class instantiates an AccessProcess named AccessProcessWorker which is a class that inherits from BackgroundWorker, then main_form then initializes Process with the following code

        AccessProcessWorker.DoWork += new DoWorkEventHandler(processWorker.worker_DoWork);
        AccessProcessWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(processWorkerCompleted);
        AccessProcessWorker.ProgressChanged += new ProgressChangedEventHandler(processProgressChanged);

this application has just gone from POC to "make it work fast".

I wrote this app to work against an Access database but now want to make it go against MS Sql, but leave the option to work against access also. So, I could do something ugly like instantiate and initialize a SqlProcessWorker or AccessProcessWorker based on some UI selection made by the user. But what I'd like to do is make it so main_form always creates something like an IProcess so I didn't have to add logic to main_form every time there is a new ProcessWorker. The problem in my design is that the initializing breaks when I do it the way I described.

If anyone has any ideas, or need further clairification please let me know.

+2  A: 

At some point you will need to instantiate the correct type, but The Factory Pattern is usually to the goto here. Now, that may be a bit much if you will only ever have one of two types to 'new' in order to get your IProcess object.

Ed Swangren
+4  A: 

What you look for is called "dependency injection".

ammoQ
I think that may be overkill for the OP at this point though.
Ed Swangren
+1. I don't think it is overkill at all, certainly my experience with StructureMap is that using it for even simple cases like this is worthwhile (and easy to do).
Richard
+2  A: 

In the interests of keeping it simple, I would actually just go with the "ugly" approach.

You've mentioned Access and SQL Server as the two current databases, but how many do you realistically believe your app is needing to support? In my experience an application's database platform is very rarely changed and not without serious thought.

If there were a large set of database platforms to support and you can't predict which in advance, then maybe a decoupled design would be useful. Otherwise KISS.

Ash
this is what I'm starting to think...sad though, I'd like to be a better developer and have more tools to solve this common problems.
Is this for a work situation? If so then unless the customer/client has actually asked for the ability to add new databases in the future, the ugly approach is actually the responsible way to go. You would just say, sure we can get it done ASAP, however if you want new databases in the future, it will require re-work and extra effort. If it's not for work, then sure, treat it as a learning experience. Some of the other answers provide some good tips. By the way, you sound like a decent developer to me.
Ash
:) ok...yes it is for work...my biggest problem is I'm the only dev, I'm used to having a team, and my boss just tosses requirements around like they were nothing, I'm trying to think of everything so when he sees the price of a license for SQL and says can we use MySQL I'm ready for it. On a side note I went to work for a foreign company, it's hard enough to gather requirements when English is the first language. I think Jim's Big European Adventure will be rapidly coming to an end :)
Surely the free Sql Server Express Edition will work for you right? Or will you cross the 4gb data file limit?
cottsak
Could be...he had me compare 25k rows against 10k rows, when I I said that it will take about a year to process he balked and asked for SQL
You might want to have a chat to your boss about the "effects" of random requirements on developing software. If he/she is not interested in even a discussion, then you're right it might be time to move on. Re comparing 25k versus 10k rows, I'd be very surprised, if using SQL Server, that it takes more then a minute. If you have plenty of RAM and decent CPU/s, using Joins and appropriate indexes will give very good performance.
Ash
+1  A: 

If both of the database are the same layout and structure, you can just use EntitySpaces and change the default connection of the application. I.e. you have one code base when it comes to data access and then you just set the current connection based on whether you want the Access or SQL Database.

GenericTypeTea
the database changes every time.
+1  A: 

I would wrap the "ugly" bits of code in a separate method, or preferably a class which takes care of choosing which DB to talk to and the synchronization with the actual BackgroundWorker instances. The important point here is to adhere to the DRY principle: Don't Repeat Yourself.

JacobE
A: 

I think that for a work project, you should do it as fast as possible, without thinking of future databases, because it's probably not gonna happen.

Are you sure there's not, somewhere, a class which already works with both Sql Server and MS Access? For example, OleDbConnection, OleDbCommand? For simple SQL, all you need is to change the connection string and you can work with both databases.

If you haven't coded the rest of the application yet, you should take a day or two to look for some frameworks, compare and choose one of your liking. It'll make you write less code, and in future apps you'll be spared most of the database plumbery code. I guarantee that the time invested will be returned to you manyfold (if you work with databases once in a while, that is).

DonkeyMaster