示例#1
0
        /// <summary>
        /// Overwrites a record. Used to update records. (or set the removed flag)
        /// </summary>
        /// <param name="mainFile">File stream of the dbset.</param>
        /// <param name="customInfos">To compare.</param>
        /// <param name="primitiveInfos">To compare.</param>
        /// <param name="orderedInfos">The infos in the order of being written to the file.</param>
        /// <param name="record">Record to be overwritten.</param>
        public static void Overwrite(Stream mainFile, BaseClass record)
        {
            int indexToBeInserted = record.id;

            mainFile.Position = record.RowSize() * (indexToBeInserted - 1); //gerekirse düzelt
            BinaryWriter bw = new BinaryWriter(mainFile);

            foreach (PropertyInfo info in record.OrderedInfos())
            {
                if (record.PrimitiveInfos().Contains(info))
                {
                    WriteSingleProperty(bw, info, info.GetValue(record));
                }
                else if (info.PropertyType.IsGenericType)
                {
                    continue;
                }
                else
                {
                    BaseClass child = (BaseClass)info.GetValue(record);

                    if (child == null)
                    {
                        bw.Write(-1);
                    }
                    else
                    {
                        bw.Write(child.id);
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Inserts a record for the first time. Returns the id of last inserted record
        /// </summary>
        /// <param name="mainFile">Stream of the file, that holds the data for the DbSet of the record</param>
        /// <param name="removedIndexes">Empty index numbers in the file, can be filled with new records</param>
        /// <param name="infos">Properties that will be stored in database. (To exclude notmapped properties)</param>
        /// <param name="record">Record to be inserted</param>
        /// <returns>Returns the id of last inserted record</returns>
        public static int Add(Stream mainFile, List <int> removedIndexes, BaseClass record)
        {
            int indexToBeInserted = -1;

            if (removedIndexes.Count() > 0)
            {
                indexToBeInserted = removedIndexes[0];
                Overwrite(mainFile, record);
                return(indexToBeInserted);
            }
            mainFile.Position = (mainFile.Length / record.RowSize()) * ((indexToBeInserted == -1) ? record.RowSize() : indexToBeInserted); //
            indexToBeInserted = Convert.ToInt32(mainFile.Position / record.RowSize()) + 1;                                                 //idler 1den başlasın
            BinaryWriter bw = new BinaryWriter(mainFile);

            foreach (PropertyInfo info in record.OrderedInfos())
            {
                if (record.PrimitiveInfos().Contains(info))
                {
                    if (info.Name == "id")
                    {
                        WriteSingleProperty(bw, info, indexToBeInserted);
                    }
                    else
                    {
                        WriteSingleProperty(bw, info, info.GetValue(record));
                    }
                }
                else if (record.OTORelInfos().Contains(info))
                {
                    BaseClass child = (BaseClass)info.GetValue(record);
                    if (child == null)
                    {
                        bw.Write(-1);
                    }
                    else
                    {
                        bw.Write(child.id);
                    }
                }
                //one to many ise burada iş yok, dbset'te request yapılacak.
            }


            return(indexToBeInserted);//id dönecek
        }