Esempio n. 1
0
        public virtual List <T> GetByParameter(string action, List <QueryParameter> parameters, bool isLazy = true)
        {
            List <T> items = null;

            items = new List <T>();
            T      entity = Activator.CreateInstance <T>();
            string spName = new Common().GetSpName(entity.GetType().Name.ToLower(), action);

            using (IDBManager dbManager = new DBManager(DataProvider.SqlServer))
            {
                try
                {
                    dbManager.Open();
                    dbManager.Parameters = new GenericMapper().AttachParameters(spName, parameters);
                    using (IDataReader reader = dbManager.ExecuteReader(CommandType.StoredProcedure, spName))
                    {
                        while (reader.Read())
                        {
                            if (items == null)
                            {
                                items = new List <T>();
                            }
                            entity = new GenericMapper().Map <T>(reader, isLazy);
                            items.Add(entity);
                        }
                    }
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new DLException("Error while loading data. Action: " + action + ". Store procedure: " + spName, ex);
                }
            }
            return(items);
        }
Esempio n. 2
0
        public virtual T GetByID(object id)
        {
            T      entity = Activator.CreateInstance <T>();
            string spName = new Common().GetSpName(entity.GetType().Name.ToLower(), "select");

            using (IDBManager dbManager = new DBManager(DataProvider.SqlServer))
            {
                try
                {
                    dbManager.Open();
                    dbManager.CreateParameters(1);
                    dbManager.AddParameters(0, "@id", id);

                    using (IDataReader reader = dbManager.ExecuteReader(CommandType.StoredProcedure, spName))
                    {
                        while (reader.Read())
                        {
                            entity = new GenericMapper().Map <T>(reader, false);
                        }
                    }
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new DLException("Error occured while loading data from database: " + entity.GetType().Name + ", id: " + id.ToString(), ex);
                }
            }
            return(entity);
        }
Esempio n. 3
0
        public virtual List <T> GetAll(QueryOptions queryOptions = null)
        {
            List <T> items = null;

            items = new List <T>();
            T      entity = Activator.CreateInstance <T>();
            string spName = new Common().GetSpName(entity.GetType().Name.ToLower(), "get");

            using (IDBManager dbManager = new DBManager(DataProvider.SqlServer))
            {
                try
                {
                    dbManager.Open();

                    using (IDataReader reader = dbManager.ExecuteReader(CommandType.StoredProcedure, spName))
                    {
                        while (reader.Read())
                        {
                            if (items == null)
                            {
                                items = new List <T>();
                            }
                            entity = new GenericMapper().Map <T>(reader);
                            items.Add(entity);
                        }
                    }
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new DLException("Error while loading data from database: " + entity.GetType().Name, ex);
                }
            }
            return(items);
        }