Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
public interface IDatabaseConnection
{
IDatabaseCommand create_command(string sql_statement);
}
public class DatabaseConnection : IDatabaseConnection
{
private readonly IDbConnection connection;
private readonly IDbTransaction transaction;
public DatabaseConnection(IDbConnection connection, IDbTransaction transaction)
{
this.connection = connection;
this.transaction = transaction;
}
public IDatabaseCommand create_command(string sql_statement)
{
var command = connection.CreateCommand();
command.CommandText = sql_statement;
command.Transaction = transaction;
return new DatabaseCommand(command);
}
}
public interface IDatabaseCommand : IDisposable
{
void add_parameter(DbParameter parameter);
DataTable find_records();
void preform_data_altering_query();
T get_a_specific_value<T>();
}
public class DatabaseCommand : IDatabaseCommand
{
private readonly IDbCommand command;
public DatabaseCommand(IDbCommand command)
{
this.command = command;
}
public void Dispose()
{
command.Dispose();
}
public void add_parameter(DbParameter parameter)
{
var param = command.CreateParameter();
param.ParameterName = parameter.name;
param.Value = parameter.value;
command.Parameters.Add(param);
}
public DataTable find_records()
{
var table = new DataTable();
table.Load(command.ExecuteReader());
return table;
}
public void preform_data_altering_query()
{
command.ExecuteNonQuery();
}
public T get_a_specific_value<T>()
{
var result = command.ExecuteScalar();
if (result == DBNull.Value) return default(T);
return (T) result;
}
}
public abstract class DbParameter
{
public string name { get; private set; }
public object value { get; private set; }
public DbType type { get; private set; }
protected DbParameter(string name, object value, DbType type)
{
this.name = name;
this.value = value;
this.type = type;
}
}
public class StringParameter : DbParameter
{
public StringParameter(string name, string value)
:base(name, value, DbType.String)
{
}
}
public class IntegerParameter : DbParameter
{
public IntegerParameter(string name, int value)
: base(name, value, DbType.Int32)
{
}
}
public class LongParameter : DbParameter
{
public LongParameter(string name, long value)
: base(name, value, DbType.Int64)
{
}
}
public class ShortParameter : DbParameter
{
public ShortParameter(string name, short value)
: base(name, value, DbType.Int16)
{
}
}
public class DateTimeParameter : DbParameter
{
public DateTimeParameter(string name, DateTime value)
: base(name, value, DbType.DateTime)
{
}
}
public class FixedLengthStringParameter : DbParameter
{
public FixedLengthStringParameter(string name, string value)
: base(name, value, DbType.StringFixedLength)
{
}
}
public class CustomerRepository
{
private readonly IConnectionManager manager;
public CustomerRepository(IConnectionManager manager)
{
this.manager = manager;
}
public CustomerRepository()
: this(new HttpConnectionManagerAdapter())
{
}
public DataTable find_customers_by_state(string state)
{
var connection = manager.current("foo");
using (var command = connection.create_command("select * from [customer] where [state] = @state"))
{
command.add_parameter(new StringParameter("state", state));
return command.find_records();
}
}
public int total_number_of_customers()
{
var connection = manager.current("foo");
using (var command = connection.create_command("select count(1) from [customer]"))
{
return command.get_a_specific_value<int>();
}
}
}
public partial class _Default : Page
{
private CustomerRepository repository;
protected override void OnLoad(EventArgs e)
{
if(IsPostBack) return;
var state = Request.QueryString["state"];
repeater.DataSource = repository.find_customers_by_state(state);
number_of_all_customers.Text = repository.total_number_of_customers().ToString();
DataBind();
}
protected override void OnInit(EventArgs e)
{
repository = new CustomerRepository();
}
}