C# 方法返回一个接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/511248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:15:53  来源:igfitidea点击:

Method return an interface

c#.netoopinterface

提问by netseng

Hi All,

大家好,

I'm thinking in this line of code

我在想这行代码

IDataReader myReader = questDatabase.ExecuteReader(getQuest);

I'm using DAAB but I can't understand how and what's the meaning of the fact the method ExecuteReader(DbCommand) returns an IDataReader Interface.

我正在使用 DAAB,但我不明白 ExecuteReader(DbCommand) 方法返回 IDataReader 接口的方式和含义是什么。

Anyone can Explain, Please

任何人都可以解释,请

采纳答案by Pablo Retyk

It allows you to you DataReader without the need of knowing which type of DataReader you are using (i.e. SqlDataReader, OleDbDataReader, EtcDataReader), so if someday you want to change the datareader you are using it won't impact you logic, in other words it gives you abstraction. For example :

它允许您使用 DataReader 而无需知道您正在使用哪种类型的 DataReader(即SqlDataReader, OleDbDataReader, EtcDataReader),因此如果有一天您想更改您正在使用的 DataReader ,它不会影响您的逻辑,换句话说,它为您提供了抽象。例如 :

you can use

您可以使用

IDbCommand command = GiveMeSomeCommand();
IDataReader r = command.ExecuteReader();

without knowing which provider you are using

不知道您使用的是哪个提供商

it can be:

有可能:

private static IDbCommand GiveMeSomeCommand()
{
    return new OleDbCommand();
}

or it can be

或者它可以是

private static IDbCommand GiveMeSomeCommand()
{
    return new SqlCommand();
}

or whatever.

管他呢。

EDIT:

编辑:

You can also use the DBFactories.

您还可以使用 DBFactories。

DbProviderFactory factory = GiveMeSomeFactory();
IDbCommand command = factory.CreateCommand();
IDataReader r = command.ExecuteReader();

//and create more objects
IDataAdapter adapter = factory.CreateDataAdapter();
IDbConnection conn = factory.CreateConnection();

and then create your provider in other layer

然后在其他层创建您的提供者

private DbProviderFactory GiveMeSomeFactory()
{
    if(something)
        return SqlClientFactory.Instance;
    else if(somethingElse)
        return OracleFactory.Instance;
    else if(notThisAndNotThat)
        return MySqlFactory.Instance;
    else
        return WhateverFactory.Instance;

}

回答by David Grant

It returns an interface because the implementation of the interface isn't important, just the API that the interface provides.

它返回一个接口,因为接口的实现并不重要,重要的是接口提供的 API。

回答by Jader Dias

"Returns an interface"really means:

“返回一个接口”真正的意思是:

Returns an instance of some class that implements that interface

返回实现该接口的某个类的实例

In this case, it returns an object very similar to a SqlDataReaderobject, that lets you to execute methods like Read()and implements the IDisposableand IDataRecordinterfaces.

在这种情况下,它返回一个与对象非常相似的SqlDataReader对象,它允许您执行Read()和实现IDisposableIDataRecord接口等方法。

回答by Rowland Shaw

It's not returning an Interface per se, but instead an object that supports that interface.

它本身并不返回接口,而是返回一个支持该接口的对象。

回答by Binary Worrier

The method will return an object, which is an instance of a class, and that type of class will support IDataReader.

该方法将返回一个对象,该对象是一个类的实例,并且该类型的类将支持IDataReader.

The point is, the type of the object isn't important, just the fact that the class implements the interface.

关键是,对象的类型并不重要,重要的是类实现了接口。

If you're driving a car, you don't need to know if it's a ford, or a toyota, you drive the car the same way.

如果你正在驾驶汽车,你不需要知道它是福特还是丰田,你以同样的方式驾驶汽车。

The driving interfaceis the same, once the car supports the interface, you can drive it.

驾驶界面是一样的,一旦汽车支持该界面,你就可以驾驶它。

Ditto with the IDataReader, once the class that is returned supports the interface, you can use it.

同上IDataReader,一旦返回的类支持该接口,您就可以使用它。

回答by random

It returns an object that implements this particular interface, and that's all you really care about. The object is a IDataReader, and can perform all the methods IDataReaderhas.

它返回一个实现这个特定接口的对象,这就是你真正关心的。对象是一个IDataReader,并且可以执行所有的方法IDataReader