public void ShouldShareReferencesAcrossMultipleMappings()
        {
            var cache        = new PreserveReferencesState();
            var mapperConfig = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <Type1, Type2>()
                .ConstructUsing(cache)
                .ReverseMap()
                .ConstructUsing(cache);
            });
            var mapper = mapperConfig.CreateMapper();

            var backend1 = new ComposableDictionary <string, Type1>();
            var backend2 = new ComposableDictionary <string, Type1>();
            var item1    = new Type1()
            {
                Name = "item1"
            };

            backend1.Add("item1", item1);
            backend2.Add("item1", item1);

            var frontend1 = backend1.WithAutoMapping <string, Type1, Type2>(mapper);
            var frontend2 = backend2.WithAutoMapping <string, Type1, Type2>(mapper);

            frontend1.TryGetValue("item1", out var item1_frontend1).Should().BeTrue();
            item1_frontend1.Name.Should().Be("item1");
            frontend1.ContainsKey("item1").Should().BeTrue();

            frontend2.TryGetValue("item1", out var item1_frontend2).Should().BeTrue();
            item1_frontend2.Name.Should().Be("item1");
            frontend2.ContainsKey("item1").Should().BeTrue();

            ReferenceEquals(item1_frontend1, item1_frontend2).Should().BeTrue();
        }
Beispiel #2
0
 public bool Initialize <TKey, TValue>(Func <TKey, TValue> constructor)
 {
     lock (_lock)
     {
         var key = typeof(IKeyValue <TKey, TValue>);
         return(_composableDictionaries.TryAdd(key, () =>
         {
             var result = new ComposableDictionary <TKey, TValue>()
                          .WithDefaultValue(((TKey key1, out TValue value) =>
             {
                 value = constructor(key1);
                 return true;
             }));
             _clearActions.Add(() => result.Clear());
             return result;
         }));
     }
 }
Beispiel #3
0
            private static bool Prefix(ComposableDictionary <string, ElementBandConfiguration> __instance, ComposableDictionary <string, ElementBandConfiguration> other)
            {
                Traverse.Create(__instance).Method("VerifyConsolidated", new Type[0]).GetValue();

                foreach (var key in other.remove)
                {
                    __instance.add.Remove(key);
                }
                foreach (var keypair in other.add)
                {
                    if (__instance.add.TryGetValue(keypair.Key, out var exists))
                    {
                        __instance[keypair.Key] = keypair.Value;
                    }
                    else
                    {
                        __instance.Add(keypair.Key, keypair.Value);
                    }
                }

                return(false);
            }