// 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 Unity; using Unity.Injection; using Unity.Lifetime; using DI.Common; namespace DI.Unity { 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(String prov, String constr) { this.prov = prov; this.constr = constr; } 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(String lbl1, String lbl2) { this.lbl1 = lbl1; this.lbl2 = lbl2; } 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(IDataAccessLayer dal, IPresenter p) { this.dal = dal; this.p = p; } 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 C1 { private static object lck = new object(); private static int v1 = 0; private int v2; public C1() { lock(lck) { v1++; v2 = v1; } } public override string ToString() { return string.Format("{0}/{1}", v2, v1); } } public class C2 { private static object lck = new object(); private static int v1 = 0; private int v2; public C2() { lock(lck) { v1++; v2 = v1; } } public override string ToString() { return string.Format("{0}/{1}", v2, v1); } } public class C3 { private static object lck = new object(); private static int v1 = 0; private int v2; public C3() { lock(lck) { v1++; v2 = v1; } } public override string ToString() { return string.Format("{0}/{1}", v2, v1); } } public class C { private C1 c1a, c1b; private C2 c2a, c2b; private C3 c3a, c3b; public C(C1 c1a, C1 c1b, C2 c2a, C2 c2b, C3 c3a, C3 c3b) { this.c1a = c1a; this.c1b = c1b; this.c2a = c2a; this.c2b = c2b; this.c3a = c3a; this.c3b = c3b; } public void Test() { Console.WriteLine("Singleton : {0} {1}", c1a, c1b); Console.WriteLine("Transient : {0} {1}", c2a, c2b); Console.WriteLine("Default : {0} {1}", c3a, c3b); } } public class Program { public static void Main(string[] args) { UnityContainer cont = new UnityContainer(); cont.RegisterType(); //cont.RegisterType(); cont.RegisterType(); cont.RegisterType(new InjectionConstructor(new object[] { ConfigurationManager.AppSettings["DBProvider"], ConfigurationManager.AppSettings["DBConnectionString"]})); //cont.RegisterType(); cont.RegisterType(); cont.RegisterType(new InjectionConstructor(new object[] { "F1", "F2" })); cont.RegisterType(new ContainerControlledLifetimeManager()); cont.RegisterType(new TransientLifetimeManager()); cont.RegisterType(); cont.RegisterType(); IApplication app = cont.Resolve(); app.Run(); C o = cont.Resolve(); o.Test(); } } }