简体   繁体   English

WF 4.5:是否可以在运行时动态添加变量?

[英]WF 4.5: Is it possible to add variables dynamically at runtime?

We are writing a custom activity. 我们正在编写一个自定义活动。 In this activity it is possible to set a database connection string and a name for a stored procedure. 在此活动中,可以设置数据库连接字符串和存储过程的名称。 At runtime the stored procedure is executing. 在运行时,存储过程正在执行。 Now we have some stored procedures which has input parameters. 现在,我们有了一些具有输入参数的存储过程。

Is it possible to generate variables dynamically in WF 4.5 for each input parameter in the stored procedure? 是否可以在WF 4.5中为存储过程中的每个输入参数动态生成变量? Reading the parameters from the stored procedure is not the problem, but I dont have any idea how to generate the variables. 从存储过程中读取参数不是问题,但是我不知道如何生成变量。

Example: The user enters a name for the stored procedure to be executed (2 input params @Variable1 and @Variable2). 示例:用户输入要执行的存储过程的名称(2个输入参数@ Variable1和@ Variable2)。 Now in the variables tab should be 2 variables: @Variable1 and @Variable2. 现在在变量选项卡中应该有2个变量:@ Variable1和@ Variable2。 If the user changes the name in the stored procedure then in the variables tab should be the new params (for example only @Variable2)... 如果用户在存储过程中更改名称,则在变量选项卡中应该是新的参数(例如,仅@ Variable2)...

We spent a lot of time on this issue. 我们在这个问题上花了很多时间。 But the only thing we have learned is that the activity has to be a NativeActivity and the variables should be added in the CacheMetadata method. 但是我们唯一了解到的是,该活动必须是NativeActivity,并且应该在CacheMetadata方法中添加变量。 But if I add a variable with AddVariable() method nothing happens :( 但是,如果我使用AddVariable()方法添加变量,则不会发生:(

If you are open to including third-party libraries, you could try using an ORM tool like Dapper to accomplish this. 如果您愿意包括第三方库,则可以尝试使用Dapper之类的ORM工具来完成此操作。 A Dapper query generally takes an anonymous type to supply its parameters. Dapper查询通常采用匿名类型来提供其参数。 Typical code for creating a custom object from fields in a database would look something like this: 用于从数据库中的字段创建自定义对象的典型代码如下所示:

IDbConnection db = New IDbConnection(...);
int id = 527; // normally passed in - using a hard coded value would defeat the purpose...
string myQuery = "SELECT Engine, Transmission, Make, Model, BodyStyle FROM Table WHERE ID = @ID";
Car result = db.Query<Car>(myQuery, new { ID = id }).First();

So I believe that you could use reflection to pass in the type ("Car") using reflection and create an anonymous object or pass in an actual object with an "ID" property at runtime. 因此,我相信您可以在运行时使用反射来传递类型(“ Car”),并创建一个匿名对象或使用“ ID”属性传递实际对象。 It will automatically create a custom Car object with the resulting data, assuming that the Car object has properties of Engine, Transmission, Make, Model, BodyStyle, etc. 假设Car对象具有Engine,Transmission,Make,Model,BodyStyle等属性,它将自动使用结果数据创建自定义Car对象。

Note that if you don't supply the type you expect to get back, you get an ExpandoObject: Creating an anonymous type dynamically? 请注意,如果不提供期望返回的类型,则会得到ExpandoObject: 动态创建匿名类型吗? - you may also be able to pass one of these in for your input parameters, which would mean that you could create it at runtime. -您也可以为输入参数传递其中之一,这意味着您可以在运行时创建它。

This guy came up with a generic method for Dapper that may help you: http://www.bradoncode.com/blog/2012/12/creating-data-repository-using-dapper.html 这个家伙想出了一种适用于Dapper的通用方法,该方法可以为您提供帮助: http : //www.bradoncode.com/blog/2012/12/creating-data-repository-using-dapper.html

What he came up with is something like this: 他想出的是这样的:

IEnumerable<T> items = null;

// extract the dynamic sql query and parameters from predicate
QueryResult result = DynamicQuery.GetDynamicQuery(_tableName, predicate);

using (IDbConnection cn = Connection)
{
    cn.Open();
    items = cn.Query<T>(result.Sql, (object)result.Param);
}

return items;

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

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