示例#1
0
        public void DisablePluralizer_Example()
        {
            var cn = TestHelper.CreateConnection();
            using (var session = new SimoSession(cn))
            {
                //Just call Disable on the Pluralizer
                //When you want to enable it again, call
                //Enable().
                var entityStore = new SimoEntityStore(session, DbName);
                entityStore.Session.Pluralizer.Disable();

                var person = new Person { Name = "Daniel" };
                entityStore.Insert(person);

                var refetched = entityStore.Database["Person"].FindOne<Person>(new { person.Name });

                Assert.IsNotNull(refetched);
            }
        }
        public void InsertSingle_TypedDocumentWithNoIdContainer_IsStoredAndAssignedId()
        {
            var person2Insert = new Person { Name = "Daniel", Age = 29 };

            using (var cn = TestHelper.CreateConnection())
            {
                var insertCommand = new InsertDocumentsCommand(cn)
                                    {
                                        FullCollectionName = Constants.Collections.PersonsFullCollectionName,
                                        Documents = new[] { person2Insert }
                                    };
                insertCommand.Execute();
            }

            var inferedTemplate = new { _id = SimoId.Empty };
            var refetched = TestHelper.GetDocument(person2Insert, inferedTemplate, Constants.Collections.PersonsCollectionName);
            Assert.AreNotEqual(SimoId.Empty, refetched._id);
        }
        public void QuerySinglePerson_AnonymousTypeQueryWithString_ItemReturned()
        {
            var person = new Person { Name = "Daniel", Age = 29 };
            TestHelper.InsertDocument(Constants.Collections.PersonsCollectionName, person);

            using (var cn = TestHelper.CreateConnection())
            {
                var queryCommand = new QueryDocumentsCommand<Person>(cn)
                                   {
                                       FullCollectionName = Constants.Collections.PersonsFullCollectionName,
                                       QuerySelector = new { Name = "Daniel" }
                                   };
                queryCommand.Execute();

                Assert.AreEqual(1, queryCommand.Response.NumberOfDocuments);
            }
        }
示例#4
0
        public void Insert_Example()
        {
            //The hierarchy in MongoDB is:
            //- Db
            //  - Collection
            //    - Document
            //By using EntityStore this is somewhat abstracted away.

            //You do need a connection to create a session
            //The creating of Connections and Session is something you most likely
            //will put in a IoC-container or factory or something.
            var cn = TestHelper.CreateConnection();
            using (var session = new SimoSession(cn))
            {
                var entityStore = new SimoEntityStore(session, DbName);

                //If you are dealing with typed documents (e.g. Person)
                //you can use the generic API.
                //Then the name of the collection is the name of the
                //type, but pluralized.
                //If you don't want pluralization, call
                //session.SimoPluralizer.Disable(), or
                //replace the implementation of the
                //SimoIoC.Instance.PluralizerResolver
                var person = new Person { Name = "Daniel", Age = 29 };
                entityStore.Insert(person);

                //If you are using non-typed documents you have to pass
                //the entityname string to the method you are using.
                var anonymousPerson = new { Name = "Daniel" };
                entityStore.Insert("Person", anonymousPerson);

                //So the EntityStore only allows for an abstraction
                //over the Db, Collection and Document hierarchy
                //and you can of course access these to.
                var db = session[DbName];
                var persons = db["Person"];
                persons.Insert(anonymousPerson);

                //The EntityStore also holds the Database that it wraps.
                //Of course you can obtain a collection using generics against the
                //database.
                var persons2 = entityStore.Database.GetCollection<Person>();
                persons2.Insert(person);

                var numOfStoredDocuments = entityStore.Count<Person>();
                Assert.AreEqual(4, numOfStoredDocuments);
            }
        }