HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Generic SQL Connector classes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sqlgenericclassesconnector

Problem

The need came up the other day to be able to easily switch back and forth between making IBM DB2 queries and MS SQL queries. I didn't want to mess with NHibernate as I was on a time crunch, and I also didn't want to have a million connection strings. Lastly I learned a while ago that for classes that needed to be used a specific way could be wrapped in a method with a Action with that type as a parameter. This way you can use it as needed in the base class, and in the derived classes you can just use it.

One known "issue" is that whatever provider you want to use it has to be installed and able to be instantiated by System.Data.DbProviderFactory. So I made IBaseDao in a futile attempt to be able to replace this with a mock... it might need some tweaking, but here it is for reference

public interface IBaseDao
{
    void AddParameter(string parameterName, object parameterValue);

    void ExecuteNonQuery();

    void ReadAll(Action readerActionBlock);

    void SetSqlText(string sql);
}


Now I did a few things in BaseDao that I'm unsure if it is bad practice or not. It mainly comes from unfamiliarity with the interfaces and implementations from System.Data. One is to make public the DbProviderFactory, and the other is to make the IDbCommand public.

```
public abstract class BaseDao : IBaseDao
{
public DbProviderFactory Factory { get { return _factory; } }

public IDbCommand Command { get; private set; }

private readonly DbProviderFactory _factory;

protected BaseDao(string providerName)
: this(DbProviderFactories.GetFactory(providerName))
{
}

protected BaseDao(DbProviderFactory factory)
{
_factory = factory;
}

public void ReadAll(Action readerActionBlock)
{
using (var dynamicReader = new DynamicDataReader(Command.ExecuteReader()))
{
while (dynamicReader.Read())
{
readerActionBlock(dynamicReader);
}
}
}

Solution

Some of this code lacks explanation, like what P_INFO and P_SLED represent. I guess these are your different sources? But "yeah right" is not a valid server name. If you need a placeholder name, you have all of *.example.com at your disposal.

That aside, it's a thin wrapper around DbConnection and friends, and I don't see what actual functionality it offers. I guess the motivation was to not have "a million connection strings", but if I understand correctly now you have a million references to DaoConnector.P_INFO. But maybe I don't understand what your library offers - it would be clearer if you had explicit examples of old client code vs new client code.

Context

StackExchange Code Review Q#91413, answer score: 2

Revisions (0)

No revisions yet.