what you are missing is a "factory" class that builds instances for outside world based on parameters or configuration settings it receives (if they want to have the outside world influencing its behavior) or make their own decisions, i.e. they will create the instances of either type A or B based on their internal logic that cannot be changed from the outside. In the code below you will see the DatabaseFactory which is what you were missing. Also, note the private/public/internal modifiers which are important to make sure you expose what is needed and not more than you need (making all classes public creates confusions to those who want to use your library)
Here is the code
// interfaces must be public - this is the "contract" with the outside world / assemblies
public interface IDatabase
{
void Create(DataObject obj);
}
// classes that implement interfaces should be internal - outside world don't know about them
internal class SQLDatabase : IDatabase
{
// internal on constructor helps you to make sure you are the only one
// that can create such an instance
internal SQLDatabase()
{
}
void Create(DataObject obj)
{
Entity dbContext = new Entity();
dbContext.Data.Add(obj);
dbContext.SaveChanged();
}
}
internal class OracleDatabase : IDatabase
{
internal OracleDatabase()
{
}
void Create(DataObject obj)
{
//oracle creation method
}
}
public class DataObject
{
int Data1;
string Data2;
}
// this is the factory class that creates the instances for you (ourside world)
public class DatabaseFactory
{
// you can use either params or ideally app.config keys
public IDatabase CreateInstace()
{
if (ConfigSetting == "SQL")
{
return new SQLDatabase();
}
else if (ConfigSetting == "Oracle")
{
return new OracleDatabase();
}
else throw new System.Exception ("invalid configuration setting key");
}
}
// this is in external assembly:
public class CreateElementOnDatabase
{
DataObject myObj = new DataObject();
myObj.Data1 = 10;
myObj.Data2 = "Test String";
// you only work with public interfaces
// and the factory creates the instances for you ...
IDatabase db = DatabaseFactory.CreateInstace();
db.Create(myObj);
}