“M” Samples Review: One-to-Many vs. Many-to-One

Isn’t this title funny? I like it 🙂

As I browse through the samples for the SQL Server Modeling CTP I find a lot of odd “M” code. This is sad, because Microsoft should know how to write “M” code. They are inventing it. Sure I’m expressing my personal opinion, but I think I have no hard time finding people that agree with me.

Why I do this in the “public”? Because I care! I like M and it’s concepts. For me M has the potential to become a great replacement for a variety of technologies. Most of all XSD, and maybe parts of SQL. That it comes with a SQL-code-generator is nice, though. But not too exciting. I still hope that the team adjusts their current direction. But more on that in a longer post 🙂

I’ll just start with one of the first samples I found:

Relationships

It comes with four M-Files:

asdf

 

 

 

 

 

So, what again is the difference between one-to-many and many-to-one?

OneToMany.m and ManyToOne.m

Well, this is OneToMany.m:

// original OneToMany.cs
module OneToMany {
    type A {
        Id : Integer32 => AutoNumber();
    } where identity Id;
    
    type B {
         A : A; 
    } where value.A in As;
    
    As : {A*};
    Bs : {B*};  
}

And this is ManyToOne.m:

// original ManyToOne.m
module ManyToOne {
    type A {
         B : B;  
    } where value.B in Bs;
    
    type B {
        Id : Integer32 => AutoNumber();
    } where identity Id;
    
    As : {A*};  
    Bs : {B*};
}

Do you spot the difference? I do! It would be easier to see, if we changed the ordering on the second one. Now compare this to the first one.

// reordered ManyToOne.m
module ManyToOne {
    type B {
        Id : Integer32 => AutoNumber();
    } where identity Id;
    
    type A {
         B : B;  
    } where value.B in Bs;
    
    Bs : {B*};
    As : {A*};
}

Do you see it now? Exactly. The XPath-function translate(‘AB’, ‘BA’) would have done the job! There is exactly NO difference between except for A and B switched around!

Usually the relationship the sample tries to illustrate is called one-to-many, even though in relational databases the reference goes from the many to one.

The funny part is over. Lets look at one-to-one and many-to-many.

OneToOne.m

This is OK. Although I do not like how coupled the code is. If I used the same style to express the same intent in C# people would blame me for violating every single principle we have learned over the past couple of years. I’ll write about what bothers me here in another post sometime.

// original OneToOne.m
module OneToOne {
    type A {
        Id : Integer32 => AutoNumber();
    } where identity Id;

    type B {
        A : A where value in As;
    } where unique A;
    
    As : {A*};
    Bs : {B*};
}

ManyToMany.m

// original ManyToMany.m
module ManyToMany {
    type A {
        Id : Integer32 => AutoNumber();
    } where identity Id;
    
    type B {
        Id : Integer32 => AutoNumber();
    } where identity Id;
    
    As : {A*};
    Bs : {B*};  

    type AB
    {
        ReferencedA : A;
        ReferencedB : B;
    } where value.ReferencedA in As && value.ReferencedB in Bs;
     
    ABs : {AB*};    
}

What is that. Natural modeling? What you really want to say is, that A has a n2m-Relationship with B, right? Now tell me, how this M-code is any better than SQL! It does not raise the level of abstraction, at least! IMHO this is not a solution for something that claims to be a modeling language, it’s a hack.

In“M” when you model something that naturally would be called a hierarchy or containment, the SQL compiler projects it as n2m anyway.

Relationship vs. Reference

Actually M doesn’t really support relationships (or associations) at all today. It just knows about references. What the difference is?

I’m not too sure if I get it right, but I’ll try.

A relationship is always coherent and integer. Something both sides agree on.

holding-hands1[1] 

A reference, though, is just something holding on something else.

detlef_46983[1] @Pitopia, Detlef

In relational databases relationships are modeled using references plus constraints.

So for example saying to the man, that he isn’t allowed to move as long as this baby holds his finger, you would enforce something that could be called an relation.

Summary

I think samples of this quality rather chase away people than buying them into the language. The language team should review all the documentation and samples. They should discuss them and give good guidance.

What I’ve seen so far is rather bad guidance.

Update: Also check out the MSDN Documentation on Relationships. It’s at least better than the samples. (found it after I wrote this post)

Advertisements

One thought on ““M” Samples Review: One-to-Many vs. Many-to-One

  1. Pingback: Krishna Prasad Accot (prasadaccot) « “M” Samples Review: One-to-Many vs. Many-to-One «... « Chat Catcher

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 )

Google+ photo

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

Connecting to %s