DataClass Case Study #1: Solving the Duplication Problem
-
Upload
max-guernsey -
Category
Technology
-
view
296 -
download
0
description
Transcript of DataClass Case Study #1: Solving the Duplication Problem
- 1. DataClass Case Studies Problem 1: Duplication
2. Duplication
- A single change in multiple places
3. Not all duplications are created equal 4. Compiled symbols only duplicate logical name 5. Magic strings duplicate data 6. Copy-and-paste duplicates behavior 7. Example of Duplication public class MyQueryRunner { public int RunMyQuery(IDbConnection conn) { using (var cmd = conn.CreateCommand()) { cmd.CommandText = @"SELECT MAX([Tons]) FROM [Grapes] INNER JOIN [Fungi] ON [Grapes].[InfectionID] = [Fungi].[ID] WHERE [Fungi].[Name] = 'botrytis cinerea'"; return (int)cmd.ExecuteScalar(); } } } CREATE TABLE Fungi([ID] UniqueIdentifier, [Name] NVARCHAR(200)) -- populate table INSERT INTO Fungi([ID], [Name]) VALUES(newid(), 'botrytis cinerea') CREATE TABLE Grapes([Variety] NVARCHAR(50), [Tons] INT, [InfectionID] UniqueIdentifier) 8. Single Logical Document
- Application service providers generally same platform as consumers
9. Database service providers typically not 10. Two logical documents:
- Create/upgrade database (SQL)
11. Converse with database (C#, Java, XML, etc.) 12. DataClass Changes This
- One logical document defines
- Interface
13. Construction 14. Behavior Distinguishes logical design from physical 15. ...just like a real class 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(); } } } 19. Other Topics
- Version management
20. Coupling clients to the right version 21. Minimizing cost of interface