Trying to Create a Test Class for System.Data.SqlClient

M

mmatchyn

I have been trying to create a class similar to SqlClient but one that
prints sql statements instead of running them on the database. This
way when I want to go into test mode I comment out the SqlClient class
and comment in the test class.

The problem I am facing now is I have to limit myself to simple
functions for the SqlDataReader class. I would like to copy in the
SqlDataReader object exactly the way it is so that I can use this test
code universally on every C# project. The final result would be
calling this sql test class defines SqlDataReader exactly the way it
is in SqlClient, but SqlCommand will be defined differently so that
all sql update/insert/delete commands are printed and never actually
run on the database.

Any ideas on how I might go about this?

Is there a way I can define SqlDataReader within a user defined class
SqlTest and have it extend the SqlDataReader class of SqlClient? Then
I already have my own definition for SqlCommand that works like a
charm for the kind of testing I am doing.

Another possibility can I call SqlClient to get the SqlDataReader
class but then override the SqlCommand class with the functions I
already defined? I need some help with this.

Mark
 
A

Ashot Geodakov

class SqlCommand : DbCommand

You can try to derive your own class from DbCommand and implement its
abstract methods.

Then in your code you can do this:

DbCommand cmd = new SqlCommand();
cmd.MethodX();

When you need to use your class instead, replace the line above with

DbCommand cmd = new YourCommandClass();
cmd.MethodX();
 
M

mmatchyn

I am just looking to build a class that I can simply comment out or
back in in place of the SqlClient code without changing any code other
than the using statement. So far my code works for the more basic
SqlClient functions, but for the more complex functions I will need to
extend or implement the sql data reader class so all functions are
accounted for.
 
M

mmatchyn

And you can return the actual (real) SqlClient or SqlCommand or SqlReader.

SqlReader implements IDataReader. Thus you could create your own
IDataReader, and have a factory.

Look at the ~interfaces of those real sql objects.

I can't return the actual SqlReader or SqlCommand because that would
require me to include the System.Data.SqlClient in the original
function. I want to get away from using the SqlClient in the orignal
function, so that when I debug I comment out SqlClient and comment in
SqlTest. This way it will automatically use the same code but the
code will act differently for debug mode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top