So can one simply change the DB and be almost independent from DB in classic ADO.NET?
Of course you can, but to be able to do that we have to debunk this statement.
Seems as magic pill, however all ADO.NET statements are hard coded.
That's not a byproduct of ADO.NET - that's a byproduct of your architecture. You're building the SQL statements in the wrong place. You need concrete, provider specific models, that are capable of building statements that differ between providers. This isn't as bad as it sounds - most statements can be auto-generated leveraging reflection - it's just the special cases.
For example, let's say I had a model like this:
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
and let's say I wanted to generate a SELECT
statement out of that. Well, I'm going to first need a couple attributes to tell me what property is the PK and what properties are data fields:
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
internal sealed class DataFieldAttribute : Attribute
{
public DataFieldAttribute()
{
}
}
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
sealed class PrimaryKeyAttribute : Attribute
{
public PrimaryKeyAttribute()
{
}
}
and now I need to decorate that class:
public class Employee
{
[PrimaryKey]
public int ID { get; set; }
[DataField]
public string Name { get; set; }
[DataField]
public DateTime DateOfBirth { get; set; }
}
and now I just need a simple process to create a SELECT
statement, so first let's build a base data model class:
public abstract class DataModelBase
{
protected string _primaryKeyField;
protected List<string> _props = new List<string>();
public DataModelBase()
{
PropertyInfo pkProp = this.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(PrimaryKeyAttribute), false).Length > 0).FirstOrDefault();
if (pkProp != null)
{
_primaryKeyField = pkProp.Name;
}
foreach (PropertyInfo prop in this.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(DataFieldAttribute), false).Length > 0))
{
_props.Add(prop.Name);
}
}
public virtual string TableName { get { return this.GetType().Name; } }
public virtual string InsertStatement
{
get
{
return string.Format("INSERT INTO [{0}] ({1}) VALUES ({2})",
this.TableName,
GetDelimitedSafeFieldList(", "),
GetDelimitedSafeParamList(", "));
}
}
public virtual string UpdateStatement
{
get
{
return string.Format("UPDATE [{0}] SET {1} WHERE [{2}] = @{2}",
this.TableName,
GetDelimitedSafeSetList(", "),
_primaryKeyField);
}
}
public virtual string DeleteStatement
{
get
{
return string.Format("DELETE [{0}] WHERE [{1}] = @{1}",
this.TableName,
_primaryKeyField);
}
}
public virtual string SelectStatement
{
get
{
return string.Format("SELECT [{0}], {1} FROM [{2}]",
_primaryKeyField,
GetDelimitedSafeFieldList(", "),
this.TableName);
}
}
protected string GetDelimitedSafeParamList(string delimiter)
{
return string.Join(delimiter, _props.Select(k => string.Format("@{0}", k)));
}
protected string GetDelimitedSafeFieldList(string delimiter)
{
return string.Join(delimiter, _props.Select(k => string.Format("[{0}]", k)));
}
protected string GetDelimitedSafeSetList(string delimiter)
{
return string.Join(delimiter, _props.Select(k => string.Format("[{0}] = @{0}", k)));
}
}
and now let's inherit from that data model:
public class Employee : DataModelBase
and boom, now I can get those statements any time I need them, and those statements work for any concrete provider right now.
And then I'll use Dapper to get the data because it leverages the IDbConnection
interface like what you need and it's ridiculously fast - and there you go - a provider independent solution that would easily be extended to build an Oracle version of Employee
if necessary.
This framework has internal independent language statements that is transformed to the choosen DB statesment, which looks as truely magic pill
Sure, it may look like a magic pill, but it's really a curse in a lot of ways. You have no flexibility (at least it's not easy) to build statements that are optimized for your needs to support high transaction and volume databases. You truly are subject to the master here. The .NET Entity Framework builds those statements for you, and I can't even count how many questions on StackOverflow have been over how can I change the SQL that's being generated by this LINQ statement leveraging the .NET Entity Framework.