There has always been a problem of duplication with regards to database and client code. These are the slides I use to demonstrate how that problem can be solved using DataClass.
DataClass's official website: http://dataclass.hexsw.com/
16. Example Database Class (Part I) namespace HexagonSoftware.DataClass.Examples { database WineProductionDataStore { stereotypes table, column, values, value; version 1.0 : initialized { design { public table Grapes { public column Identity ("ID"); public column Tons; public column InfectionIdentity ("InfectionID"); } public table Fungi { public column Identity ("ID"); public column Name; public values KnownFungi { public value NobleRot with Name = 'botrytis cinerea'; } } } } } }
17. Example Database Class (Part II) // in version 1.0 construction { in context Fungi { step sql { CREATE TABLE [$[Fungi]]([$[Identity]] UNIQUEIDENTIFIER, [$[Name]] NVARCHAR(50)) } step sql { INSERT INTO [$[Fungi]]([$[Identity]], [$[Name]]) VALUES(newid(), '$[KnownFungi.NobleRot.Name]') } } in context Grapes { step sql { CREATE TABLE [$[Grapes]]( [$[Identity]] UniqueIdentifier, [$[Tons]] INT, [$[InfectionIdentity]] UniqueIdentifier) } } }
18. Example Database Class Usage using DBDesign = WineProductionDataStore.Design._1_0; public class MyQueryRunner { public int RunMyQuery(IDbConnection conn) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = string.Format( @"SELECT MAX([{0}]) FROM [{1}] INNER JOIN [{2}] ON [{3}].[{4}] = [{2}].[{5}] WHERE [{2}].[{6}] = '{7}'", DBDesign.Grapes.Tons.GetPhysicalName(), DBDesign.Grapes.GetPhysicalName(), DBDesign.Fungi.GetPhysicalName(), DBDesign.Grapes.InfectionIdentity.GetPhysicalName(), DBDesign.Fungi.Identity.GetPhysicalName(), DBDesign.Fungi.Name.GetPhysicalName(), DBDesign.Fungi.KnownFungi.NobleRot.Name ); return (int)cmd.ExecuteScalar(); } } }