示例#1
0
 public override void UpdateProperty(Guid id, string propertyName, object value)
 {
     try {
         // Find modified poperty.
         PropertyInfo property = typeof(T).GetProperty(propertyName);
         if (property == null)
         {
             throw new ApplicationException("Property " + propertyName + " for " + typeof(T).Name + " could not be found, UpdateProperty failed.");
         }
         else
         {
             using (DataContext dataContext = DBLinqContext.CreateDBLinqDataContext(m_storageType, m_dbConnStr)) {
                 T emptyT = new T();
                 emptyT.Id = id.ToString();
                 property.SetValue(emptyT, value, null);
                 dataContext.ExecuteDynamicUpdate(emptyT, new List <MemberInfo>()
                 {
                     property
                 });
             }
         }
     }
     catch (Exception excp) {
         logger.Error("Exception DBLinqAssetPersistor Update (for " + typeof(T).Name + "). " + excp.Message);
         throw;
     }
 }
示例#2
0
        public override T Get(Guid id)
        {
            try {
                using (DataContext dataContext = DBLinqContext.CreateDBLinqDataContext(m_storageType, m_dbConnStr)) {
                    Table <T> table = dataContext.GetTable <T>();

                    string idString = id.ToString();

                    return((from asset in table
                            where asset.Id == idString
                            select asset).FirstOrDefault());
                }
            }
            catch (Exception excp) {
                logger.Error("Exception DBLinqAssetPersistor Get (id) (for " + typeof(T).Name + "). " + excp.Message);
                throw;
            }
        }
示例#3
0
        public override List <T> Get(Expression <Func <T, bool> > whereClause, string orderByField, int offset, int count)
        {
            try
            {
                using (DataContext dataContext = DBLinqContext.CreateDBLinqDataContext(m_storageType, m_dbConnStr)) {
                    Table <T> table = dataContext.GetTable <T>();

                    var query = from asset in table select asset;

                    if (whereClause != null)
                    {
                        query = query.Where(whereClause);
                    }

                    if (!orderByField.IsNullOrBlank())
                    {
                        query = query.OrderBy(orderByField);
                    }
                    else
                    {
                        query = query.OrderBy(x => x.Id);
                    }

                    if (offset != 0)
                    {
                        query = query.Skip(offset);
                    }

                    if (count < Int32.MaxValue)
                    {
                        query = query.Take(count);
                    }

                    return(query.ToList());
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception DBLinqAssetPersistor Get List (where for " + typeof(T).Name + "). " + excp.Message);
                return(null);
            }
        }
示例#4
0
        public override void Delete(T asset)
        {
            try {
                //m_dbLinqTable.DeleteOnSubmit(asset);
                //m_dbLinqDataContext.SubmitChanges();
                //m_dbLinqDataContext.ExecuteDynamicDelete(asset);
                using (DataContext dataContext = DBLinqContext.CreateDBLinqDataContext(m_storageType, m_dbConnStr)) {
                    dataContext.ExecuteDynamicDelete(asset);
                }

                if (Deleted != null)
                {
                    Deleted(asset);
                }
            }
            catch (Exception excp) {
                logger.Error("Exception DBLinqAssetPersistor Delete (for " + typeof(T).Name + "). " + excp.Message);
                throw;
            }
        }
示例#5
0
        public override int Count(Expression <Func <T, bool> > whereClause)
        {
            try {
                using (DataContext dataContext = DBLinqContext.CreateDBLinqDataContext(m_storageType, m_dbConnStr)) {
                    Table <T> table = dataContext.GetTable <T>();

                    if (whereClause == null)
                    {
                        return(table.Count());
                    }
                    else
                    {
                        return(table.Count(whereClause));
                    }
                }
            }
            catch (Exception excp) {
                logger.Error("Exception DBLinqAssetPersistor Count (for " + typeof(T).Name + "). " + excp.Message);
                throw excp;
            }
        }
示例#6
0
        public override T Add(T asset)
        {
            try {
                //m_dbLinqTable.InsertOnSubmit(asset);
                //m_dbLinqDataContext.SubmitChanges();
                //m_dbLinqDataContext.ExecuteDynamicInsert(asset);
                using (DataContext dataContext = DBLinqContext.CreateDBLinqDataContext(m_storageType, m_dbConnStr)) {
                    dataContext.ExecuteDynamicInsert(asset);
                }

                if (Added != null)
                {
                    Added(asset);
                }

                return(asset);
            }
            catch (Exception excp) {
                logger.Error("Exception DBLinqAssetPersistor Add (for " + typeof(T).Name + "). " + excp.Message);
                throw;
            }
        }
示例#7
0
        public override T Get(Expression <Func <T, bool> > whereClause)
        {
            try {
                using (DataContext dataContext = DBLinqContext.CreateDBLinqDataContext(m_storageType, m_dbConnStr)) {
                    Table <T> table = dataContext.GetTable <T>();

                    if (whereClause == null)
                    {
                        throw new ArgumentException("The where clause must be specified for a non-list Get.");
                    }
                    else
                    {
                        IQueryable <T> getList = from asset in table.Where(whereClause) select asset;
                        return(getList.FirstOrDefault());
                    }
                }
            }
            catch (Exception excp) {
                logger.Error("Exception DBLinqAssetPersistor Get (where for " + typeof(T).Name + "). " + excp.Message);
                return(default(T));
            }
        }
示例#8
0
        public override void Delete(Expression <Func <T, bool> > whereClause)
        {
            try {
                using (DataContext dataContext = DBLinqContext.CreateDBLinqDataContext(m_storageType, m_dbConnStr)) {
                    Table <T> table = dataContext.GetTable <T>();

                    var batch = from asset in table.Where(whereClause) select asset;

                    if (batch.Count() > 0)
                    {
                        T[] batchArray = batch.ToArray();
                        for (int index = 0; index < batchArray.Length; index++)
                        {
                            dataContext.ExecuteDynamicDelete(batchArray[index]);
                        }
                    }
                }
            }
            catch (Exception excp) {
                logger.Error("Exception DBLinqAssetPersistor Delete (batch) (for " + typeof(T).Name + "). " + excp.Message);
                throw;
            }
        }
示例#9
0
 public override object GetProperty(Guid id, string propertyName)
 {
     try {
         PropertyInfo property = typeof(T).GetProperty(propertyName);
         if (property == null)
         {
             throw new ApplicationException("Property " + propertyName + " for " + typeof(T).Name + " could not be found, GetProperty failed.");
         }
         else
         {
             using (DataContext dataContext = DBLinqContext.CreateDBLinqDataContext(m_storageType, m_dbConnStr)) {
                 Table <T> table    = dataContext.GetTable <T>();
                 string    idString = id.ToString();
                 Expression <Func <T, Object> > mySelect = DynamicExpression.ParseLambda <T, Object>(property.Name);
                 var query = (from asset in table where asset.Id == idString select asset).Select(mySelect);
                 return(query.FirstOrDefault());
             }
         }
     }
     catch (Exception excp) {
         logger.Error("Exception DBLinqAssetPersistor GetProperty (for " + typeof(T).Name + "). " + excp.Message);
         throw;
     }
 }