C# 不带参数的泛型委托 - 定义和调用

发布于 2024-11-26 06:51:20 字数 1409 浏览 1 评论 0原文

我目前正在重构一个现有的 DAL,它有一个用户调用的外观和一个内部类,该内部类依赖于 ADO.Net 提供程序来使用 SqlProvider 来完成实际工作,并且我试图确保代码是 DRY,我'通过使用 Func 已经完成了,所以我可以执行以下操作:

return RunCommand(c => c.ExecuteNonQuery(commandText, parameters));

RunCommand 方法如下所示:

    private T RunCommand<T>(Func<Commands, T> toRun)
    {
        return toRun(CreateCommand());
    }

CreateCommand() 方法只是构建要使用的命令对象,这样我就可以拥有一个处理命令的方法所有的电话只需返回预期的类型,例如 DataSet、DataReader 等

我遇到的问题是,门面上的几个调用提供了一个 out 参数,我知道如果我可以使用委托,那么应该能够删除重复的代码,但之后很多谷歌搜索和尝试我还没有弄清楚如何做。代码是:

 Commands commands = CreateCommand();
 return commands.ExecuteNonQuery(out cmd, commandText, parameters);

我真正想做的是能够调用:

return RunCommand(c => c.ExecuteNonQuery(out cmd, commandText, parameters));

我已经看到 这个存在的问题,但对于我的一生,我无法弄清楚如何将其变成我所需要的。

这个委托似乎是我需要的 private delegate V TestOutParameter(T a, out U b, V c); 但我调用它的代码只是不对:

    private V RunCommand<T, U, V>(TestOutParameter<Commands, DbCommand, V> commandToExecute)
    {
        DbCommand cmd;
        return (V)commandToExecute(CreateCommand(), out cmd);
    }

任何人都可以帮助我吗,因为这已经让我发疯了一个星期了!

I'm currently refactoring an existing DAL which has a facade the user calls and an inner class that does the actual work dependent upon the ADO.Net provider to use e.g. SqlProvider, and I'm trying to ensure that code is DRY, I've done ok by using a Func so I can do:

return RunCommand(c => c.ExecuteNonQuery(commandText, parameters));

And the RunCommand method looks like:

    private T RunCommand<T>(Func<Commands, T> toRun)
    {
        return toRun(CreateCommand());
    }

The CreateCommand() method simply builds the command object to use, this then allows me to have a single method that handles all the calls that just return the expected type e.g. DataSet, DataReader, etc

The problem I have is that several calls on the facade provide an out parameter I know should be able remove the repeated code if I can use a delegate but after a lot of googling & experimenting I've not managed to work out how. The code is:

 Commands commands = CreateCommand();
 return commands.ExecuteNonQuery(out cmd, commandText, parameters);

What I'd really like to do is be able to call:

return RunCommand(c => c.ExecuteNonQuery(out cmd, commandText, parameters));

I've seen this existing question but for the life of me I cannot work out how to turn that into what I need.

This delegate would seem to be what I need private delegate V TestOutParameter<T, U, V>(T a, out U b, V c); but the code I've got for calling it just isn't right:

    private V RunCommand<T, U, V>(TestOutParameter<Commands, DbCommand, V> commandToExecute)
    {
        DbCommand cmd;
        return (V)commandToExecute(CreateCommand(), out cmd);
    }

Can anybody help me as this has been driving me mad for a week!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笑百媚生 2024-12-03 06:51:21

您的委托有三个参数而不是两个。假设您希望它镜像 Func,而不是:

private delegate V TestOutParameter<T, U, V>(T a, out U b, V c);

您应该:

private delegate V TestOutParameter<T, U, V>(T a, out U b);

我个人建议您将其重命名为:

private delegate TResult FuncOut<T1, T2, TResult>(T1 arg1, out T2 arg2)

Your delegate has three parameters instead of two. Assuming you want it to mirror Func, instead of:

private delegate V TestOutParameter<T, U, V>(T a, out U b, V c);

you should have:

private delegate V TestOutParameter<T, U, V>(T a, out U b);

I would personally recommend you rename it to something like:

private delegate TResult FuncOut<T1, T2, TResult>(T1 arg1, out T2 arg2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文