|
-
July 29th, 2010, 12:47 PM
#1
Passing Methods as a Parameter; are delegates the right approach here
I'm trying to develop a framework for several applications we are developing here and one of the framework classes I am trying to build is for creating a database. Ideally, I would have a method where I could pass it the following two methods: CreateDatabaseTables() and ResetDatabaseValues();
For instance, I might have three applications which I'll call Application1, Application2 and Application3; each one these applications would have a different database schema which I would incorporate into code (e.g. the CreateDatabaseTables has a bunch of "Create Table" commands). I want to create a single database method that can be utilized by each of these so it would look something like:
Application1
BuildLocalDatabase(CreateTablesForApp1(),ResetDatabaseValuesforApp1())
Application2
BuildLocalDatabase(CreateTablesForApp2(),ResetDatabaseValuesforApp2())
Application3
BuildLocalDatabase(CreateTablesForApp3(),ResetDatabaseValuesforApp3())
The BuildLocalDatabase method would do something like:
publid bool BuildLocalDatabase(CreateTablesForApp(),ResetDatabaseValuesforApp())
{
- see if database file exists; if it does, delete it
- create a new database file
- call CreateTablesForApp
- if the tables were created successfully, call ResetDatabaseValuesForApp
}
Any thoughts on how I would go able doing this. There's actually a bunch of validation and other stuff that I would want to do in the BuildLocalDatabase function and obviously my goal here is to minimize the amount of duplication code in each application...any suggestions on how one might go about doing this. I think in C++, I could have just passed the CreateTablesForApp and ResetDatabaseValuesForApp methods as function points, but it doesn't seem like there is a way to do this in C#. And delegates does seem to handle it well since I'm really only limited to one method (and the multicast seems to want to run the methods twice).
Thanks for your help,
•Ed
-
July 29th, 2010, 01:10 PM
#2
Re: Passing Methods as a Parameter; are delegates the right approach here
 Originally Posted by cgerard
I think in C++, I could have just passed the CreateTablesForApp and ResetDatabaseValuesForApp methods as function points, but it doesn't seem like there is a way to do this in C#.
Passing in function pointers would be more of a C-styled approach. C++ (and C#) would probably take a more polymorphic approach.
-
July 29th, 2010, 01:33 PM
#3
Re: Passing Methods as a Parameter; are delegates the right approach here
I would simple use an interface where you would be able to have a separate class for each database you are using and the interface could have all needed methods like create, restore, search...all that you want.
Each database class wears the same interface and would have exactly that methods you will need to work with your database.
That way you can create different database classes and all of them could be handled with the same program the program calles the methods of the interface and that methods are defined in the class itself.
Code:
public class AnyDatabase: IDatabase{
public bool LoadDatabase(string name){
// All needed steps
return true;
}
}
public class AnotherDatabase: IDatabase{
public bool LoadDatabase(string name) {
// All needed steps
return true;
}
}
public interface IDatabase {
bool LoadDatabase(string name);
}
// And in the program
IDatabase myDatabase = newAnyDatabase();
bool success = myDatabase.LoadDatabase("Customers");
This would be my approach.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
July 29th, 2010, 01:34 PM
#4
Re: Passing Methods as a Parameter; are delegates the right approach here
I just wrote a blog post that I think might help you understand delegates in c#.
http://alexdresko.com/2010/07/27/usi...uplicate-code/
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|