The ObjectMapper .NET Project

Official blog of the AdFactum ObjectMapper .NET

Archive for September, 2006

Attribute: [Unique]

Posted by Gerhard Stephan on 20th September 2006

Using the Unique Attribute on a property tells the ObjectMapper .NET to create an unique constraint when exporting the DDL file. The ObjectMapper .NET allows different ways to use the Unique Attribute.

Create a single unique property:

First of all you can create a single unique property. This can be done by attributing the property with the Unique attribute.

            [Unique]

            [PropertyLength(7)]

            public string PersonnelNumber

            {

                  get { return _personnelNumber;}

                  set { _personnelNumber = value;}

            }

The resulting DDL script would look like:

ALTER TABLE EMPLOYEE ADD CONSTRAINT EMPLOYEE_UK01 UNIQUE (PERSONNELNUMBER);

Create unique key groups:

As an other feature, you can group unique keys and bundle them to one unique key index. This is extremly useful because not every property is unique by itself. A unique key group has a number that identifies the group. The number itself is insignificant, it’s only important  that the properties you want to group have the same key group number assigned.

        [PropertyLength(5)]

        [Unique(1)]             // Key Group 1 has been set

        public string Locale

        {

            get { return locale; }

            set { locale = value; }

        }

 

        [PropertyLength(32)]

        [Unique(1)]             // Key Group 1 has been set

        public string Key

        {

            get { return key; }

            set { key = value; }

        }

The resulting DDL script would look like:

ALTER TABLE [TRANSLATION] ADD CONSTRAINT TRANSLATION_UK01 UNIQUE ([LOCALE],[KEY]);

Create unique key groups with sorted keys:

Additional to the unique key group feature you can add an sorting to the key group. Look at our example above. Most times it is necessary to store the properties in a defined sorting. E.g. You always need it if you want to build business logic driven foreign keys that rely on a combined unique key index.

In that case, the second number, after the key group defines the sorting of the keys. Imagine you want to change the ordering of the keys in our example above. Than it would look like:

        [PropertyLength(5)]

        [Unique(1,2)]             // Key Group 1 has been set

        public string Locale

        {

            get { return locale; }

            set { locale = value; }

        }

 

        [PropertyLength(32)]

        [Unique(1,1)]             // Key Group 1 has been set

        public string Key

        {

            get { return key; }

            set { key = value; }

        }

 

The resulting DDL script would look like:

ALTER TABLE [TRANSLATION] ADD CONSTRAINT TRANSLATION_UK01 UNIQUE ([KEY], [LOCALE]);

You see that the sorting of the columns within the unique key index changed.

Summary:

Some special things, like function based unique key constraints, should be better left to a seasoned database admin. Additionally such things are not easy to maintain by source code. But I think that the unique key attribute offers you a lot possibilities to cover more than 90% of all use cases, for which you might need unique key constraints.

Cheers
Gerhard

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Posted in Attributes | No Comments »

New Release – ObjectMapper .NET 1.3.814.0

Posted by Gerhard Stephan on 14th September 2006

The new release covers a lot of new functionality to fit the growing needs of our customers. First of all I changed the connection string used with the SQL Server. That improvement has been suggested by Luis Vargas (see Forum entry).

The next improvement has been made when working with virtual links. Virtual links can now join values from native SQL selects. Thus enables you to bind fields to views or sub selections.

[VirtualLink("SELECT * FROM PRODUCT", typeof(Product), "ProductName", "ProductKey", "ProductKey", "ValidUntil", "@VALIDATION_DATE")]

 

The next big change has been made for unique key fields. Unique keys can now grouped and ordered to build unique key groups. This feature enables you to build combined unique keys.

 

        [PropertyLength(5)]

        [Unique(1)]             // Key Group 1 has been set

        public string Locale

        {

            get { return locale; }

            set { locale = value; }

        }

 

        [PropertyLength(32)]

        [Unique(1)]             // Key Group 1 has been set

        public string Key

        {

            get { return key; }

            set { key = value; }

        }

 

 

As a further change I added business driven foreign keys. Those foreign keys are not compareable with technical foreign keys which can be extracted from the object relations. Business driven foreign keys rely on business logic and business keys (e.g. the combined Unique Keys).

 

    /// <summary>

    /// This class offers translations for a region within a country.

    /// Therefore we must ensure that the country exists in database.

    /// </summary>

    [Table("REGIONS")]

    [ForeignKey("CountryKey", "Locale", typeof(Country), "Key", "Locale")]

    public class CountryRegion : Translation

    {

        private string countryKey;

 

        /// <summary>

        /// Gets or sets the country key.

        /// </summary>

        /// <value>The country key.</value>

        [PropertyLength(32)]

        public string CountryKey

        {

            get { return countryKey; }

            set { countryKey = value; }

        }

 

 

Also a new feature suggested by a customer (see Forum entry) is the possibility to serialize and export the meta data for properties. Therefore I implemented a new class PropertyMetaInfo that holds all informations for a property that can be set by the attributes of the ObjectMapper .NET (e.g. Property Length, Uniqueness etc.). The Meta Informations of a class can be retrieved by calling the ReflectionHelper GetPropertyMetaInfo. The result of this method is serializeable and therefore can used with WebServices to offer the client the possibility to access the meta informations.

 

PropertyMetaInfo[] metaInfos = ReflectionHelper.GetPropertyMetaInfos(typeof(Employee));   

 

Even I solved some more smaller issues, that are the biggest changes in this release.

Hope that you enjoy the new features

 

Cheers

Gerhard Stephan 

 

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Posted in Releases | 1 Comment »

Attribute: [Required]

Posted by Gerhard Stephan on 7th September 2006

You can use the Required attribute to force that the property must be filled. Within the DDL creation process the ObjectMapper .NET adds a "NOT NULL" to the column definition.

This can be used to create mandatory fields that are checked by database. If you try to store an empty property that is marked as required, the database will throw an exception.

Here’s an example how to mark a property as required.

            [Required]

            public string LastName

            {

                  get { return _lastName; }

                  set { _lastName = value; }

            }

 

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Posted in Attributes | No Comments »