Blog de Francisco Velázquez

otro blog personal

Archive for December 2010

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 , , ,

Pequeñas utilidades para Windows

leave a comment »

Written by fravelgue

December 9, 2010 at 8:09 pm

Posted in util