Use Case: Eliminating duplication between client and database code

Abstract

There are a number of concerns around changing a database’s design. One of the bigger risks is that a database’s design might get out of sync with its clients. This is not an excuse to simply leave a database’s design in place without ever changing it; that pretty much guarantees it will be abused or become irrelevant. Instead, this is an opportunity find and eliminate the root cause of the problem: duplication of symbols and design descriptors between database servers and clients.

Learning Objectives

  • Exposing the design of a database to clients
  • Getting compiler feedback on coupling between client and database class

Body

Strong coupling is preferable to weak coupling, which is essentially duplication. Clients have traditionally been very weakly coupled to the databases they use. DataClass gives you the ability to change that. By defining design as a first class member of your database class, you have the ability to generate client code directly from a specified design.

This client code ensures that couplings between a client application and the database it uses are correct and it does this at compile time. That is, if a client is coupled to database structures that no longer exist or have been renamed, that client will fail to compile.

Consider the following example.

You can click on keywords and concepts in blue.
handdatabase HasAnInterface
{
  types int as integer;
  
  version 1.0 : initialized
  {
    design
    {
      public table CanBeCoupled
      {
        public column Identifier with DataType = type(int);
      }
    }
    
    construction
    {
      step sql
      {
CREATE TABLE $[CanBeCoupled]($[CanBeCoupled.Identifier] INT);
      }
    }
  }
}

HasAnInterface.dbc

That will get compiled into a .NET and/or Java class named HasAnInterface. Of course, that class will have the ability to produce a new instance of the HasAnInterface database class. It has something else, too: a set of symbols that allow a client to properly couple to the HasAnInterface class of database.

Here is an example of what a client that does that might look like:

using Design = HasAnInterface.Design._1_0;

public class UsesAnInterface
{
  public static void AddNumber(Connection conn, int toAdd)
  {
    conn.ExecuteSql(
"INSERT INTO " + Design.CanBeCoupled.GetPhysicalName() + " VALUES(?)", toAdd);
  }
}
      

UsesAnInterface.cs

If HasAnInterface.dbc and UsesAnInterface.cs get out of sync, there will be a compiler error that compiler error will protect you and your team from producing a pairing of client and database that cannot possibly work together.

Related Use Cases

Related Concepts

Related Keywords

Other Actions

documentation | all examples | use cases | concepts | keywords