简体   繁体   English

启用不同的方法,但使用相同的名称-C#

[英]Switch on different methods but using the same name - C#

I realise that the title may be very confusing, so to explain what I would like to do, I will illustrate how I can easily solve the same problem in PHP. 我意识到标题可能会非常混乱,因此在解释我想做的事情时,我将说明如何轻松解决PHP中的相同问题。

Eg I give the user of my application a choice database types (ie MySQL, MSSQL, etc). 例如,我给我的应用程序用户一个选择的数据库类型(即MySQL,MSSQL等)。

In my code I would like to have only one function for each operation rather than a function for each database as I explain below. 在我的代码中,我希望每个操作只有一个功能,而不是每个数据库都有一个功能,如下所述。

NOT

main.php main.php

<?php
function mysql_get_users() {
  //Foo
}

function mssql_get_users() {
  //Foo
}

if($dbtype == "mysql") {
  echo mysql_get_users();
}

if($dbtype == "mssql") {
  echo mssql_get_users();
}

?>

BUT

main.php main.php

<?php
if($dbtype == "mysql") {
  include("mysql.php");
}

if($dbtype == "mssql") {
  include("mssql.php");
}
echo get_users();
?>

mysql.php mysql.php

<?php
function get_users() {
  //Foo
}
?>

mssql.php mssql.php

<?php
function get_users() {
  //Foo
}
?>

Now I have the same problem, but in C# using Visual Studio 2010. Since I cannot do this: 现在,我遇到了同样的问题,但是在C#中使用Visual Studio2010。由于无法执行此操作:

 if(dbtype=="mysql") {
    public static MySqlConnection connect(String connectionString)
    {
        MySqlConnection connection = new MySqlConnection(connectionString);
        connection.Open();
        return connection;
    }
}

The big problem with the above is particularly that I cannot put an if-clause around a method(function), then I have to face the problems of the types which are unique to each database type amongst the fact that I have to include the returned connection variable in each method related to database function. 上面的大问题特别是我不能在方法(函数)周围加上if-子句,然后我不得不面对每种数据库类型所独有的类型的问题,其中我必须包括返回的事实。与数据库功能有关的每种方法中的连接变量。

So, any thoughts on how to fix this and make it work like the PHP example above? 那么,关于如何解决此问题并使它像上面的PHP示例一样起作用的任何想法? I am neither an expert nor a newbie on C#, but I learn quite quickly. 我既不是C#的专家,也不是新手,但我很快就学到了。

You will want to research "interfaces" or "delegates". 您将要研究“接口”或“代理”。

Interfaces are more common across various languages (including C# and Java), and allow you to provide different implementations (through implementing classes) that all share the same interfaces, and can be used interchangeably. 接口在各种语言(包括C#和Java)中更为常见,并且允许您提供(通过实现类)不同的实现,这些实现都共享相同的接口,并且可以互换使用。

Delegates are also a rather cool option, but are limited to one method - which may actually be what you're looking for here. 代表也是一个很酷的选择,但仅限于一种方法-实际上,这可能就是您在这里要寻找的。 (Compared to an interface, which defines one or more methods that exist as part of the interface). (与接口相比,它定义了作为接口一部分存在的一个或多个方法)。

Some additional references: 一些其他参考:

Personally, I think you need to approach the problem differently. 就个人而言,我认为您需要以不同的方式来解决这个问题。 This is where object-oriented programming is your friend. 在这里,面向对象的编程就是您的朋友。 Basically, you would abstract out the database-specific stuff so the calling code knows nothing about which database it is using. 基本上,您将抽象出特定于数据库的内容,因此调用代码对使用的数据库一无所知。 If you were doing it from scratch, here is a very over-simplified example: 如果您是从头开始的,这是一个过于简化的示例:

public interface IDatabase {
   void Connect(string ...);
   string Query(string ...);
}

public class MySQLDB : IDatabase {
   void Connect(string ...){
       MySqlConnection connection = new MySqlConnection(connectionString);
       ...
   }

    // same for query, etc
}

public class OracleDB : IDatabase {
   void Connect(string ...){
       // connect using Oracle library
       ...
   }

    // same for query, etc
}

So then, in your client code, you choose which type of database to instantiate, and then call the same functions regardless of what kind it is: 因此,然后在客户端代码中,选择要实例化的数据库类型,然后调用相同的函数,而不管它是哪种类型:

IDatabase db;
if (dbtype == "mysql")
   db = new MySqlDB();
else if (dbtype == "oracle")
   db = new OracleDB();

db.Connect(...);
...
public static IDbConnection connect(String connectionString)
{
    if ( dbtype == "mysql" )
    {
        MySqlConnection connection = new MySqlConnection(connectionString);
        connection.Open();
        return connection;
    }
    else if ( dbtype == "mssql" )
    {
        SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();
        return connection;
    }
}

Maybe you can choose Factory Method or Strategy to do this. 也许您可以选择“工厂方法”或“策略”来执行此操作。 Basically you can create a interface for the work: 基本上,您可以为工作创建一个接口:

public interface ConnectionFactory {
    IDBConnection CreateConnection(string connectionString);
}

Then give them different implementation based on the type . 然后根据type为它们提供不同的实现。

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

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