Here is a link describing this approach.
Below is my resultant class based on Dan's suggestion
using System;
using System.Data;
using System.Data.Common;
namespace CustomDataAccess{
public class DataSetBuilder
{
#region Properties
private DataSet _DataSet;
public DataSet DataSet { get { return _DataSet; } }
#endregion
#region Constructors
public DataSetBuilder()
{
this._DataSet = new DataSet();
}
public DataSetBuilder(string DataSetName)
{
this._DataSet = new DataSet(DataSetName);
}
public DataSetBuilder(DataSet DataSet)
{
this._DataSet = DataSet;
}
#endregion
#region Public Methods
public DataSetBuilder InsertTables(DataTable Table)
{
this._DataSet.Tables.Add(Table);
return this;
}
public DataSetBuilder InsertTables(string DbProviderName, string ConnectionString, string TableName, string CommandText)
{
System.Data.Common.DbDataAdapter adapter = Create_Adapter(DbProviderName, ConnectionString);
Fill_Adapter(adapter, TableName, CommandText);
adapter.SelectCommand.Connection.Close();
return this;
}
public DataSetBuilder InsertTables(string DbProviderName, string ConnectionString, string[] TableName, string[] CommandText)
{
if (TableName.Length != CommandText.Length)
{
throw new Exception("Error: Must provide a table name for each command.");
}
System.Data.Common.DbDataAdapter adapter = Create_Adapter(DbProviderName, ConnectionString);
for (int i = 0; i < TableName.Length; i++)
{
Fill_Adapter(adapter, TableName[i], CommandText[i]);
}
adapter.SelectCommand.Connection.Close();
return this;
}
public void AddRelations(string ParentTable, string PrimaryKey, string ChildTable, string ForeignKey, bool NestingRule)
{
Add_Relations(ParentTable, PrimaryKey, ChildTable, ForeignKey, NestingRule);
}
public void AddRelations(string[] ParentTable, string[] PrimaryKey, string[] ChildTable, string[] ForeignKey, bool[] NestingRule)
{
for (int i = 0; i < ParentTable.Length; i++)
{
Add_Relations(ParentTable[i], PrimaryKey[i], ChildTable[i], ForeignKey[i], NestingRule[i]);
}
}
#endregion
#region Private Methods
private System.Data.Common.DbDataAdapter Create_Adapter(string DbProviderName, string ConnectionString)
{
DbProviderFactory dbFactory = System.Data.Common.DbProviderFactories.GetFactory(DbProviderName);
System.Data.Common.DbConnection connection = dbFactory.CreateConnection();
connection.ConnectionString = ConnectionString;
connection.Open();
System.Data.Common.DbCommand command = dbFactory.CreateCommand();
command.Connection = connection;
System.Data.Common.DbDataAdapter adapter = dbFactory.CreateDataAdapter();
adapter.SelectCommand = command;
return adapter;
}
private void Fill_Adapter(System.Data.Common.DbDataAdapter Adapter, string TableName, string CommandText)
{
Adapter.SelectCommand.CommandText = CommandText;
Adapter.Fill(_DataSet, TableName);
}
private void Add_Relations(string ParentTable, string PrimaryKey, string ChildTable, string ForeignKey, bool NestingRule)
{
DataColumn pk = _DataSet.Tables[ParentTable].Columns[PrimaryKey];
DataColumn fk = _DataSet.Tables[ChildTable].Columns[ForeignKey];
DataRelation relation = _DataSet.Relations.Add(pk, fk);
relation.Nested = NestingRule;
}
#endregion
}}
An implementation of an interface is the realization of that interface. Realization is being implemented within the class. Just return an instance of the class (A or B) which realizes the return-type interface.
interface IA{}
class A: IA{}
class UseIA
{
public IA DesiredMethod()
{
return new A();
}
}
Best Answer
This is not allowed, as Chris Burrows (who helped create and implement
dynamic
) explains: