AdFactum ObjectMapper .NET Blog

Official blog of the AdFactum ObjectMapper .NET

Archive for the 'Attributes' Category

Attribute: [SelectFunction]

Posted by Gerhard Stephan on 21st March 2007

The SelectFunction attribute is used to define a database function that is called when selecting the object from database. This function might be used to get the last access date of an object in database. Have a look at the following example.

        /// <summary>

        /// Gets or sets the last read.

        /// </summary>

        /// <value>The last read.</value>

        [SelectFunction("GETDATE()")]

        public DateTime LastRead

        {

            get { return lastRead; }

            set { lastRead = value; }

        }

The created SQL looks like:

SELECT [DATABASEFUNCTION].* , GETDATE() as LASTREAD FROM [DATABASEFUNCTION]  WHERE [DATABASEFUNCTION].[ID]=’12fca955-12ba-4486-bf89-4bf92d6db13d’;

Posted in Attributes | No Comments »

Attribute: [UpdateFunction]

Posted by Gerhard Stephan on 21st March 2007

The UpdateFunction attribute is used to define a database function that is called when updating the object in database. This function might be used to store the lastupdate of an object to database. Have a look at the following example.

        /// <summary>

        /// Gets or sets the last updated.

        /// </summary>

        /// <value>The last updated.</value>

        [UpdateFunction("GETDATE()")]

        public DateTime LastUpdated

        {

            get { return lastUpdated; }

            set { lastUpdated = value; }

        }

 

The created SQL looks like:

UPDATE DATABASEFUNCTION SET [LASTUPDATED] = GETDATE() WHERE [DATABASEFUNCTION].[ID]=’12fca955-12ba-4486-bf89-4bf92d6db13d’;

Posted in Attributes | No Comments »

Attribute: [InsertFunction]

Posted by Gerhard Stephan on 21st March 2007

The InsertFunction attribute is used to define a database function that is called when inserting the object into the database. This function might be used to store the creation date of an object to database. Have a look at the following example.

        /// <summary>

        /// Gets or sets the creation.

        /// </summary>

        /// <value>The creation.</value>

        [InsertFunction("GETDATE()")]

        public DateTime Creation

        {

            get { return creation; }

            set { creation = value; }

        }

The created SQL looks like:

INSERT INTO [DATABASEFUNCTION] ( [CREATION], [ID]) VALUES ( GETDATE(), ’12fca955-12ba-4486-bf89-4bf92d6db13d’);

Posted in Attributes | No Comments »

Attribute: [BindPropertyTo]

Posted by Gerhard Stephan on 19th December 2006

The attribute BindPropertyTo is used to bind single properties or a collection to a specifc data type. This property is always required if you want to map a property with a not specified data type – like a property that returns an interface, an abstract class or a untyped collection.

Using the [BindPropertyTo] for that cases offers you a better performance, because the ObjectMapper .NET knows the target data type for the property and does not need to evaluate it dynamically.

Examples for using the BindPropertyTo attribute are:

        private IList contacts = new ArrayList();

 

        [BindPropertyTo(typeof(Contact))]

        public IList Contacts

        {

            get { return contacts; }

            set { contacts = value; }

        }

or

        private AbstractContact contact = new Contact();

 

        [BindPropertyTo(typeof(Contact))]

        public AbstractContact Contact

        {

            get { return contact; }

            set { contact = value; }

        }

or

        private IContact contact = new Contact();

 

        [BindPropertyTo(typeof(Contact))]

        public IContact Contact

        {

            get { return contact; }

            set { contact = value; }

        }

Additionally you will find useful informations in blog entry How to do bind collections.

Posted in Attributes | No Comments »

Attribute: [WeakReferenced]

Posted by Gerhard Stephan on 20th November 2006

The WeakReferenced Abbtribute forces the mapper to use outer joins when joining parent and child table using Virtual Links.

This attribute is always useful if the child property is not a mandatory property of the parent class.

Think about a contact object that has a mandatory firstname and lastname and a salutation enumeration. The salutation enumeration is not mandatory.

    /// <summary>

    /// Descripes a contact

    /// </summary>

    [Table("CONTACTS")]

    public class Contact : ValueObject

    {

        private Salutation salutation;

        private string salutationText;  // get text from db using a virtual link

 

        private string firstName;

        private string lastName;

 

        /// <summary>

        /// Gets or sets the salutation.

        /// </summary>

        /// <value>The salutation.</value>

        public Salutation Salutation

        {

            get { return salutation; }

            set { salutation = value; }

        }

 

        /// <summary>

        /// Gets or sets the salutation text.

        /// </summary>

        /// <value>The salutation text.</value>

        [VirtualLink(typeof(SalutationText), "Value", "Key", "Salutation")]

        public string SalutationText

        {

            get { return salutationText; }

            set { salutationText = value; }

        }

 

        /// <summary>

        /// Gets or sets the name of the first.

        /// </summary>

        /// <value>The name of the first.</value>

        [Required]

        [PropertyLength(50)]

        public string FirstName

        {

            get { return firstName; }

            set { firstName = value; }

        }

 

        /// <summary>

        /// Gets or sets the name of the last.

        /// </summary>

        /// <value>The name of the last.</value>

        [Required]

        [PropertyLength(50)]

        public string LastName

        {

            get { return lastName; }

            set { lastName = value; }

        }

    }

 

The salutation enumeration looks like this:

    /// <summary>

    /// Salutation

    /// </summary>

    public enum Salutation

    {

        None,

       

        DearMr,

        DearMrs,

        MyFriend

    }

 

And last but not least, the text corresponding to that enumeration will be retrieved by a virtual link using database joins.

 

    /// <summary>

    /// Static texts for salutation

    /// </summary>

    [StaticData]

    [WeakReferenced]

    public class SalutationText : ValueObject

    {

        private Salutation  key;

        private string      value;

 

        public Salutation Key

        {

            get { return key; }

            set { key = value; }

        }

 

        public string Value

        {

            get { return value; }

            set { this.value = value; }

        }

    }

 

When selecting the contact the SQL looks like that:

SELECT contacts.*, v1.VALUE AS salutationtext

  FROM contacts LEFT OUTER JOIN salutationtext v1 ON contacts.salutation = v1.KEY

 

If you wouldn’t use the attribute [WeakReferenced] a contact without a filled salutation could not be loaded. 
Because of that issue the attribute is very important.

Posted in Attributes | 1 Comment »

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

Posted in Attributes | No Comments »

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; }

            }

 

Posted in Attributes | No Comments »

Attribute: [StaticData]

Posted by Gerhard Stephan on 7th August 2006

Using the StaticData attribute you can tell the ObjectMapper .NET that it’s your intentation that the class only contains static data.

What changes if the ObjectMapper .NET knows that this class shall only contain static data? If you’re calling the DeleteRecursive method, the child objects won’t be deleted, because the class is marked as Static.

So the attribute prevents the deletion of objects that contains static data.

Here’s the example code:

    [Table("PRICES")]

    [StaticData]

    class Price : ValueObject

 

 

 

Posted in Attributes | No Comments »

Attribute: [Table]

Posted by Gerhard Stephan on 13th July 2006

The table attribute is used to rename the mapping from class to database table.

      [Table („CONTACTS“)]

      public class Contact : ValueObject

 

The ObjectMapper .NET does not need that property to map the class to a database table. If the attribute misses, the class name itself will be taken as the valid name for the database table.

But most times it makes sense to rename the mapping of the class to the underlaying database table. Imagine your class is named "Contact", so you would instinctively name your database table "Contacts". That is because the database table contains the plural of your entities, but the class itself is defined as a singular entity.

 

Posted in Attributes | No Comments »

Attribute: [PropertyLength]

Posted by Gerhard Stephan on 11th July 2006

The PropertyLength attribute is used to specify the length of a database column.

Setting this attribute has no effect on how the ObjectMapper .NET will handle this property, but it defines the length of a database column and therefore it affects the DDL creation.

Look at this example:

            [PropertyLength(50)]

            public string FirstName

            {

                  get { return _firstName; }

                  set { _firstName = value; }

            }

 

Furthermore it has an effect on which data type the database column will have. Dependent on the database system you’re using, strings can only be stored up to a specific length of e.g. Oracle 4000 characters. If you want to store longer strings you have to choose other data types that can handle unlimited string size, e.g. CLOBS in Oracle or MEMO fields for Micorosoft Access.

To force the ObjectMapper .NET to use such data types, you have to set the attribute PropertyLength to int.MaxValue.

            [PropertyLength(int.MaxValue)]

            public string Text

            {

                  get { return text; }

                  set { text = value; }

            }

 

This example will force the ObjectMapper .NET to use data types like CLOB (Oracle), MEMO (Microsoft Access) or TEXT (Microsoft SQL Server).

 

Posted in Attributes | 1 Comment »