Passing multiple methods (delegate?)

I am trying to develop a framework for several applications that we are developing here, and one of the infrastructure classes that I am trying to create is creating a database. Ideally, I would have a method in which I could pass the following two methods to it: CreateDatabaseTables () and ResetDatabaseValues ​​();

For example, I can have three applications, which I will call Application1, Application2, and Application3; each of these applications will have a different database schema that I would include in the code (for example, in CreateDatabaseTables there are a bunch of Create Table commands). I want to create a single database method that can be used by each of them, so that it looks something like this:

Application1

BuildLocalDatabase(CreateTablesForApp1(),ResetDatabaseValuesforApp1()) 

Application2

 BuildLocalDatabase(CreateTablesForApp2(),ResetDatabaseValuesforApp2()) 

Application3

  BuildLocalDatabase(CreateTablesForApp3(),ResetDatabaseValuesforApp3()) 

The BuildLocalDatabase method will 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 could do this. There is actually a bunch of validation and other things that I would like 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 you can to do it. I think in C ++ I could just pass the CreateTablesForApp and ResetDatabaseValuesForApp methods as function points, but there seems to be no way to do this in C #. And delegates seem to be doing this well, since I'm really limited to just one method (and multicast seems to want to run the methods twice).

+6
c #
source share
3 answers

Do you want to use delegates:

 public bool BuildLocalDatabase(Func<Database, bool> createTables, Action<Database> resetValues) { // Do initialization if (createTables(db)) { resetValues(db); } } 

You would call it this way:

 BuildLocalDatabase( (db) => CreateTablesForApp1(), (db) => ResetDatabaseValuesforApp1() ); 

(I set the "Database" parameter if necessary - if you do not, you can simply use Func<bool> and Action without this parameter and simply pass the method name directly instead of lambdas. Usually, such methods need some kind of parameter, for example, in connection with the database, etc., so I insert it.)

+4
source share

Well, basically you can. If the question arises of delegating syntax, you need to lose a few () and define a delegate:

 public delegate void MyDelegate(); publid bool BuildLocalDatabase(MyDelegate CreateTablesForApp, MyDelegate ResetDatabaseValuesforApp) { CreateTablesForApp(); ... ResetDatabaseValuesforApp(); } 

and name it like this:

 BuildLocalDatabase(CreateTablesForApp1,ResetDatabaseValuesforApp1); 
+1
source share

You should try using the built-in delegates:

Use the Action delegate if the delegate does not have a return value; otherwise, use Func delegates, each of which is overloaded with up to 4 input parameters.

+1
source share

All Articles