Example #1
0
        /// <summary>
        /// Checks the type atrribute.
        /// </summary>
        /// <param name="type">The type.</param>
        public static void CheckTypeAtrribute(Type type)
        {
            ActiveRecordAttribute attribute = ActiveRecordMaster.GetAttribute(type);
            PrimaryKeyAttribute   pk        = ActiveRecordMaster.GetPrimaryKey(type);

            CheckTypeAtrribute(type, ref attribute, ref pk);
        }
Example #2
0
        /// <summary>
        /// Lazy load a property (HasMany).
        /// </summary>
        /// <typeparam name="TPropertyClass">The type of the property class.</typeparam>
        /// <param name="attr">The attr.</param>
        /// <returns></returns>
        /// <exception cref="AttributeNotFoundException"></exception>
        private List <TPropertyClass> LazyLoadMany <TPropertyClass>(HasManyAttribute attr)
            where TPropertyClass : ActiveRecordBase
        {
            ActiveRecordAttribute attribute = ActiveRecordMaster.GetAttribute(this.GetType());

            if (attribute == null)
            {
                throw new AttributeNotFoundException();
            }

            try
            {
                PrimaryKeyAttribute pk = ActiveRecordMaster.GetPrimaryKey(this.GetType());

                if (pk == null)
                {
                    return(null);
                }

                Criteria criteria = Criteria.For <TPropertyClass>();
                criteria.SQLString = ActiveRecordMaster.MakeHasManySelect(this.GetType(), attr.CurrentPropertyInfo.Name);

                criteria.AddParameter("@CODE", pk.CurrentPropertyInfo.Name, pk.CurrentPropertyInfo.GetValue(this, null));

                return(ActiveRecordMaster.ExecuteQueryCriteria <TPropertyClass>(criteria));
            }
            catch (Exception ex)
            {
                LogManager.Log("Monty.ActiveRecord", LogCategory.Error, String.Format("Error when tries to execute ActiveRecordBase.FindByForeignKey(attr: [{0}])", attr), ex);
                return(null);
            }
        }
Example #3
0
        /// <summary>
        /// Finds an item by primary key.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static ActiveRecordBase Find(Type type, object id)
        {
            CheckTypeAtrribute(type);

            try
            {
                PrimaryKeyAttribute pk = ActiveRecordMaster.GetPrimaryKey(type);

                if (pk == null)
                {
                    return(null);
                }

                Criteria criteria = Criteria.For(type);
                criteria.SQLString = ActiveRecordMaster.MakePrimaryKeySelect(type);

                criteria.AddParameter("@CODE", pk.CurrentPropertyInfo.Name, id);

                List <ActiveRecordBase> list = ActiveRecordMaster.ExecuteQueryCriteria(type, criteria);

                if (list != null && list.Count > 0)
                {
                    return(list[0]);
                }

                return(null);
            }
            catch (Exception ex)
            {
                LogManager.Log("Monty.ActiveRecord", LogCategory.Error, String.Format("Error when tries to execute ActiveRecordBase.FindAll(type: [{0}])", type), ex);
                return(null);
            }
        }
Example #4
0
        /// <summary>
        /// Sets the id.
        /// </summary>
        /// <param name="id">The id.</param>
        public void SetId(object id)
        {
            PrimaryKeyAttribute pk = ActiveRecordMaster.GetPrimaryKey(this.GetType());

            if (pk == null)
            {
                return;
            }

            pk.SetValue(this, id);
        }
Example #5
0
        /// <summary>
        /// Check if the criteria return results.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static bool Exists(Type type, object id)
        {
            PrimaryKeyAttribute pk = ActiveRecordMaster.GetPrimaryKey(type);

            Criteria criteria = Criteria.For(type);

            criteria
            .AddFilter(criteria.Eq(pk.CurrentPropertyInfo.Name, id));

            return(ActiveRecordBase.Exists(type, criteria));
        }
Example #6
0
        /// <summary>
        /// Gets the id.
        /// </summary>
        /// <returns></returns>
        public object GetId()
        {
            PrimaryKeyAttribute pk = ActiveRecordMaster.GetPrimaryKey(this.GetType());

            if (pk == null)
            {
                return(null);
            }

            return(pk.GetValue(this));
        }