Blog de Francisco Velázquez

otro blog personal

Abstrayendo operaciones básicas en business objects

with one comment

Tras leer el buen artículo de Matt Long, se me ocurrió que tal podría aplicar estos principios para facilitar mi trabajo con Gentle.NET (increíble pero cierto) y tal vez que fuera el origen de un cambio en el motor de persistencia.

Así podríamos tener una clase base para todos nuestros objetos de negocio.


public class BusinessObject<T> : Persistent
        where T : class
    {
        static protected PersistenceBroker persistenceBroker;
        protected Type _type;
        static protected BusinessObject<T> _objToRetrieve = null;
        protected bool _empty = true;
        protected bool _changed = false;

        
        [TableColumn("id", NotNull = true), PrimaryKey(AutoGenerated = false)]
        public int Id { get; set; }

        public bool Changed
        { 
            get { return _changed; } 
        }

        static BusinessObject()
        {
            // TODO Singleton
            if (_objToRetrieve == null) _objToRetrieve = new BusinessObject<T>();
            if (persistenceBroker == null) 
                    persistenceBroker = new PersistenceBroker(typeof(T));
        }

        static public T Retrieve(int id)
        {
            return _objToRetrieve.retrieve(id);         
        }

        protected T retrieve(int id)
        {
            Key key = new Key(typeof(T), true, "Id", id);
            return ((PersistenceBroker)persistenceBroker).RetrieveInstance(typeof(T), key) as T;
        }

        public override void Persist()
        {
            if (Changed || !IsPersisted)
            {
                base.Persist();
                _changed = false;
            }
        }

        public override void Remove()
        {
            base.Remove();            
        }

        // TODO FindBy

        public override bool Equals(object obj)
        {
            bool result = false;
            if (obj != null)
                result = ((this.GetType() == obj.GetType()) 
                    && (((BusinessObject<T>)this).Id == ((BusinessObject<T>)obj).Id));
            return result;
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
    }

[Serializable]
    [TableName("Example")]
    class Exmple : BusinessObject<Example>
    {
        [TableColumn("name", NotNull = false)]
        public string Name { get; set; }

        [TableColumn("description", NotNull = false)]
        public string Description { get; set; }

        [TableColumn("date", NotNull = false)]
        public DateTime Date { get; set; }

        [TableColumn("error", NotNull = false)]
        public bool Error { get; set; }
    }

Written by fravelgue

December 11, 2010 at 12:41 pm

Posted in .net

Tagged with , , ,

One Response

Subscribe to comments with RSS.

  1. […] que los micro-ORMs están de moda (Massive, Dapper, PetaPOCO, SimpleData, etc). Basándome en esta abstracción y en mi querido Gentle.NET, me he decido a construir unos DAOs sobre Dapper, que es usado en […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: