AdFactum ObjectMapper .NET Blog

Official blog of the AdFactum ObjectMapper .NET

A final performance boost

Posted by Gerhard Stephan on August 17th, 2006

The following article is obsolete!
The ICreateObject interface is now part of the AdFactum ObjectMapper .NET 1.90.1917 and it’s supported by the new UniversalFactory class. So you don’t have to implement a new factory to get this performance boost.

If you need a final performance boost in your application you have to implement a HashFactory in order to create your entities.

As you might know the ObjectMapper .NET allows you to setup your own factory in order to create the business entities loaded by the ObjectMapper .NET. This circumstance can be used to implement an algorithm that is based on the factory pattern. The creation of a new object using the new method is much more faster than it’s pendant using the Activator object.

Therefore all objects must implement a new interface:

    public interface ICreateObject

    {

        /// <summary>

        /// Returns a new copy of the object type

        /// </summary>

        /// <returns></returns>

        IValueObject CreateNewObject();

    }

This may look like that:

    class Contact : ValueObject, ICreateObject

    {

       

       

 

        public IValueObject CreateNewObject()

        {

            return new Contact();

        }

    }

 

 Now the most exciting part of all. The Factory:

    class HashFactory : IObjectFactory

    {

        /// <summary>

        /// Hashtable that contains the list of template objects

        /// </summary>

        static Dictionary<Type, IFactoryMethod> voHash = new Dictionary<Type, IFactoryMethod>();

       

        /// <summary>

        /// Method to create an object from a type name

        /// </summary>

        /// <param name="typeName"></param>

        /// <returns></returns>

        public object Create(string typeName)

        {

            return Create(Type.GetType(typeName));

        }

 

        /// <summary>

        /// Method to create an object from a type

        /// </summary>

        /// <param name="type"></param>

        /// <returns></returns>

        public object Create(Type type)

        {

            IFactoryMethod voTemplate;

            voHash.TryGetValue(type, out voTemplate);

 

            IValueObject result;

            if (voTemplate == null)

            {

                result = Activator.CreateInstance(type) as IValueObject;

 

                voTemplate = result as ICreateObject;

                if (voTemplate != null)

                {

                    voHash.Add(type, voTemplate);

                    result = voTemplate.CreateNewObject();

                }

            }

            else

                result = voTemplate.CreateNewObject();

           

            return result;

        }

    }

Last but not least, that factory can be used to create the ObjectMapper .NET.

           ObjectMapper mapper = new ObjectMapper(new HashFactory(), persister, Transactions.Manual);

 

And now – boost your application !

Cheers
Gerhard

 

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>