public void GetInstanceFromInstancesOfSpecification()
        {
            var spec   = (IObjectSpec)Metamodel.GetSpecification(typeof(Person));
            var person = Persistor.Instances(spec).Cast <Person>().Single(p => p.PersonId == 1);

            AssertIsPerson(person, 1);
        }
 public void GetInstanceIsAlwaysSameObject() {
     var spec = (IObjectSpec) Metamodel.GetSpecification(typeof (Person));
     var person1 = GetPerson(1);
     var person2 = Persistor.Instances(typeof (Person)).Cast<Person>().Single(p => p.PersonId == 1);
     var person3 = Persistor.Instances(spec).Cast<Person>().Single(p => p.PersonId == 1);
     Assert.AreSame(person1, person2);
     Assert.AreSame(person2, person3);
 }
 private Pet CreateNewTransientPet() {
     var nextIndex = Persistor.Instances<Pet>().Select(p => p.PetId).Max() + 1;
     var spec = (IObjectSpec) Metamodel.GetSpecification(typeof (Pet));
     var newPetAdapter = LifecycleManager.CreateInstance(spec);
     var pet = (Pet) newPetAdapter.Object;
     pet.PetId = nextIndex;
     return pet;
 }
        private Product CreateNewTransientProduct()
        {
            var nextIndex         = Persistor.Instances <Product>().Select(p => p.Id).Max() + 1;
            var spec              = (IObjectSpec)Metamodel.GetSpecification(typeof(Product));
            var newProductAdapter = LifecycleManager.CreateInstance(spec);
            var product           = (Product)newProductAdapter.Object;

            product.Id = nextIndex;
            return(product);
        }
        public void CreateAndDeleteNewObjectWithScalars() {
            var person = CreateNewTransientPerson();
            var name = Guid.NewGuid().ToString();
            person.Name = name;

            var adapter = Save(person);

            var person1 = Persistor.Instances<Person>().SingleOrDefault(p => p.Name == name);
            Assert.IsNotNull(person1);

            Delete(adapter.Object);

            var person2 = Persistor.Instances<Person>().SingleOrDefault(p => p.Name == name);

            Assert.IsNull(person2);
        }
        public void SaveNewObjectCallsPersistingPersistedRecursivelyFails() {
            Assert.AreEqual(0, Persistor.Instances<OrderFail>().Count());

            var order = CreateNewTransientOrderFail();
            order.Name = Guid.NewGuid().ToString();

            try {
                Save(order);
                Assert.Fail("Expect exception");
            }
// ReSharper disable once EmptyGeneralCatchClause
            catch (Exception) {
                // expected
            }

            Assert.AreEqual(0, Persistor.Instances<OrderFail>().Count());
        }
        public void SaveNewObjectCallsPersistingPersistedRecursively() {
            Assert.AreEqual(0, Persistor.Instances<Order>().Count());

            var order = CreateNewTransientOrder();
            order.Name = Guid.NewGuid().ToString();
            var adapter = Save(order);
            Assert.AreEqual(1, order.GetEvents()["Persisting"], "persisting");
            Assert.AreEqual(5, Persistor.Instances<Order>().Count());

            Assert.IsTrue(Persistor.Instances<Order>().All(i => i.PersistingCalled));

            // handle quirk in EF which swaps out object on save 
            // fix this when EF updated
            var orderAfter = (Order) adapter.Object;
            Assert.AreEqual(1, orderAfter.GetEvents()["Persisted"], "persisted");
            Assert.AreEqual(1, orderAfter.GetEvents()["Updating"], "updating");
            Assert.AreEqual(1, orderAfter.GetEvents()["Updated"], "updated");
        }
 private Product GetProduct(int id)
 {
     return(Persistor.Instances <Product>().Single(p => p.Id == id));
 }
        public void GetInstanceFromInstancesOfType()
        {
            var person = Persistor.Instances(typeof(Person)).Cast <Person>().Single(p => p.PersonId == 1);

            AssertIsPerson(person, 1);
        }