简体   繁体   中英

Passing an interface instead of an object instance

Take for instance the SqlBulkCopy.WriteToServer() method. One of the overloads takes an IDataReader as the parameter. My question is, what's the benefit/advantage to passing an interface to a method instead of the object instance itself?

http://msdn.microsoft.com/en-us/library/434atets.aspx

You can have many possible implementations for that one interface. Its better to depend on an abstraction (in this case an interface) than an actual concrete class - this allows much better flexibility and testability.

This also puts the focus on what is really required by the WriteToServer() method - the only thing its contract requires is for the caller to pass in any instance of a concrete class that provides the methods / properties declared by the IDataReader interface.

Passing an interface means that you can pass any object that implements that interface not just one particular object.

This makes the code more flexible as it doesn't have to know about all the possible objects that may implement the interface now or in the future.

It also makes it more robust as it only has to deal with the properties and methods on the interface which are well known and defined.

The formal parameter type is an interface type - that means that you can pass in any object that implements this interface (or rather, an instance of an object that implements the interface).

You are not passing in an interface, you are passing in an object that conforms to the contract defined by the interface.

So, if your data source is SQL Server, you would pass a SqlDataReader , if Oracle, an OracleDataReader .

You could also implement your own data reader and pass that to the function and even write a mock data reader to test the method thoroughly.

This is a well known design principle - Program to an Interface, not an implementation .

And from MSDN - When to Use Interfaces :

Interfaces are a powerful programming tool because they let you separate the definition of objects from their implementation.

When a method lists one of its arguments as an interface, it isn't requesting you to pass in an instance of that interface (which is impossible anyway, you can create instances of interfaces), it's asking you to pass in any object that implements that interface.

Example:

interface IMyObject {
    public void SomeMethod();
}

public class MyObject : IMyObject {
    public void SomeMethod() {
        // implementing code here
    }
}

You can now pass any instance of MyObject as an argument that is of type IMyObject:)

public class YourObject {
    public void DoSomething(IMyObject o) {
        // some code here
    }
}

YourObject yo = new YourObject();
MyObject   mo = new MyObject();
yo.DoSomething(mo); // works

I hope that makes sense!

Actually, it expects you to pass an instance of a type that implements the interface, rather than the interface itself.

The interface type is used when the only thing the method cares about are the methods declared by the interface. As long as the object implements that interface, methods defined in it can be invoked on the object.

This is one of the reasons for interfaces - in this example, all the consumer of the interface (ie the function) cares about is that it can read data - it doesn't mind whether it's from an SqlDataReader or an OleDataReader or for any other provider - the alternate would be providing separate overloads that are virtually identical for every possible Data Reader (which is of course impractical, given someone may come up with one for, say dBase or a more exotic database engine)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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