예제 #1
0
        public void InitAfterChanges()
        {
            _manager1.Root.Level1             = _manager1.Create <ILevel1>();
            _manager1.Root.Level1.IntValue    = 42;
            _manager1.Root.Level1.StringValue = "43";

            _manager1.Root.Level1.Level2           = _manager1.Create <ILevel2>();
            _manager1.Root.Level1.Level2.GuidValue = Guid.NewGuid();
            _manager1.Root.Level1.Level2.Level3    = new Level3 {
                IntValue = 44
            };

            _manager1.Register(_modelClient12);

            _manager2.Register(_modelClient21);
            _manager2.ApplyChanges(_modelClient21, _modelClient12.SerializeAndClearChanges());

            Assert.True(_manager1.IsDeepEqual(_manager1.Root, _manager2.Root));
        }
        public void SubscribeUnsubscribeTest()
        {
            int counter = 0;

            int notifications = 0;

            _manager.Subscriptions.OnChangeForSubscriptions = () => notifications++;

            var sub = _manager.Subscriptions.SubscribeForType <ILevel1>((o, c) => counter++);

            _manager.Root.Level1 = _manager.Create <ILevel1>();

            _manager.Subscriptions.GetAndClearNotifications().ForEach(p => p());
            Assert.Equal(1, counter);
            Assert.Equal(1, notifications);


            sub.Unsubscribe();
            _manager.Root.Level1 = _manager.Create <ILevel1>();
            _manager.Subscriptions.GetAndClearNotifications().ForEach(p => p());

            Assert.Equal(1, counter);
            Assert.Equal(1, notifications);
        }
        public void AddReplaceRemoveAndApplyChanges()
        {
            var modelClient = new ModelClient
            {
                Filter = new FilterConfigurator(true)
                         .Allow <ILevel1>(c => c.Deny(t => t.IntValue))
                         .Allow <ILevel2>(c => c.Allow(t => t.Level3))
                         .Build()
            };

            _manager.Register(modelClient);

            _manager.Root.Level1             = _manager.Create <ILevel1>();
            _manager.Root.Level1.IntValue    = 42;
            _manager.Root.Level1.StringValue = "43";

            _manager.Root.Level1.Level2           = _manager.Create <ILevel2>();
            _manager.Root.Level1.Level2.GuidValue = Guid.NewGuid();
            _manager.Root.Level1.Level2.Level3    = new Level3 {
                IntValue = 44
            };

            _manager.Root.Dictionary1[10]                               = _manager.Create <ILevel1>();
            _manager.Root.Dictionary1[10].IntValue                      = 40;
            _manager.Root.Dictionary1[10].StringValue                   = "some";
            _manager.Root.Dictionary1[10].Dictionary2["some"]           = _manager.Create <ILevel2>();
            _manager.Root.Dictionary1[10].Dictionary2["some"].GuidValue = Guid.NewGuid();

            var level1 = _manager.Create <ILevel1>();

            level1.IntValue                      = 50;
            level1.StringValue                   = "some";
            level1.Dictionary2["some"]           = _manager.Create <ILevel2>();
            level1.Dictionary2["some"].GuidValue = Guid.NewGuid();
            level1.Dictionary2["some"].Level3    = new Level3 {
                IntValue = 44
            };
            _manager.Root.Dictionary1[5] = level1;

            _manager2.ApplyChanges(modelClient, modelClient.SerializeAndClearChanges());
            Assert.Equal(0, _manager2.Root.Level1.IntValue);
            Assert.Equal("43", _manager2.Root.Level1.StringValue);
            Assert.Equal(Guid.Empty, _manager2.Root.Level1.Level2.GuidValue);

            Assert.Equal(44, _manager2.Root.Level1.Level2.Level3.IntValue);
            Assert.Equal(0, _manager2.Root.Dictionary1[10].IntValue);
            Assert.Equal("some", _manager2.Root.Dictionary1[10].StringValue);
            Assert.Equal(Guid.Empty, _manager2.Root.Dictionary1[10].Dictionary2["some"].GuidValue);

            Assert.Equal(0, _manager2.Root.Dictionary1[5].IntValue);
            Assert.Equal("some", _manager2.Root.Dictionary1[5].StringValue);
            Assert.Equal(Guid.Empty, _manager2.Root.Dictionary1[5].Dictionary2["some"].GuidValue);
            Assert.Equal(44, _manager2.Root.Dictionary1[5].Dictionary2["some"].Level3.IntValue);
        }
        public void SerializeDeserializeWithFilter()
        {
            var filter = new FilterConfigurator(true)
                         .Allow <ILevel1>(c => c.Deny(t => t.IntValue))
                         .Allow <ILevel2>(c => c.Allow(t => t.Level3))
                         .Build();

            _manager.Root.Level1             = _manager.Create <ILevel1>();
            _manager.Root.Level1.IntValue    = 42;
            _manager.Root.Level1.StringValue = "43";

            _manager.Root.Level1.Level2           = _manager.Create <ILevel2>();
            _manager.Root.Level1.Level2.GuidValue = Guid.NewGuid();
            _manager.Root.Level1.Level2.Level3    = new Level3 {
                IntValue = 44
            };

            _manager.Root.Dictionary1[10]                               = _manager.Create <ILevel1>();
            _manager.Root.Dictionary1[10].IntValue                      = 40;
            _manager.Root.Dictionary1[10].StringValue                   = "some";
            _manager.Root.Dictionary1[10].Dictionary2["some"]           = _manager.Create <ILevel2>();
            _manager.Root.Dictionary1[10].Dictionary2["some"].GuidValue = Guid.NewGuid();

            var level1 = _manager.Create <ILevel1>();

            level1.IntValue                      = 50;
            level1.StringValue                   = "some";
            level1.Dictionary2["some"]           = _manager.Create <ILevel2>();
            level1.Dictionary2["some"].GuidValue = Guid.NewGuid();
            level1.Dictionary2["some"].Level3    = new Level3 {
                IntValue = 44
            };
            _manager.Root.Dictionary1[5] = level1;

            var document = _manager.SerializeBson(_manager.Root, filter);
            var result   = _manager.DeserializeBson <ITestModel>(document);


            Assert.Equal(0, result.Level1.IntValue);
            Assert.Equal("43", result.Level1.StringValue);
            Assert.Equal(Guid.Empty, result.Level1.Level2.GuidValue);

            Assert.Equal(44, result.Level1.Level2.Level3.IntValue);
            Assert.Equal(0, result.Dictionary1[10].IntValue);
            Assert.Equal("some", result.Dictionary1[10].StringValue);
            Assert.Equal(Guid.Empty, result.Dictionary1[10].Dictionary2["some"].GuidValue);

            Assert.Equal(0, result.Dictionary1[5].IntValue);
            Assert.Equal("some", result.Dictionary1[5].StringValue);
            Assert.Equal(Guid.Empty, result.Dictionary1[5].Dictionary2["some"].GuidValue);
            Assert.Equal(44, result.Dictionary1[5].Dictionary2["some"].Level3.IntValue);
        }
예제 #5
0
 public T Create <T>()
 {
     return(_manager.Create <T>());
 }
예제 #6
0
        public void AddReplaceRemoveAndApplyChanges()
        {
            _manager1.Root.Level1             = _manager1.Create <ILevel1>();
            _manager1.Root.Level1.IntValue    = 42;
            _manager1.Root.Level1.StringValue = "43";

            _manager1.Root.Level1.Level2           = _manager1.Create <ILevel2>();
            _manager1.Root.Level1.Level2.GuidValue = Guid.NewGuid();
            _manager1.Root.Level1.Level2.Level3    = new Level3 {
                IntValue = 44
            };

            _manager1.Root.Dictionary1[10]          = _manager1.Create <ILevel1>();
            _manager1.Root.Dictionary1[10].IntValue = 40;

            var level1 = _manager1.Create <ILevel1>();

            level1.IntValue                      = 50;
            level1.Level2                        = _manager1.Create <ILevel2>();
            level1.Dictionary2["some"]           = _manager1.Create <ILevel2>();
            level1.Dictionary2["some"].GuidValue = Guid.NewGuid();

            _manager1.Root.Dictionary1[5] = level1;

            var a = _modelClient12.SerializeAndClearChanges();

            _manager2.ApplyChanges(_modelClient21, a);
            var b = _modelClient23.SerializeAndClearChanges();

            _manager3.ApplyChanges(_modelClient32, b);
            Assert.True(_manager1.IsDeepEqual(_manager1.Root, _manager2.Root));
            Assert.True(_manager1.IsDeepEqual(_manager1.Root, _manager3.Root));

            // replace
            _manager1.Root.Level1.IntValue = 43;
            _manager1.Root.Level1.Level2   = _manager1.Create <ILevel2>();
            _manager1.Root.Dictionary1[10] = _manager1.Create <ILevel1>();

            _manager2.ApplyChanges(_modelClient21, _modelClient12.SerializeAndClearChanges());
            _manager3.ApplyChanges(_modelClient32, _modelClient23.SerializeAndClearChanges());
            Assert.True(_manager1.IsDeepEqual(_manager1.Root, _manager2.Root));
            Assert.True(_manager1.IsDeepEqual(_manager1.Root, _manager3.Root));

            //remove
            _manager1.Root.Level1.StringValue = null;
            _manager1.Root.Level1.Level2      = null;
            _manager1.Root.Dictionary1.Remove(10);

            _manager2.ApplyChanges(_modelClient21, _modelClient12.SerializeAndClearChanges());
            _manager3.ApplyChanges(_modelClient32, _modelClient23.SerializeAndClearChanges());
            Assert.True(_manager1.IsDeepEqual(_manager1.Root, _manager2.Root));
            Assert.True(_manager1.IsDeepEqual(_manager1.Root, _manager3.Root));
        }