Several teams I've been on (and no I don't recall all that we were doing, it's been a while) used IDataReader instead of SqlDataReader when coding the Data Layer CRUD methods.
Can someone tell me why our architects preferred IDataReader always?
Several teams I've been on (and no I don't recall all that we were doing, it's been a while) used IDataReader instead of SqlDataReader when coding the Data Layer CRUD methods.
Can someone tell me why our architects preferred IDataReader always?
The reason has to do with coding to an interface: if you code to IDataReader
, you have better assurance that you can switch to a different brand of RDBMS later, should there be a need to do so. If you code to SqlDataReader
, on the other hand, your code may still be easy to port to use with, say, Oracle, but you would have less confidence of that (and you will have to do the porting, too).
IDataReader
is abstraction, not enforcing any concrete types but rather contract (ie. perform CRUD operations). For the purpose of extendability and testability programming to interface is preferred.
Having dependencies represented by interfaces allows you to change them much more easily, be it in unit tests (when providing mock objects instead of real implementations) or non-production environments (like working with different implementations).
For additional research, I suggest giving a look at dependency injection and other concepts related to SOLID principles.
That's ridiculous. Chances that you'll need to change entire data layer are relatively low (as this is rather large cost), but there are fairly good chances you'll need to support additional ones. Consider following scenario:
So there you go. This actually happened to a project I've been working on, and in the span of less than 10 months it all went from Oracle is all we need to Oracle, MySQL and SQLite. Sure, those transitions always required some extra work (primarily due to DB differences) but majority of DAL code was reused. Wouldn't that be possible if our DAL was tightly coupled to Oracle? I'm sure it would, but I'm just as sure it would take a lot more time and effort.
Lesson learned - always plan for success.
I think that the The IDataReader is an interface. that implements and handles many classes that handle database acces while if you use SqlDataReader is an implemented class that actually uses the IDataReader interface but instead of being limited to a class IDataReader allows you to implement and easily change the database provider if you decide to do so in the future therefore you can avoid rewriting references if you want to switch database engines