Example #1
0
        public void Persist_Method()
        {
            Assert.Throws <ArgumentNullException>(() => ObjectExtensions.Persist <TestEntity>(null));
            Assert.Throws <InvalidOperationException>(() => ServiceLocator.Current.GetInstance <IRepository <TestEntity> >());

            ServiceLocator.SetLocatorProvider(() => this.serviceLocator);

            var entity = new TestEntity {
                Name = "first"
            };

            using (var repository = ServiceLocator.Current.GetInstance <IRepository <TestEntity> >())
            {
                Assert.False(repository.Any());

                repository.Transaction(() => Assert.True(ReferenceEquals(entity.Persist(), entity)));
                entity = repository.Single();
                Assert.NotEqual(0, entity.Id);
                Assert.Equal("first", entity.Name);

                entity.Name = "second";
                repository.Transaction(() => entity.Persist());
                entity = repository.Single();
                Assert.NotEqual(0, entity.Id);
                Assert.Equal("second", entity.Name);
            }
        }
Example #2
0
        public void Refresh_Method()
        {
            Assert.Throws <ArgumentNullException>(() => ObjectExtensions.Refresh <TestEntity>(null));
            Assert.Throws <InvalidOperationException>(() => ServiceLocator.Current.GetInstance <IRepository <TestEntity> >());

            ServiceLocator.SetLocatorProvider(() => this.serviceLocator);

            var entity = new TestEntity {
                Name = "first"
            };

            using (var repository = ServiceLocator.Current.GetInstance <IRepository <TestEntity> >())
            {
                Assert.True(ReferenceEquals(entity.Refresh(), entity));
                Assert.Equal(0, entity.Id);
                Assert.Equal("first", entity.Name);

                repository.Transaction(() => entity.Persist());
                var originalId   = entity.Id;
                var originalName = entity.Name;

                entity.Name = "second";
                entity.Refresh();
                Assert.Equal(originalId, entity.Id);
                Assert.Equal(originalName, entity.Name);
            }
        }
    public void Persist_Method()
    {
      Assert.Throws<ArgumentNullException>(() => ObjectExtensions.Persist<TestEntity>(null));
      Assert.Throws<InvalidOperationException>(() => ServiceLocator.Current.GetInstance<IRepository<TestEntity>>());

      ServiceLocator.SetLocatorProvider(() => this.serviceLocator);

      var entity = new TestEntity { Name = "first" };
      using (var repository = ServiceLocator.Current.GetInstance<IRepository<TestEntity>>())
      {
        Assert.False(repository.Any());

        repository.Transaction(() => Assert.True(ReferenceEquals(entity.Persist(), entity)));
        entity = repository.Single();
        Assert.NotEqual(0, entity.Id);
        Assert.Equal("first", entity.Name);

        entity.Name = "second";
        repository.Transaction(() => entity.Persist());
        entity = repository.Single();
        Assert.NotEqual(0, entity.Id);
        Assert.Equal("second", entity.Name);
      }
    }
    public void Delete_Method()
    {
      Assert.Throws<ArgumentNullException>(() => ObjectExtensions.Delete<TestEntity>(null));
      Assert.Throws<InvalidOperationException>(() => ServiceLocator.Current.GetInstance<IRepository<TestEntity>>());

      ServiceLocator.SetLocatorProvider(() => this.serviceLocator);

      var entity = new TestEntity();
      using (var repository = ServiceLocator.Current.GetInstance<IRepository<TestEntity>>())
      {
        repository.Transaction(() => Assert.True(ReferenceEquals(entity.Delete(), entity)));
        repository.Transaction(() => entity.Persist());
        repository.Transaction(() => new TestEntity().Delete());
        Assert.True(ReferenceEquals(repository.Single(), entity));

        repository.Transaction(() => entity.Delete());
        Assert.False(repository.Any());
      }
    }
Example #5
0
        public void Delete_Method()
        {
            Assert.Throws <ArgumentNullException>(() => ObjectExtensions.Delete <TestEntity>(null));
            Assert.Throws <InvalidOperationException>(() => ServiceLocator.Current.GetInstance <IRepository <TestEntity> >());

            ServiceLocator.SetLocatorProvider(() => this.serviceLocator);

            var entity = new TestEntity();

            using (var repository = ServiceLocator.Current.GetInstance <IRepository <TestEntity> >())
            {
                repository.Transaction(() => Assert.True(ReferenceEquals(entity.Delete(), entity)));
                repository.Transaction(() => entity.Persist());
                repository.Transaction(() => new TestEntity().Delete());
                Assert.True(ReferenceEquals(repository.Single(), entity));

                repository.Transaction(() => entity.Delete());
                Assert.False(repository.Any());
            }
        }
    public void Refresh_Method()
    {
      Assert.Throws<ArgumentNullException>(() => ObjectExtensions.Refresh<TestEntity>(null));
      Assert.Throws<InvalidOperationException>(() => ServiceLocator.Current.GetInstance<IRepository<TestEntity>>());

      ServiceLocator.SetLocatorProvider(() => this.serviceLocator);

      var entity = new TestEntity { Name = "first" };
      using (var repository = ServiceLocator.Current.GetInstance<IRepository<TestEntity>>())
      {
        Assert.True(ReferenceEquals(entity.Refresh(), entity));
        Assert.Equal(0, entity.Id);
        Assert.Equal("first", entity.Name);

        repository.Transaction(() => entity.Persist());
        var originalId = entity.Id;
        var originalName = entity.Name;

        entity.Name = "second";
        entity.Refresh();
        Assert.Equal(originalId, entity.Id);
        Assert.Equal(originalName, entity.Name);
      }
    }