简体   繁体   English

传递多种方法(委托?)

[英]Passing multiple methods (delegate?)

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(); 理想情况下,我会有一个方法,我可以传递以下两个方法:CreateDatabaseTables()和ResetDatabaseValues();

For instance, I might have three applications which I'll call Application1, Application2 and Application3; 例如,我可能有三个应用程序,我将其称为Application1,Application2和Application3; each one these applications would have a different database schema which I would incorporate into code (eg the CreateDatabaseTables has a bunch of "Create Table" commands). 每个应用程序都有一个不同的数据库模式,我将其合并到代码中(例如,CreateDatabaseTables有一堆“创建表”命令)。 I want to create a single database method that can be utilized by each of these so it would look something like: 我想创建一个可以被这些方法使用的单个数据库方法,所以它看起来像:

Application1 应用1

BuildLocalDatabase(CreateTablesForApp1(),ResetDatabaseValuesforApp1())

Application2 应用2

BuildLocalDatabase(CreateTablesForApp2(),ResetDatabaseValuesforApp2())

Application3 Application3

 BuildLocalDatabase(CreateTablesForApp3(),ResetDatabaseValuesforApp3())

The BuildLocalDatabase method would do something like: BuildLocalDatabase方法将执行以下操作:

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. 实际上我想在BuildLocalDatabase函数中做一些验证和其他事情,显然我的目标是最小化每个应用程序中的重复代码量...任何关于如何执行此操作的建议。 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#. 我认为在C ++中,我可以将CreateTablesForApp和ResetDatabaseValuesForApp方法作为函数点传递,但似乎没有办法在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). 代表似乎确实处理得很好,因为我实际上只限于一种方法(多播似乎想要运行两次方法)。

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.) (如果你不需要,我输入一个“Database”参数 - 如果你不需要,你可以只使用Func<bool>Action ,不带那个参数,只需直接传递方法名而不是lambdas。通常类似的方法这需要某种形式的参数,例如数据库连接等,所以我把它放入。)

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

You should try to use the built-in delegates: 您应该尝试使用内置代理:

Use the Action delegate if the delegate doesn't have a return value, otherwise use the Func delegates, each are overloaded with up to 4 input parameters. 如果委托没有返回值,则使用Action委托,否则使用Func委托,每个委托重载最多4个输入参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM