Example #1
0
 /// <summary>
 /// Listen to entity change using a custom listener implementation.
 /// </summary>
 public static void Listen(IEntity entity, EntityIdListener listener)
 {
     // If id is already set, take a shortcut and assign directly
     if (entity.Id > 0)
     {
         listener.AssignId(entity.Id);
     }
     else
     {
         entity.IdChanged += listener.IdChanged;
     }
 }
Example #2
0
        /// <summary>
        /// Get or create an entity for a business object
        /// </summary>
        /// <param name="unitOfWork">An open database unit of work</param>
        /// <param name="obj">The business object</param>
        /// <typeparam name="TEntity">The entity type to use</typeparam>
        public static TEntity GetEntity <TEntity>(this IUnitOfWork unitOfWork, IPersistentObject obj)
            where TEntity : class, IEntity
        {
            var repository = unitOfWork.GetRepository <IRepository <TEntity> >();
            var entity     = repository.GetByKey(obj.Id);

            if (entity == null)
            {
                entity = repository.Create();
                EntityIdListener.Listen(entity, obj);
            }

            return(entity);
        }
        /// <summary>
        /// Get or create an entity for a business object
        /// </summary>
        /// <param name="dbSet">An open database set</param>
        /// <param name="obj">The business object</param>
        /// <typeparam name="TEntity">The entity type to use</typeparam>
        public static TEntity GetEntity <TEntity>(this IDbSet <TEntity> dbSet, IPersistentObject obj)
            where TEntity : class, IEntity
        {
            var entity = dbSet.FirstOrDefault(e => e.Id == obj.Id);

            if (entity != null)
            {
                return(entity);
            }

            entity = dbSet.Create();
            dbSet.Add(entity);
            EntityIdListener.Listen(entity, obj);

            return(entity);
        }