Example #1
0
        public void EntityKey_Creation()
        {
            // Test one of the generic Create overloads (that doesn't box
            // the key values)
            object key          = EntityKey.Create(5, 2.34M, "test", "hello world");
            string formattedKey = key.ToString();

            object[] keyValues   = new object[] { 5, 2.34M, "test", "hello world" };
            string   expectedKey = "{" + string.Join(",", keyValues) + "}";

            Assert.AreEqual(expectedKey, formattedKey);

            // pass the same set into the params version and verify the keys are equal
            object key2 = EntityKey.Create(keyValues);

            Assert.AreSame(key.GetType(), key2.GetType());
            Assert.AreEqual(key, key2);
            Assert.AreEqual(key.GetHashCode(), key2.GetHashCode());

            // Test boxing version for N key values
            key          = EntityKey.Create(5, "hello", 3, 4, 5, "test", 2132, '?');
            formattedKey = key.ToString();
            Assert.AreEqual("{5,hello,3,4,5,test,2132,?}", formattedKey);

            // pass the same set of values into the params version and verify the keys are equal
            key2 = EntityKey.Create(new object[] { 5, "hello", 3, 4, 5, "test", 2132, '?' });
            Assert.AreEqual(key, key2);
            Assert.AreEqual(key.GetHashCode(), key2.GetHashCode());
        }
Example #2
0
        public void should_refresh_cache_on_update()
        {
            var zh_CN            = CultureInfo.GetCultureInfo("zh-CN");
            var translationsInDb = new [] { CreateBrandTranslation(1, zh_CN, "Translated Brand1") };

            var underlyingStoreMock = new Mock <ITranslationStore>();

            underlyingStoreMock.Setup(it => it.Find(zh_CN, new[] { EntityKey.Create <Brand>(1) }))
            .Returns(translationsInDb);

            underlyingStoreMock.Setup(it => it.AddOrUpdate(zh_CN, EntityKey.Create <Brand>(1), It.IsAny <IEnumerable <PropertyTranslation> >()))
            .Callback(() =>
            {
                translationsInDb[0].PropertyTranslations[0].TranslatedText = "Translated Brand1 (Update)";
            });

            var store = new CachedTranslactionStore(underlyingStoreMock.Object, new [] { typeof(Brand) });

            var translation = store.Find(zh_CN, EntityKey.Create <Brand>(1));

            Assert.Equal("Translated Brand1", translation.PropertyTranslations[0].TranslatedText);

            store.AddOrUpdate(zh_CN, EntityKey.Create <Brand>(1), new List <PropertyTranslation>
            {
                new PropertyTranslation("Name", "Brand1", "Translated Brand1 (Update)")
            });

            Assert.Equal("Translated Brand1 (Update)", translationsInDb[0].PropertyTranslations[0].TranslatedText);

            translation = store.Find(zh_CN, EntityKey.Create <Brand>(1));
            Assert.Equal("Translated Brand1 (Update)", translation.PropertyTranslations[0].TranslatedText);
        }
Example #3
0
 private EntityTransaltion CreateBrandTranslation(int brandId, CultureInfo culture, string translatedName)
 {
     return(new EntityTransaltion(culture.Name, EntityKey.Create <Brand>(brandId), new List <PropertyTranslation>
     {
         new PropertyTranslation("Name", "Apple", translatedName)
     }));
 }
Example #4
0
        public void EntityKey_NullValues()
        {
            string expectedMsg = new ArgumentNullException("value", Resource.EntityKey_CannotBeNull).Message;
            ArgumentNullException expectedException = null;

            try
            {
                EntityKey.Create(new object[] { 5, null, "test" });
            }
            catch (ArgumentNullException e)
            {
                expectedException = e;
            }
            Assert.AreEqual(expectedMsg, expectedException.Message);
            expectedException = null;

            try
            {
                EntityKey.Create <int, string>(5, null);
            }
            catch (ArgumentNullException e)
            {
                expectedException = e;
            }
            Assert.AreEqual(expectedMsg, expectedException.Message);
            expectedException = null;
        }
Example #5
0
        public void EntityKey_TestHashCodeValues()
        {
            Guid g = Guid.NewGuid();

            object[] keyValues        = new object[] { 123, 34.5M, "hello", new DateTime(234234), g, false, '?' };
            object   key              = EntityKey.Create(keyValues);
            int      hashCode         = key.GetHashCode();
            int      expectedHashCode = 0;

            foreach (object keyValue in keyValues)
            {
                expectedHashCode ^= keyValue.GetHashCode();
            }
            Assert.AreEqual(expectedHashCode, hashCode);

            // test the non-boxed version and verify we get the same hashcode
            key      = EntityKey.Create(123, 34.5M, "hello", new DateTime(234234), g, false, '?');
            hashCode = key.GetHashCode();
            Assert.AreEqual(expectedHashCode, hashCode);

            // compute directly without boxing and verify equal
            int directlyComputed = ((int)123).GetHashCode() ^ ((decimal)34.5M).GetHashCode() ^ "hello".GetHashCode() ^
                                   new DateTime(234234).GetHashCode() ^ g.GetHashCode() ^ false.GetHashCode() ^ '?'.GetHashCode();

            Assert.AreEqual(directlyComputed, key.GetHashCode());
        }
Example #6
0
        public void can_cache_not_translated_items()
        {
            var zh_CN             = CultureInfo.GetCultureInfo("zh-CN");
            var brandKeys         = new[] { EntityKey.Create <Brand>(1), EntityKey.Create <Brand>(2) };
            var brandTranslations = new[] { CreateBrandTranslation(1, zh_CN, "Apple China"), null };

            var underlyingStoreMock = new Mock <ITranslationStore>();

            underlyingStoreMock.Setup(it => it.Find(zh_CN, brandKeys)).Returns(brandTranslations);

            var store = new CachedTranslactionStore(underlyingStoreMock.Object, new[] { typeof(Brand) });

            var actualResult = store.Find(zh_CN, brandKeys);

            Assert.NotNull(actualResult[0]);
            Assert.Equal("Apple China", actualResult[0].PropertyTranslations[0].TranslatedText);
            Assert.Null(actualResult[1]);

            actualResult = store.Find(zh_CN, brandKeys);
            Assert.NotNull(actualResult[0]);
            Assert.Equal("Apple China", actualResult[0].PropertyTranslations[0].TranslatedText);
            Assert.Null(actualResult[1]);

            underlyingStoreMock.Verify(it => it.Find(zh_CN, brandKeys), Times.Once());
        }
Example #7
0
        public void should_cache_by_culture()
        {
            var zh_CN = CultureInfo.GetCultureInfo("zh-CN");
            var nl_NL = CultureInfo.GetCultureInfo("nl-NL");

            var brandKey = EntityKey.Create <Brand>(15);

            var cnTranslation = CreateBrandTranslation(15, zh_CN, "Apple China");
            var nlTranslation = CreateBrandTranslation(15, nl_NL, "Apple Dutch");

            var underlyingStoreMock = new Mock <ITranslationStore>();

            underlyingStoreMock.Setup(it => it.Find(zh_CN, new[] { brandKey }))
            .Returns(new[] { cnTranslation });

            underlyingStoreMock.Setup(it => it.Find(nl_NL, new[] { brandKey }))
            .Returns(new[] { nlTranslation });

            var store = new CachedTranslactionStore(underlyingStoreMock.Object, new[] { typeof(Brand) });

            var translation = store.Find(zh_CN, brandKey);

            Assert.Equal("Apple China", translation.PropertyTranslations[0].TranslatedText);

            translation = store.Find(nl_NL, brandKey);
            Assert.Equal("Apple Dutch", translation.PropertyTranslations[0].TranslatedText);
        }
Example #8
0
        //Helper method that can be used to create composite primary keys
        /// <summary>Creates a primary key object for an entity identified by type. Use it for entity types with composite primary key, to create a key instance from
        /// column values. The created key may be used in session.GetEntity() method as primary key parameter. </summary>
        /// <typeparam name="TEntity">Entity type.</typeparam>
        /// <param name="session">The entity session.</param>
        /// <param name="values">Value(s) of the primary key columns.</param>
        /// <returns>A primary key object.</returns>
        public static object CreatePrimaryKey <TEntity>(this IEntitySession session, params object[] values)
        {
            Util.CheckParam(values, "values");
            var entInfo = session.Context.App.Model.GetEntityInfo(typeof(TEntity), throwIfNotFound: true);
            var pk      = EntityKey.Create(entInfo.PrimaryKey, values);

            return(pk);
        }
 public override object GetIdentity()
 {
     if (this._stationCode == null)
     {
         return(null);
     }
     return(EntityKey.Create <string, DateTime>(this._stationCode, this._timePoint));
 }
Example #10
0
 /// <summary>
 /// Computes a value from the key fields that uniquely identifies this entity instance.
 /// </summary>
 /// <returns>An object instance that uniquely identifies this entity instance.</returns>
 public override object GetIdentity()
 {
     if (((this._email == null) ||
          (this._userName == null)))
     {
         return(null);
     }
     return(EntityKey.Create(this._email, this._userName));
 }
 /// <summary>
 /// Computes a value from the key fields that uniquely identifies this entity instance.
 /// </summary>
 /// <returns>An object instance that uniquely identifies this entity instance.</returns>
 public override object GetIdentity()
 {
     if (((((this._email == null) ||
            (this._firstName == null)) ||
           (this._lastName == null)) ||
          (this._userName == null)))
     {
         return(null);
     }
     return(EntityKey.Create(this._country, this._dateOfBirth, this._email, this._firstName, this._gender, this._lastName, this._userName));
 }
Example #12
0
        public override object GetIdentity()
        {
            if (this._stationCode != null)
            {
                goto Label_000A;
            }
            return(null);

Label_000A:
            return(EntityKey.Create <string, DateTime>(this._stationCode, this._timePoint));
        }
Example #13
0
        public static EntityKey CreatePrimaryKeyInstance(this EntityInfo entity, object primaryKeyValue)
        {
            var pkType = primaryKeyValue.GetType();

            switch (primaryKeyValue)
            {
            case EntityKey key:
                return(key);

            case object[] arr:
                return(EntityKey.Create(entity.PrimaryKey, arr));

            default:
                return(EntityKey.Create(entity.PrimaryKey, new object[] { primaryKeyValue }));
            }
        }
Example #14
0
        public void should_fetch_from_cache_on_second_find()
        {
            var zh_CN = CultureInfo.GetCultureInfo("zh-CN");

            var underlyingStoreMock = new Mock <ITranslationStore>();

            underlyingStoreMock.Setup(it => it.Find(It.IsAny <CultureInfo>(), It.IsAny <EntityKey[]>()))
            .Returns(new[] { CreateBrandTranslation(1, zh_CN, "Apple Translated") });

            var store        = new CachedTranslactionStore(underlyingStoreMock.Object, new[] { typeof(Brand) });
            var translations = store.Find(zh_CN, new[] { EntityKey.Create <Brand>(1) });

            underlyingStoreMock.Verify(it => it.Find(zh_CN, new[] { EntityKey.Create <Brand>(1) }), Times.Once());

            translations = store.Find(zh_CN, new[] { EntityKey.Create <Brand>(1) });

            underlyingStoreMock.Verify(it => it.Find(zh_CN, new[] { EntityKey.Create <Brand>(1) }), Times.Once());
        }
Example #15
0
        public void should_only_cache_types_configured_to_be_cached()
        {
            var zh_CN = CultureInfo.GetCultureInfo("zh-CN");
            var underlyingStoreMock = new Mock <ITranslationStore>();

            underlyingStoreMock.Setup(it => it.Find(zh_CN, new[] { EntityKey.Create <Product>(1) }))
            .Returns(new[] {
                new EntityTransaltion("zh-CN", EntityKey.Create <Product>(1), new List <PropertyTranslation>
                {
                    new PropertyTranslation("Name", "Product1", "Translated Product1")
                })
            });

            var store = new CachedTranslactionStore(underlyingStoreMock.Object, new[] { typeof(Brand) });

            store.Find(zh_CN, new[] { EntityKey.Create <Product>(1) });
            store.Find(zh_CN, new[] { EntityKey.Create <Product>(1) });

            underlyingStoreMock.Verify(it => it.Find(CultureInfo.GetCultureInfo("zh-CN"), new[] { EntityKey.Create <Product>(1) }), Times.Exactly(2));
        }
Example #16
0
        public void should_combine_result_of_cached_and_uncached()
        {
            var zh_CN           = CultureInfo.GetCultureInfo("zh-CN");
            var allTranslations = new[] {
                CreateBrandTranslation(1, zh_CN, "Apple Translated"),
                CreateBrandTranslation(2, zh_CN, "Dell Translated"),
                CreateBrandTranslation(3, zh_CN, "Lenovo Translated")
            };

            var underlyingStoreMock = new Mock <ITranslationStore>();

            underlyingStoreMock.Setup(it => it.Find(zh_CN, It.IsAny <EntityKey[]>()))
            .Returns <CultureInfo, EntityKey[]>((culture, keys) =>
            {
                var result = new List <EntityTransaltion>();
                foreach (var key in keys)
                {
                    result.Add(allTranslations.First(it => it.EntityKey.Equals(key)));
                }

                return(result.ToArray());
            });

            var store = new CachedTranslactionStore(underlyingStoreMock.Object, new[] { typeof(Brand) });

            var translations = store.Find(zh_CN, EntityKey.Create <Brand>(1), EntityKey.Create <Brand>(2));

            Assert.NotNull(translations[0]);
            Assert.NotNull(translations[1]);
            Assert.Equal("Apple Translated", translations[0].PropertyTranslations[0].TranslatedText);
            Assert.Equal("Dell Translated", translations[1].PropertyTranslations[0].TranslatedText);

            translations = store.Find(zh_CN, EntityKey.Create <Brand>(1), EntityKey.Create <Brand>(3));
            Assert.NotNull(translations[0]);
            Assert.NotNull(translations[1]);
            Assert.Equal("Apple Translated", translations[0].PropertyTranslations[0].TranslatedText);
            Assert.Equal("Lenovo Translated", translations[1].PropertyTranslations[0].TranslatedText);
        }
Example #17
0
        public void should_fetch_from_underlying_store_if_not_cached()
        {
            var zh_CN = CultureInfo.GetCultureInfo("zh-CN");

            var underlyingStoreMock = new Mock <ITranslationStore>();

            underlyingStoreMock.Setup(it => it.Find(It.IsAny <CultureInfo>(), It.IsAny <EntityKey[]>()))
            .Returns(new[] { CreateBrandTranslation(1, zh_CN, "Apple Translated") });

            var store        = new CachedTranslactionStore(underlyingStoreMock.Object, new [] { typeof(Brand) });
            var translations = store.Find(zh_CN, new[] { EntityKey.Create <Brand>(1) });

            Assert.Equal(1, translations.Length);
            Assert.NotNull(translations[0]);
            Assert.Equal("Apple Translated", translations[0].PropertyTranslations[0].TranslatedText);

            underlyingStoreMock.Verify(it => it.Find(zh_CN, new[] { EntityKey.Create <Brand>(1) }), Times.Once());

            translations = store.Find(zh_CN, new[] { EntityKey.Create <Brand>(2) });

            underlyingStoreMock.Verify(it => it.Find(zh_CN, new[] { EntityKey.Create <Brand>(1) }), Times.Once());
            underlyingStoreMock.Verify(it => it.Find(zh_CN, new[] { EntityKey.Create <Brand>(2) }), Times.Once());
        }
Example #18
0
 public override object GetIdentity()
 {
     return(EntityKey.Create(ID1, ID2));
 }