예제 #1
0
        private void AddElement(IDomainModel domain, int max)
        {
            ids = new ConcurrentDictionary<int, Identity>();

            Parallel.For(0, max, i =>
            {
                using (var tx = store.BeginSession())
                {
                    var a = new XExtendsBaseClass(domain);
                    if (ids.TryAdd(i, ((IModelElement)a).Id))
                        tx.AcceptChanges();
                }
            });
        }
예제 #2
0
 private void CreateLoan()
 {
     try
     {
         // Run a command
         using (var session = store.BeginSession())
         {
             session.Execute(new NewLoanCommand(SelectedBook, SelectedMember));
             session.AcceptChanges();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     };
 }
        private void Serialize(Stream stream, IEnumerable <IModelEntity> entities, IEnumerable <IModelRelationship> relationships, HashSet <Identity> deletedEntities = null, HashSet <Identity> deletedRelationShips = null)
        {
            ISession session = null;

            if (Session.Current != null)
            {
                Session.Current.SetMode(SessionMode.Serializing);
            }
            else
            {
                IHyperstore          store = _domain.Store;
                SessionConfiguration sessionConfiguration = new SessionConfiguration();
                sessionConfiguration.Mode = SessionMode.Serializing;
                session = store.BeginSession(sessionConfiguration);
            }

            try
            {
                _monikers = new Dictionary <Identity, MonikerEntry>();
                SerializeEntities(entities, deletedEntities);
                SerializeRelationships(relationships, deletedRelationShips);
                _writer.Save(stream, _monikers.Values);
            }
            finally
            {
                if (session != null)
                {
                    session.AcceptChanges();
                    session.Dispose();
                }
                _monikers        = null;
                _monikerSequence = 0;
            }
        }
예제 #4
0
        private ISession EnsuresRunInSession()
        {
            if (Session.Current != null)
            {
                return(null);
            }

            return(_store.BeginSession(new SessionConfiguration
            {
                Readonly = true
            }));
        }
예제 #5
0
        public async Task ConstraintNotExecuted()
        {
            await CreateDomain();

            Library lib;

            using (var session = store.BeginSession())
            {
                lib       = new Library(domain);
                lib.Name  = "My Library"; // OK
                lib.Email = "toto";       // Failed
                // rollback;
            }

            using (var session = store.BeginSession(new SessionConfiguration {
                Mode = SessionMode.SkipConstraints
            }))
            {
                lib       = new Library(domain);
                lib.Name  = "My Library"; // OK
                lib.Email = "toto";       // Failed
                session.AcceptChanges();
            }
        }
예제 #6
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Ensures run in session.
        /// </summary>
        /// <exception cref="SessionRequiredException">
        ///  Thrown when a Session Required error condition occurs.
        /// </exception>
        /// <returns>
        ///  An ISession.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        protected ISession EnsuresRunInSession(bool readOnly = false)
        {
            if (Session.Current != null)
            {
                return(null);
            }

            if (!(this is INotifyPropertyChanged) || (_schema.Schema.Behavior & DomainBehavior.Observable) != DomainBehavior.Observable)
            {
                throw new SessionRequiredException();
            }

            return(_store.BeginSession(new SessionConfiguration {
                Readonly = readOnly
            }));
        }
예제 #7
0
        private void ProcessEvents(ProcessInfo info)
        {
            var tx = _store.BeginSession(new SessionConfiguration
            {
                IsolationLevel = SessionIsolationLevel.Serializable,
                Mode           = info.Mode
            });

            _store.Trace.WriteTrace(TraceCategory.EventBus, "Process events from {0} for Store {1}", info.Origin, _store.Id);

            try
            {
                foreach (var @event in info.Events)
                {
                    if (@event == null)
                    {
                        continue;
                    }

                    _store.Trace.WriteTrace(TraceCategory.EventBus, "Process event : " + @event);
                    var dispatcher = GetEventDispatcher(@event);
                    if (dispatcher != null)
                    {
                        dispatcher.HandleEvent(@event);
                    }
                }

                tx.AcceptChanges();
            }
            catch (Exception ex)
            {
                ((ISessionInternal)tx).SessionContext.Log(new DiagnosticMessage(MessageType.Error, "ProcessEvents", "EventProcessor", null, ex));
                throw;
            }
            finally
            {
                tx.Dispose();
            }
        }
예제 #8
0
        public async Task SerializatonBench(int cx)
        {
            store = await StoreBuilder.New().CreateAsync();

            await store.Schemas.New <LibraryDefinition>().CreateAsync();

            var domain = await store.DomainModels
                         .New()
                         .UsingIdGenerator(services => new Hyperstore.Modeling.Domain.LongIdGenerator())
                         .CreateAsync("Test");

            var sw = new Stopwatch();

            Console.WriteLine("Benchmark serializing {0} elements...", cx * 2);

            Library lib;

            using (var session = store.BeginSession())
            {
                lib      = new Library(domain);
                lib.Name = "Lib1";
                session.AcceptChanges();
            }

            Parallel.For(0, cx, (i) =>
            {
                using (var session = store.BeginSession())
                {
                    var b    = new Book(domain);
                    b.Title  = "Book \"book\" " + i.ToString();
                    b.Copies = i + 1;
                    lib.Books.Add(b);

                    var m  = new Member(domain);
                    m.Name = "Book " + i.ToString();
                    lib.Members.Add(m);
                    session.AcceptChanges();
                }
            });
            //  Console.ReadKey();
            //Console.Write("xml ...");
            //sw.Start();
            //using (var stream = File.Open("test.xml",FileMode.Create))
            //{
            //    HyperstoreSerializer.Serialize(stream, domain);
            //}
            //Console.WriteLine(" : serialize {1:n}bytes in {0}ms ", sw.ElapsedMilliseconds, new FileInfo("test.xml").Length);

            //Console.Write("json ...");
            sw.Restart();
            using (var stream = File.Open("test.json", FileMode.Create))
            {
                HyperstoreSerializer.Serialize(stream, domain, new SerializationSettings {
                    Options = SerializationOptions.Json | SerializationOptions.CompressSchema
                });
            }
            // Console.WriteLine(" : serialize {1:n}bytes in {0}ms ", sw.ElapsedMilliseconds, new FileInfo("test.json").Length);

            //Console.Write("old xml ...");
            //sw.Restart();
            //using (var stream = File.Open("test2.xml", FileMode.Create))
            //{
            //    var ser = new XmlDomainModelSerializer();
            //    await ser.Serialize(domain, stream, XmlSerializationOptions.Elements);
            //}
            //Console.WriteLine(" : serialize {1:n}bytes in {0}ms ", sw.ElapsedMilliseconds, new FileInfo("test2.xml").Length);


            //  Console.ReadKey();
        }