AdFactum ObjectMapper .NET Blog

Official blog of the AdFactum ObjectMapper .NET

Persistent Domain Object Pattern meets the ObjectMapper .NET

Posted by Gerhard Stephan on June 23rd, 2006

If you are using the ObjectMapper .NET your entities will at least need to implement the IValueObject interface. Furthermore you have to add some attributes (e.g. to specify the column length) to the properties of your business entities.

All these things are dependencies and due to this you’ll need to reference the ObjectMapper .NET assembly wherever you access the business entities.

So it would be nice if we could implement a loose coupling between the implementation details of the business entities and the UI.

The “Persistent Domain Object Pattern” – short named as PDO-Pattern – caters for that issue. It splits the business entities to an interface that is usually used by the UI and the concrete implementation for the persistent layer.

The UI only references the interfaces of the business entities. The implementation will be covered through the business components which are also using the interfaces for object exchange.

Component Diagram

For this Blog I implemented an example that shows how to separate the business entities and the user interface. Imagine you want to build an application that manages a company with one contact. Ok – not a real world example, but good enough to show how to implement the pattern.

PDO Pattern Class Diagram

As you can see the concrete implementation of the business entities implements the interfaces ICompany and IContact. Furthermore the business entities are derived from ValueObject which is important for the ObjectMapper .NET because of the object mapping.

      /// <summary>

      /// Implementation for the company

      /// </summary>

      [Table("Company")]

      public class CompanyImpl : ValueObject, ICompany

      {

            private string companyName;

            private string street;

            private string plz;

            private string city;

            private IContact companyContact;

           

           

If you are working with interfaces in a business entity you have to tell the ObjectMapper .NET the concrete implementation of the interface due to the object mapping.

            /// <summary>

            /// Gets or sets the company contact.

            /// </summary>

            /// <value>The company contact.</value>

            [Ignore]

            public IContact CompanyContact

            {

                  get { return companyContact; }

                  set { companyContact = value; }

            }

 

            /// <summary>

            /// Gets or sets the company contact used by the ObjectMapper .NET

            /// </summary>

            /// <value>The company contact.</value>

            [PropertyName("CompanyContact")]

            public ContactImpl CompanyContactImpl

            {

                  get { return companyContact as ContactImpl; }

                  set { companyContact = value; }

            }

 

As you can see the interface is tagged with an [Ignore] Attribute in order to tell the mapper that this interface should be ignored for mapping. Instead of an interface mapping we implemented a Getter / Setter to the real aggregated object. This method is not visible to the UI but used by the ObjectMapper .NET to store the Contact entity.

Last but not least the UI talks to the business components in order to receive and store the data.

      public class CompanyService 

      {

            public static ICompany CreateNewCompany ()

            { … }

                 

            public static void SaveCompany (ICompany company)

            { … }

 

            public static IList GetAllCompanies ()

            { … }

      }

 

And the UI only needs two references to external assemblies, the BusinessComponents and the Business Entities. There’s no need for adding a reference neither to the ObjectMapper nor to the concrete implementation of the Business Entities.

The complete example can be downloaded using the following link:

Persistent Domain Object Pattern meets the ObjectMapper .NET

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>