Modeling with MSchema

I am, as many other developers I presume, in the early process of grasping what Oslo is all about, so what could be better than sharing that process (and mistakes I do) with others. :)
In this post I will retell a lightning talk I had a while ago at Dotway’s office in Stockholm.

Before having this Lightning talk I installed the Oslo SDK January 2009 CTP and SQL Server 2008, and of course I read the excellent documentation that comes along with the Oslo SDK.

So this step-by-step talk was about how to model a fracture of a simple CV domain using the MSchema and Ipad (Intellipad) and how to use the M tool chain to put the model in SQL Server.  I deliberately kept the domain as simple as possible just to be able to show some of the basic steps involved in the modeling process.

Here it goes…

So, the first thing you need to do is to open up the text tool Ipad (Samples Enabled). You can do this either by running this in a command prompt: “C:\Program Files\Microsoft Oslo SDK 1.0\Bin\Intellipad\ipad.exe” /configuration:ipad-vs-samples.xaml, or even simpler by opening it from the start menu.
Save Untitled1 as CV.m (.m is the format for M source files). Notice how the mode was changed from Standard Mode into M Mode.

Everything you write in MSchema need to be inside a module, so add a module called CV:

bild11

A module controls visibility in the same way as CLR-namespaces do, however it also allocate storage, among other things. You may split up a module in different files, and a file may contain more than one module but a module cannot contain other modules. A module corresponds to a SQL Server database schema.
The CV domain has consultants (each with an id and a name field) so let’s define that by adding a Consultant type into your module, something like this:

bild23

The AutoNumber() above, which is used to set a default value, maps to “identity”in T-SQL and the identity(id) constraint maps to the primary key on that field.
Wouldn’t it be interesting to see the T-SQL that maps to our type?
In Ipad you have two different views that let you see a preview of the T-SQL that corresponds to your model. The first one called Repository T-SQL Preview displays T-SQL based on the Oslo Repository design patterns. The second one called Generic T-SQL Preview displays a simpler version of the T-SQL needed to create a database schema corresponding to your model. For the purpose of this tutorial use the Generic T-SQL Preview menu choice, which you find under the M Mode menu.
At the moment all that is displayed is the following comment:

bild3

So go ahead and add an extent called Consultants to your module and see what will happen:

bild4

This will produce the following in the T-SQL preview window:

bild5

As you can see the extent Consultants corresponds to the table called Consultants.
Now add another type to your module called Skill with the fields Id, Name and description, and an extent called Skills:

bild6


Once again you get instant feedback from the T-SQL preview window:

bild7


Now let’s say you want a many-to-many relationship between the Consultant and Skill types. One way in which you can accomplish this is by adding a new skills field in the type Consultant and assign it the type Skill. You will also need to add a membership constraint to the field like this:

bild8


This will be transformed into a new SQL junction table:

bild9


The same thing would have been accomplished by adding this where predicate to the Consultants extent:

bild10


Although not shown here, in MSchema you can also model many-to-many relationships as two one-to-many relationships.
With this in place you can define a couple instances of consultant using MGraph:

bild111


So how do you get the model into a SQL Server database?
The first step is to compile your CV.m file into an image file. You do this with the m compiler, m.exe
Open up a new command prompt and navigate to the folder containing your CV.m file. Run the following line:
“C:\Program Files\Microsoft Oslo SDK 1.0\Bin\m.exe” CV.m -p:image -t:TSQL10
If you look inside the folder you will now find a file called CV.mx. The next step is to generate a new database containing the database schema found in CV.mx. This is done with the tool called mx.exe. Run the following line in the command prompt:
“C:\Program Files\Microsoft Oslo SDK 1.0\Bin\mx.exe” -i:cv.mx -db:cvdb -c -ig -f –verbose
Switch to SQL Server Management Studio and check that a database called cvdb has been created and that it contains the three tables CV.Consultants, CV.Skills and CV.Consultants_Skills. The database should also contain the insert into statements that you created with MGraph.

Ok, that’s it for now!

About these ads

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: