Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple methods (delegate?)

Tags:

c#

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).

like image 293
user405965 Avatar asked Jul 29 '10 17:07

user405965


People also ask

Can a delegate call multiple methods?

Introduction. A delegate is a reference type variable whose objects can refer to multiple methods which can then be invoked later. Instead of calling methods directly, delegates can be used to call these methods.

How do you pass parameters to delegates?

In C#, we can also pass a method as a parameter to a different method using a delegate. We use the delegate keyword to define a delegate. Here, Name is the name of the delegate and it is taking parameter.

Why use delegates over methods?

Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods. Delegates can be chained together; for example, multiple methods can be called on a single event. Methods don't have to match the delegate type exactly.


2 Answers

You'll want to use delegates:

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

You'd then call this as:

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

(I put in a "Database" parameter in case you need it - if you don't, you can just use Func<bool> and Action, without that parameter, and just pass the method name directly instead of lambdas. Usually methods like this need some form of parameter, such as a DB connection, etc, though- so I put it in.)

like image 180
Reed Copsey Avatar answered Oct 20 '22 23:10

Reed Copsey


Well, basically you can. If the question is about delegate syntac, you need to lose a few () and define a delegate:

public delegate void MyDelegate();

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

and call it like:

BuildLocalDatabase(CreateTablesForApp1,ResetDatabaseValuesforApp1);
like image 27
Henk Holterman Avatar answered Oct 20 '22 21:10

Henk Holterman