The XML schema’s for DLinq

Tonight I browsed some more through the documentation of DLinq (I’ll refer to Linq to SQL this way, because it is shorter and until the namespace is changed from System.Data.DLinq). In particular I looked at the document ‘DLinq Overview for C# Developers’.

There is a section called ‘Generator Tool XML Reference’, that lists a prototypical example of the XML syntax used for the mapping without attributes. But actually, there are two types of XML around:

  1. SQL metadata for a database
    Generated by running /xml on SqlMetal
    Used for code and mapping file generation
  2. External mapping files
    Generated by running /map on SqlMetal (fragment listed below)

<?xml version=1.0encoding=utf-8?>
<
Database xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexmlns:xsd=http://www.w3.org/2001/XMLSchemaName=Northwind
>
  <
Table Name=Orders
>
    <
Type Name=.Orders
>
      <
Column Name=OrderIDMember=OrderIDStorage=_OrderIDDbType=Int NOT NULL IDENTITYIsIdentity=TrueIsAutoGen=True
/>
      <
Column Name=CustomerIDMember=CustomerIDStorage=_CustomerIDDbType=NChar(5)
/>
      <
Column Name=EmployeeIDMember=EmployeeIDStorage=_EmployeeIDDbType=Int
/>
      …

      <Association Name=FK_Order_Details_OrdersMember=OrderDetailsStorage=_OrderDetailsThisKey=OrderIDOtherTable=Order DetailsOtherKey=OrderID
/>
    </Type
>
</
Table>

You can use the SqlMetal tool to give you a headstart on creating the domain objects. You may find yourself deleting some of the classes, Table and EntityRef definitions that you do not want, from the generated code. But, once you start down that path, it is pretty hard to regenerate the code, without having to go through the process of deleting and additing again.

So here’s a tip for you, so you might delay the manual editing of the generated code just a little longer. You first create a dump of a database’s schema, like so:

SqlMetal.exe /server:. /database:Northwind /xml:NorthwindSchema.xml

After that you can add Hidden and Access attributes to all elements of the XML file. All elements share these two attributes (and Name, btw), which are used during code generation. Hidden is used to determine whether the corresponding code fragment is emitted. E.g. an Association element becomes an EntitySet. The Access attribute determines the access modifier of the code fragment or type, which can be public, internal or even protected and private for situations where the modifier is allowed that value. It is still editing the generated XML, but at least not the generated code.

<Table Name=Products>
  <
Column Name=ProductIDType=System.Int32DbType=Int NOT NULL IDENTITYIsIdentity=trueIsAutoGen=trueIsReadOnly=falsePrecision=10Scale=255
/>
  <
Column Name=ProductNameType=System.StringDbType=NVarChar(40) NOT NULLNullable=falseStringLength=40
/>
  <
Column Access=private Name=SupplierIDType=System.Int32DbType=IntNullable=truePrecision=10Scale=255
/>
  <
Column Access=private Name=CategoryIDType=System.Int32DbType=IntNullable=truePrecision=10Scale=255
/>
  …
  <
Association Hidden=true Name=FK_Order_Details_ProductsProperty=OrderDetailsKind=ManyToOneChildTarget=Order Details
>
    <
Column Name=ProductID
/>
  </
Association
>
  <
Association Name=FK_Products_CategoriesProperty=CategoriesKind=ManyToOneParentTarget=Categories
>
    <
Column Name=CategoryID
/>
  </
Association>

Then run the next command:

SqlMetal.exe /code:NorthwindDomain.cs NorthwindSchema.xml /pluralize /namespace:Northwind.Domain

and you will notice that the EntityRef<OrderDetails> does no longer exist and that the properties for SupplierID and CategoryID are private:

[Column(Storage=“_CategoryID”, DBType=“Int”)]
private System.Nullable<int> CategoryID {
  get {
    return this._CategoryID;
  }
  set {
    if ((this._CategoryID != value)) {
      this.OnPropertyChanging(“CategoryID”);
      this._CategoryID = value;
      this.OnPropertyChanged(“CategoryID”);
    }
  }
}

In some other blog entry I will write about why you might want to hide certain properties from the public interface of your objects.

Anyway, surely a nice visual tool will become available for Visual Studio to generate and produce the schema XML and edit it. You can use the DLinq Designer right now, but it does not use the schema file as the basis for code generation. You should give it a try, though.

Dlinqdesigner

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

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 )

Google photo

You are commenting using your Google 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