What we’re trying to do is to write our database method to the interface. Not to the concrete class.
Here is the complete code.
public interface IDataHelper
{
void Connect();
void Execute();
}
//SqlDataHelper - using SQL database
public class SqlHelper :IDataHelper
{
public void Connect()
{
//SqlDataHelper Implementation
Console.WriteLine("Connectiing sql..");
}
public void Execute()
{
//SqlDataHelper Implementation
Console.WriteLine("executing Sql..");
}}
//OracleHelper - using Oracle database
public class OracleHelper : IDataHelper
{
public void Connect()
{
//SqlDataHelper Implementation
Console.WriteLine("Connectiing oracle..");
}
public void Execute()
{
//SqlDataHelper Implementation
Console.WriteLine("executing oracle..");
}}
public class UserDataBase
{
public UserDataBase()
{}
//method implementing to the IDataHelper interface
public static void CheckDB(IDataHelper help)
{
help.Connect();
help.Execute();
}
public static void Main(string[] args)
{
//Use 1
//Oracle as the Database
OracleHelper oraleHelper = new OracleHelper();
CheckDB(oraleHelper);
//Use 2
//Sql as the Database
SqlHelper sqlHelper = new SqlHelper();
CheckDB(sqlHelper);
}}
No comments:
Post a Comment