// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.Common; using DI.Common; namespace DI.No { public class MockDataAccessLayer : IDataAccessLayer { public Data GetOneRecord(int f1) { return new Data { F1 = f1, F2 = "X" + f1 }; } public IList GetAllRecords() { IList res = new List(); res.Add(new Data { F1 = 1, F2 = "A" }); res.Add(new Data { F1 = 2, F2 = "BB" }); res.Add(new Data { F1 = 3, F2 = "CCC" }); return res; } } public class DbDataAccessLayer : IDataAccessLayer { private string prov; private string constr; private IDbConnection GetConnection() { IDbConnection con = DbProviderFactories.GetFactory(prov).CreateConnection(); con.ConnectionString = constr; return con; } public DbDataAccessLayer() { this.prov = ConfigurationManager.AppSettings["DBProvider"]; this.constr = ConfigurationManager.AppSettings["DBConnectionString"]; } public Data GetOneRecord(int f1) { using(IDbConnection con = GetConnection()) { con.Open(); IDbCommand cmd = con.CreateCommand(); cmd.CommandText = "SELECT f1,f2 FROM t1 WHERE f1 = @f1"; cmd.Connection = con; IDbDataParameter pf1 = cmd.CreateParameter(); pf1.ParameterName = "@f1"; pf1.DbType = DbType.Int32; pf1.Value = f1; cmd.Parameters.Add(pf1); IDataReader rdr = cmd.ExecuteReader(); if(rdr.Read()) { return new Data { F1 = (int)rdr["f1"], F2 = (string)rdr["f2"] }; } else { return null; } } } public IList GetAllRecords() { IList res = new List(); using(IDbConnection con = GetConnection()) { con.Open(); IDbCommand cmd = con.CreateCommand(); cmd.CommandText = "SELECT f1,f2 FROM t1"; cmd.Connection = con; IDataReader rdr = cmd.ExecuteReader(); while(rdr.Read()) { res.Add(new Data { F1 = (int)rdr["f1"], F2 = (string)rdr["f2"] }); } } return res; } } public class PlainPresenter : IPresenter { public void Start() { // nothing } public void One(Data o) { Console.WriteLine(o.F1 + " " + o.F2); } public void Finish() { // nothing } } public class HtmlPresenter : IPresenter { private String lbl1; private String lbl2; public HtmlPresenter() { this.lbl1 = ConfigurationManager.AppSettings["Label1"]; this.lbl2 = ConfigurationManager.AppSettings["Label2"]; } public void Start() { Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(""); } public void One(Data o) { Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(""); } public void Finish() { Console.WriteLine("
" + lbl1 + "" + lbl2 + "
" + o.F1 + "" + o.F2 + "
"); } } public class StandardApplication : IApplication { private IDataAccessLayer dal; private IPresenter p; public StandardApplication() { //this.dal = new MockDataAccessLayer(); this.dal = new DbDataAccessLayer(); //this.p = new PlainPresenter(); this.p = new HtmlPresenter(); } public void Run() { // one p.Start(); Data o = dal.GetOneRecord(2); p.One(o); p.Finish(); // all p.Start(); foreach(Data o1 in dal.GetAllRecords()) { p.One(o1); } p.Finish(); } } public class Program { public static void Main(string[] args) { IApplication app = new StandardApplication(); app.Run(); } } }