ORM uses Reflection to create instances of your Entity classes, and Reflection tends to be slow.  What is especially slow is filling Property values by name.  In an effort to reduce this overhead, ORM allows you to provide an optional static Entity generation proxy that significantly reduces the time required to build Entity instances.

Simply add a static method called `ORM_CreateProxy` to your Entity Class that returns an instance given a FieldAttributeCollectioin and an IDataReader.  To help keep your object model clean, this can be a private method. Here's a simple example:

    [Entity(KeyScheme.Identity)]
    public class Author
    {
        private static Author ORM_CreateProxy(FieldAttributeCollection fields, IDataReader results)
        {
            var item = new Author();

            foreach (var field in fields)
            {
                var value = results[field.Ordinal];

                switch (field.FieldName)
                {
                    case "Name":
                        item.Name = value == DBNull.Value ? null : (string)value;
                        break;
                    // fill in any additional properties here
                }
            }

            return item;
        }

        [Field(IsPrimaryKey = true)]
        public int AuthorID { get; set; }

        [Field(SearchOrder = FieldSearchOrder.Ascending)]
        public string Name { get; set; }
    }

Last edited May 29, 2013 at 5:16 PM by ctacke, version 1

Comments

No comments yet.