示例#1
0
        /// <summary>
        /// Load object of type T from database.
        /// </summary>
        /// <typeparam name="T">Type of object to return.</typeparam>
        /// <param name="Id">unique identifier or primary key value</param>
        /// <returns>Object of type T</returns>
        protected virtual T GetObjectByID <T>(int Id)
        {
            Type objectType = typeof(T);

            object[] objectBindingattribs = objectType.GetCustomAttributes(typeof(ObjectBindingAttribute), true);
            if (objectBindingattribs == null)
            {
                throw new ApplicationException("ObjectBindingAttribute is not defined for type " + objectType.Name + ".");
            }

            ObjectBindingAttribute objectBindingAttrib = objectBindingattribs[0] as ObjectBindingAttribute;

            if (objectBindingAttrib == null)
            {
                throw new ApplicationException("ObjectBindingAttribute cast error for type " + objectType.Name + ".");
            }

            T obj = default(T);

            try
            {
                using (MySqlConnection con = this.OpenConnection())
                {
                    MySqlCommand command = new MySqlCommand(objectBindingAttrib.SelectByIDStoredProcedure, con);
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Parameters.Add(new MySqlParameter("@pID", MySqlDbType.Int32)).Value = Id;
                    MySqlDataReader dr = command.ExecuteReader(System.Data.CommandBehavior.SingleRow);

                    if (dr == null)
                    {
                        return(obj);
                    }
                    dr.Read();
                    obj = ObjectFactory.CreateObject <T>(dr);

                    dr.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Create("Greska prilikom pozivanja GetObjectAll<T>() metode!", LogEntryLevel.Critical, ex);
            }

            return(obj);
        }
示例#2
0
        /// <summary>
        /// Load objects of type T using reflection.
        /// </summary>
        /// <typeparam name="T">Type of objects to return.</typeparam>
        /// <returns>List of objects of type T</returns>
        protected virtual List <T> GetObjectAll <T>()
        {
            Type objectType = typeof(T);

            object[] objectBindingattribs = objectType.GetCustomAttributes(typeof(ObjectBindingAttribute), true);
            if (objectBindingattribs == null)
            {
                throw new ApplicationException("ObjectBindingAttribute is not defined for type " + objectType.Name + ".");
            }

            ObjectBindingAttribute objectBindingAttrib = objectBindingattribs[0] as ObjectBindingAttribute;

            if (objectBindingAttrib == null)
            {
                throw new ApplicationException("ObjectBindingAttribute cast error for type " + objectType.Name + ".");
            }

            List <T> objList = new List <T>();

            try
            {
                using (MySqlConnection con = this.OpenConnection())
                {
                    MySqlCommand command = new MySqlCommand(objectBindingAttrib.SelectAllStoredProcedure, con);
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    MySqlDataReader dr = command.ExecuteReader();

                    if (dr == null)
                    {
                        return(null);
                    }
                    while (dr.Read())
                    {
                        objList.Add(ObjectFactory.CreateObject <T>(dr));
                    }
                    dr.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Create("Greska prilikom pozivanja GetObjectAll<T>() metode!", LogEntryLevel.Critical, ex);
            }

            return(objList);
        }
示例#3
0
        public virtual void Delete()
        {
            Type objectType = typeof(T);

            object[] objectBindingattribs = objectType.GetCustomAttributes(typeof(ObjectBindingAttribute), true);
            if (objectBindingattribs == null)
            {
                return;
            }

            ObjectBindingAttribute objectBindingAttrib = (ObjectBindingAttribute)objectBindingattribs[0];

            MySqlCommand updateCommand = PrepeareCommand(objectBindingAttrib.DeleteStoredProcedure);

            updateCommand.Connection.Open();
            updateCommand.ExecuteNonQuery();
            updateCommand.Connection.Close();
        }
示例#4
0
        public virtual void Save()
        {
            Type objectType = typeof(T);

            object[] objectBindingattribs = objectType.GetCustomAttributes(typeof(ObjectBindingAttribute), true);
            if (objectBindingattribs == null)
            {
                return;
            }

            ObjectBindingAttribute objectBindingAttrib = (ObjectBindingAttribute)objectBindingattribs[0];

            try
            {
                if (this.State == ObjectState.Existing)
                {
                    MySqlCommand updateCommand = PrepeareCommand(objectBindingAttrib.UpdateStoredProcedure);

                    //if (updateCommand.Parameters.Contains("@pVrijemeModifikacije"))
                    //    updateCommand.Parameters["@pVrijemeModifikacije"].Value = DateTime.Now;

                    updateCommand.Connection.Open();
                    updateCommand.ExecuteNonQuery();
                    if ((int)updateCommand.Parameters["@pError"].Value == -1)
                    {
                        throw new SaveFailedException(String.Format("Someone has changed data already."));
                    }

                    if (updateCommand.Parameters.Contains("@pVrijemeModifikacije"))
                    {
                        this.VrijemeModificiranja = DateTime.Parse(updateCommand.Parameters["@pVrijemeModifikacije"].Value.ToString());
                    }

                    updateCommand.Connection.Close();
                }
                else if (this.State == ObjectState.New)
                {
                    MySqlCommand insertCommand = PrepeareCommand(objectBindingAttrib.InsertStoredProcedure);

                    insertCommand.Connection.Open();
                    insertCommand.ExecuteNonQuery();
                    insertCommand.Connection.Close();

                    PropertyInfo prop         = objectType.GetProperty("ID");
                    object[]     fieldAttribs = prop.GetCustomAttributes(typeof(ObjectPropertyAttribute), true);
                    if (fieldAttribs == null || fieldAttribs.Length == 0)
                    {
                        throw new ApplicationException("Field binding attribute on " + prop.Name + " is missing.");
                    }

                    ObjectPropertyAttribute fieldAttrib = (ObjectPropertyAttribute)fieldAttribs[0];

                    _id = (int)insertCommand.Parameters["@" + fieldAttrib.ParameterName].Value;
                    if (insertCommand.Parameters.Contains("@pVrijemeKreiranja"))
                    {
                        this.VrijemeKreiranja = DateTime.Parse(insertCommand.Parameters["@pVrijemeKreiranja"].Value.ToString());
                    }

                    this.State = ObjectState.Existing;
                }
            }
            catch (System.Exception ex)
            {
                Logging.Log.Create("Error in the ObjectBase Save method.", Logging.LogEntryLevel.Critical, ex);
                throw;
                //throw new SaveFailedException(String.Format("Error while trying to save data for object:{0} of state:{1}",
                //    objectType.Name, this.State.ToString()), ex);
            }
        }