Posts tagged UML

Generating Database Objects from an UML Model – Part 1

imageIn this mini-series of one post, I’ll show you how to generate SQL-script from an UML model.

First, you need a model. This can be any model, but here I use an UML model. The class diagram on the left shows a simple model consisting of a parent class and a related child class. Both are stereotyped <<table>> so you can see we are talking about a data model.

Both have some attributes. Note that their types have been declared explicitly.

The interesting part is their association. As you can see it is a traditional master-detail relationship. For clarity I have named the association and both association ends, but the latter is not strictly necessary.

This UML diagram can be represented in the following XML form:

<?xml version="1.0" encoding="utf-8"?>
<EntityModel>
  <Entity Name="TestParent">
    <Property Name="TestParentName1" Type="String" />
    <Property Name="TestParentNumber" Type="Integer" />
  </Entity>
  <Entity Name="TestChild">
    <Property Name="TestChildName" Type="String" />
  </Entity>
  <Association Name="FK_TestParent_TestChild">
    <End Name="TestParent_End" Role="TestParent" Multiplicity="0..1" />
    <End Name="TestChild_End" Role="TestChild" Multiplicity="*" />
  </Association>
</EntityModel>

Why that is interesting? That is because XML easily can be transformed into something useful.

For performing the actual transformation you can use, for example:

  1. XSLT stylesheet-driven transformation
  2. template-driven code generator
  3. code using XmlSerializer or XmlReader

Thus, it is transformed into a sql-script.

/****** Table [dbo].[TestParent] ******/
CREATE TABLE [dbo].[TestParent](
	[TestParentID] [int] IDENTITY(1,1) NOT NULL,
	[TestParentName1] [varchar](MAX) NULL,
	[TestParentNumber] [int] NULL,
 CONSTRAINT [PK_ParentTable] PRIMARY KEY CLUSTERED ([TestParentID] ASC)
)
GO
/****** Table [dbo].[TestChild] ******/
CREATE TABLE [dbo].[TestChild](
	[TestChildID] [int] IDENTITY(1,1) NOT NULL,
	[TestParentID] [int] NULL,
	[TestChildName] [varchar](50) NULL,
 CONSTRAINT [PK_ChildTable] PRIMARY KEY CLUSTERED ([TestChildID] ASC)
)
GO
/****** ForeignKey [FK_TestParent_TestChild] ******/
ALTER TABLE [dbo].[TestChild]
ADD CONSTRAINT [FK_TestParent_TestChild] FOREIGN KEY([TestParentID])
REFERENCES [dbo].[TestParent] ([TestParentID])
GO

The keen eye will notice that in the final sql-script primary and foreign key columns are introduced, which are not present in the original model (diagram nor xml file). The other way around, specification of associations in the original model is much richer than the resulting foreign key constraint in the sql-script. These “mismatches” must be handled and are typically present when performing O/RM mapping.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

UML Use Case – Uses or Extends?

If you are a more functional focussed person, you are probably thinking: who cares. And if you are, please tell me why do you functional people talk so much with such a low information density? Is it thinking out loud?

Okay. Back to topic.

If you are a technical übermensch – like yours truly – you probably sh*t on people who do not see the delicate difference between a uses and an extends relationship.

In short, a tiny refresher for your brain.

  • Uses (formerly known as include) indicates a “has a” relationship, and
  • Extends (formerly known as extend) indicates an “is a” relationship

.image

As you can see I still use pre-historic diagramming. Not only because I am wise and therefore somewhat older(fashioned), but merely because I use a freely available and (almost) open source diagramming tool StarUML. This tool unfortunately stuck at UML v1.3 Notwithstanding, an excellent tool.

Translated to a real world situation:

image If you are a car driver you want to drive a car. Something you must include at least once is to start the engine. An extension of driving a car is driving a formula 1 car, which indeed is something very special.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Extracting Business Knowledge From Data Models

Recently, I was following a discussion on extracting business knowledge from UML models. UML models, UML models, and more UML models.

Slowly a thought was emerging from my soaky brain:

hey dude, data models are models too.

code generationIn fact, a data model doesn’t always have to be represented by an ERD diagram.

It is also very feasible to diagram a data model using UML class diagrams. It even gets better when you stereotype your classes <<table>>. This enables code generation software to automagically recognize the stereotypes and – for example – generate SQL script for you.

Why on earth that could be remotely interesting?

Because your model is independent from its physical representation.

This way, you can “extract” a model from the physical database, introduce some enhancements, and re-apply the enhancements to the original physical database.

It enables you to enforce an uniform use of naming conventions.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Conceptual and Storage Model

The most common model is the data model. Deep in the application architecture a physical model can be found, which is a straight representation of the database (ERD diagram).DataSource ExplorerMore superficially, in the core of the application architecture, a conceptual representation of the same data model can be found (UML diagram). However, it should be kept in mind that both are derived from the same underlying data model, thus are essentially the same. As a result mapping from one to another is relatively straight forward (Data Source Explorer).

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

The Art of Creating A Top model

If you want to master the art of model-driven code generation, an essential thing you need is a model of some kind. Maybe you are in desperate need of Kate Moss, but that’s not the kind of model I talking about. I mean some kind of data model.

Preferably, you describe your model in UML. But that is not essential.

As far as I am concerned, you can write it down in notepad, if you like.

My model contains students. Students are member of a certain class, for example a class named Class 3a. A typical class consists of many students, but a student can only be part of one class. The students attend many different courses and a course can have many students.

This data model translates to database tables and relations. In this case you have a table Student, Class and Course. Student has a 1-to-many relationship with Class and a many-to-many relationship with Course.

If you created this database and then extracted a database model from the database, it would look like this:

class-student-course DBS

This would be a physical data model. A drawback of a physical model is that there is no proper way to show a many-to-many relationship. A join table StudentCourse is needed to show such a relationship. In fact it is a pattern to create a many-to-many join in a table.

In UML it looks like this:

class-student-course UML

And this is really all there is to create an UML class diagram.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)