Пример #1
0
        /// <summary>
        ///     Serialize a class
        /// </summary>
        /// <param name="type">The type</param>
        /// <param name="propertyName">The name of the property.</param>
        /// <param name="foreignTable">The referenced type</param>
        /// <param name="bw">The writer</param>
        /// <param name="cache">Cycle cache</param>
        private void _SerializeClass(Type type, string propertyName, object foreignTable, BinaryWriter bw, CycleCache cache)
        {
            bw.Write(propertyName + PROPERTY_VALUE_SEPARATOR);
            bw.Write(_typeResolver(type.AssemblyQualifiedName));

            // serialize to the stream if the foreign key is nulled
            _SerializeNull(bw, foreignTable == null);

            if (foreignTable == null)
            {
                return;
            }

            var foreignKey = _database.Save(foreignTable.GetType(), foreignTable.GetType(), foreignTable, cache);

            // need to be able to serialize the key
            if (!_serializer.CanSerialize(foreignKey.GetType()))
            {
                var exception = new SterlingSerializerException(_serializer, foreignKey.GetType());
                _logManager.Log(SterlingLogLevel.Error, exception.Message, exception);
                throw exception;
            }

            _logManager.Log(SterlingLogLevel.Verbose,
                            string.Format(
                                "Sterling is saving foreign key of type {0} with value {1} for parent {2}",
                                foreignKey.GetType().FullName, foreignKey, type.FullName), null);

            _serializer.Serialize(foreignKey, bw);
        }
        /// <summary>
        /// Asynchronous version of ISterlingDatabaseInstance Save method (Begin part).
        /// </summary>
        public static IAsyncResult BeginSave <T>(
            this ISterlingDatabaseInstance sterling,
            T instance,
            AsyncCallback callback,
            Object state) where T : class, new()
        {
            // Create IAsyncResult Object identifying the asynchronous operation.
            AsyncResult ar = new AsyncResult(callback, state);

            // Use a thread pool thread to perform the operation.
            ThreadPool.QueueUserWorkItem((obj) =>
            {
                var asyncResult = (AsyncResult)obj;
                try
                {
                    // Perform the operation.
                    sterling.Save <T>(instance);
                    asyncResult.SetAsCompleted(null, false);
                }
                catch (Exception e)
                {
                    // If operation fails, set the exception.
                    asyncResult.SetAsCompleted(e, false);
                }
            }, ar);

            return(ar); // Return the IAsyncResult to the caller.
        }
Пример #3
0
        public static void PopulateDatabase(ISterlingDatabaseInstance db)
        {
            var colors  = new[] { "Red", "Orange", "Yellow", "Blue", "Green", "Indigo", "Violet" };
            var cats    = new[] { "Panther", "Cougar", "Lynx", "Jaguar", "Leopard", "Cheetah", "Lion" };
            var planets = new[] { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };

            var colorItemList = colors.Select(c => new CoolColor {
                Name = c
            }).ToList();

            Console.WriteLine("Saving colors (doesn't really take a second, but we'll wait anyway)...");
            db.SaveAsync <CoolColor>(colorItemList);

            Thread.Sleep(1000); // just pause a bit because we're not monitoring the background worker
            Console.WriteLine("Creating combinations...");
            var planetId   = 1;
            var comboCount = 0;

            foreach (var colorItem in colorItemList)
            {
                foreach (var cat in cats)
                {
                    var catItem = new Cat {
                        Key = $"CAT-{cat}", Name = cat
                    };
                    db.Save(catItem);
                    foreach (var planet in planets)
                    {
                        comboCount++;
                        var planetItem = new Planet {
                            Id = planetId++, Name = planet
                        };
                        db.Save(planetItem);
                        var comboItem = new Combo {
                            Id = comboCount, Cat = catItem, Planet = planetItem, Color = colorItem
                        };
                        db.Save(comboItem);
                    }
                }
            }
            Console.WriteLine($"Generated and saved {comboCount} combinations.");
        }