コード例 #1
0
        public bool Delete <TEntity> (IID entity) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                Type           baseType = typeof(TEntity);
                TableAttribute ta       = null;

                while (true)
                {
                    ta = (TableAttribute)Attribute.GetCustomAttribute(baseType, typeof(TableAttribute));

                    if (ta != null)
                    {
                        break;
                    }

                    baseType = baseType.BaseType;

                    if (baseType == null)
                    {
                        break;
                    }
                }

                if (ta == null)
                {
                    throw new ArgumentException("Failed to find Table of " + typeof(TEntity).ToString( ));
                }


                using (CSDB context = new CSDB(Connection))
                {
#if (DEBUG)
                    TextWriter log = new StringWriter();
                    context.Log = log;
#endif
                    TEntity t = GetSingle <TEntity> (entity.ID, context);

                    context.GetTable(baseType).DeleteOnSubmit(t);
                    context.SubmitChanges( );
#if (UseCache)
                    cache.DeleteItemFromCache(baseType, ((IID)t).ID);
#endif
                    return(true);
                }
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error deleting entity from database of type " + typeof(TEntity).ToString( ));
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }
コード例 #2
0
        private bool addRange <TEntity> (TEntity[] entities) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                ArrayList newEntities = new ArrayList(entities.Count());
                TEntity   newEntity;

                // determine type here
                var type = typeof(TEntity);   //.GetType(); // entities.GetType();

                foreach (IID entity in entities)
                {
                    // create an object of the type
                    newEntity = (TEntity)Activator.CreateInstance(type);

                    MakeObjectsEqual(newEntity, entity);

                    newEntities.Add(newEntity);
                }


                using (CSDB context = new CSDB(Connection))
                {
                    TEntity[] array = (TEntity[])newEntities.ToArray();
                    context.GetTable <TEntity> ( ).InsertAllOnSubmit(array);
                    context.SubmitChanges( );
                }

                for (int i = 0; i < entities.Count( ); i++)
                {
                    ((IID)entities[i]).ID            = ((IID)newEntities[i]).ID;
                    ((IID)entities[i]).Discriminator = ((IID)newEntities[i]).Discriminator;
                    cache.AddToCache(type, (IID)entities[i]);
                }

                return(true);
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error inserting range into database of type " + typeof(TEntity).ToString( ));
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }
コード例 #3
0
        public bool DeleteRange <TEntity> (TEntity[] entities) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                using (CSDB context = new CSDB(Connection))
                {
                    // retrieve entities from context

                    HashSet <TEntity> ToBeDeleted = new HashSet <TEntity>();

                    foreach (IID entity in entities)
                    {
                        TEntity t = GetSingle <TEntity> (entity.ID, context);
                        if (t != null)
                        {
                            ToBeDeleted.Add(t);
                        }
                    }
                    if (ToBeDeleted.Count == 0)
                    {
                        return(false);
                    }

                    context.GetTable <TEntity> ( ).DeleteAllOnSubmit(ToBeDeleted);
                    context.SubmitChanges( );

                    foreach (IID entity in entities)
                    {
                        cache.DeleteItemFromCache(typeof(TEntity), ((IID)entity).ID);
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error deleting range from database of type " + typeof(TEntity).ToString( ));
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }
コード例 #4
0
        private bool add <TEntity> (TEntity entity) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            try
            {
                TEntity newEntity;

                // determine type here
                var type = entity.GetType();

                Type           baseType = typeof(TEntity);
                TableAttribute ta       = null;

                while (true)
                {
                    ta = (TableAttribute)Attribute.GetCustomAttribute(baseType, typeof(TableAttribute));

                    if (ta != null)
                    {
                        break;
                    }

                    baseType = baseType.BaseType;

                    if (baseType == null)
                    {
                        break;
                    }
                }

                if (ta == null)
                {
                    throw new ArgumentException("Failed to find Table of " + typeof(TEntity).ToString( ));
                }


                // create an object of the type
                newEntity = (TEntity)Activator.CreateInstance(type);

                MakeObjectsEqual(newEntity, entity);

                using (CSDB context = new CSDB(Connection))
                {
                    context.GetTable <TEntity> ( ).InsertOnSubmit(newEntity);
                    context.SubmitChanges( );
                }

                ((IID)entity).ID            = ((IID)newEntity).ID;
                ((IID)entity).Discriminator = ((IID)newEntity).Discriminator;

                cache.AddToCache(baseType, (IID)entity);

                return(true);
            }
            catch (Exception ex)
            {
                ChampionshipSolutions.Diag.Diagnostics.LogLine("Error inserting into database of type " + typeof(TEntity).ToString());
                ChampionshipSolutions.Diag.Diagnostics.LogLine(ex.Message);
                return(false);
            }
        }
コード例 #5
0
        public TEntity GetID <TEntity> (int?ID) where TEntity : class
        {
            if (ID == null)
            {
                return(null);
            }

            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }


            Type           baseType = typeof(TEntity);
            TableAttribute ta       = null;

            while (true)
            {
                ta = (TableAttribute)Attribute.GetCustomAttribute(baseType, typeof(TableAttribute));

                if (ta != null)
                {
                    break;
                }

                baseType = baseType.BaseType;

                if (baseType == null)
                {
                    break;
                }
            }

            if (ta == null)
            {
                throw new ArgumentException("Failed to find Table of " + typeof(TEntity).ToString( ));
            }
#if (UseCache)
            // Try to get the data from the cache first
            bool    deleted;
            TEntity cacheObj = (TEntity)cache.GetFromCache(baseType, ID.Value, out deleted);

            if (deleted)
            {
                return(null);
            }

            if (cacheObj != null)
            {
                CacheReadCounter++;
                return(cacheObj);
            }
#endif
            // could not find this item in the cache or it needs updating.

            using (CSDB context = new CSDB(Connection))
            {
                TEntity temp = context.GetTable(baseType).OfType <TEntity>().Where(t => ((IID)t).ID == ID).ToList( ).FirstOrDefault();

                if (temp != null)
                {
                    if (typeof(IID).IsAssignableFrom(typeof(TEntity)))
                    {
                        ((IID)temp).DState = getDataState( );
#if (UseCache)
                        cache.AddToCache(baseType, (IID)temp);
#endif
                    }
                }

                DBReadCounter++;
                return(temp);
            }
        }
コード例 #6
0
        public IList <TEntity> GetAll <TEntity> ( ) where TEntity : class
        {
            if (Connection == null)
            {
                throw new Exception("No connection to database");
            }

            // try to get the Table (base) type instead of T

            Type           baseType = typeof(TEntity);
            TableAttribute ta       = null;

            while (true)
            {
                ta = (TableAttribute)Attribute.GetCustomAttribute(baseType, typeof(TableAttribute));

                if (ta != null)
                {
                    break;
                }

                baseType = baseType.BaseType;

                if (baseType == null)
                {
                    break;
                }
            }

            if (ta == null)
            {
                throw new ArgumentException("Failed to find Table of " + typeof(TEntity).ToString( ));
            }

#if (UseCache)
            // Try to get the data from the cache first
            var cacheTable = cache.GetTableFromCache(baseType);

            if (cacheTable != null)
            {
                CacheReadCounter++;

                // Casting list makes sure that only valid items are returned.
                // For example if TEntity is Athlete then we need to remove all
                // objects of type person.
                List <TEntity> castingList = new List <TEntity>();

                foreach (var i in cacheTable)
                {
                    if (i is TEntity)
                    {
                        castingList.Add((TEntity)i);
                    }
                }

                return(castingList.Cast <TEntity> ( ).ToList( ));
            }

            // could not find this item in the cache or it needs updating.
#endif
            using (CSDB context = new CSDB(Connection))
            {
                context.DeferredLoadingEnabled = false;


                IList <TEntity> temp = context.GetTable(baseType).OfType <TEntity>().ToList( );

                if (typeof(IID).IsAssignableFrom(typeof(TEntity)))
                {
                    foreach (TEntity ent in temp)
                    {
                        ((IID)ent).DState = getDataState( );
                    }

#if (UseCache)
                    cache.AddTableToCache(baseType, temp.Cast <IID>().ToList());
#endif
                }

                DBReadCounter++;
                return(temp);
            }
        }