public void Should_not_be_able_to_add_an_object_with_the_same_id_as_one_that_was_already_added() { IIdentityMap <IDomainObject> map = CreateSUT( ); int id = 1; map.Add(new DomainObject(id)); map.Add(new DomainObject(id)); }
public void Should_not_be_able_to_add_an_object_that_has_already_been_added() { IIdentityMap <IDomainObject> map = CreateSUT( ); IDomainObject addedObject = new DomainObject(1); map.Add(addedObject); map.Add(addedObject); }
public void Should_add_customer_to_identity_map_after_retrieving_from_mapper() { ICustomer customer = _mockery.DynamicMock <ICustomer>( ); long customerId = 23455; using (_mockery.Record( )) { SetupResult.For(_mockIdentityMap.ContainsObjectWithIdOf(customerId)).Return(false); SetupResult.For(_mockMapper.FindBy(customerId)).Return(customer); _mockIdentityMap.Add(customer); } using (_mockery.Playback( )) { Assert.AreEqual(customer, CreateSUT( ).FindBy(customerId)); } }
public void Should_be_able_to_retrieve_an_object_that_has_been_added_to_the_map() { IIdentityMap <IDomainObject> map = CreateSUT( ); IDomainObject objectAddedToMap = new DomainObject(1); map.Add(objectAddedToMap); Assert.AreEqual(objectAddedToMap, map.FindObjectWithIdOf(1)); }
public void Should_return_true_if_searching_for_a_() { IIdentityMap <IDomainObject> map = CreateSUT( ); int id = 1; map.Add(new DomainObject(id)); Assert.IsTrue(map.ContainsObjectWithIdOf(id)); Assert.IsFalse(map.ContainsObjectWithIdOf(2)); }
/// <summary> /// Get object T given the UID. /// </summary> /// <returns>The object T.</returns> /// <param name="UID">UID.</param> public virtual async Task <T> GetAsync(int UID) { T obj; if ((obj = _idMap.Find(UID)) != null) { return(obj); } SQLiteConnection db = await ConnectToTableAsync <T>(); return(await Task.Run(() => { obj = db.Get <T>(UID); _idMap.Add(obj.UID, obj); db.Close(); return obj; })); }
public void Should_be_able_to_add_a_new_domain_object_to_the_identitiy_map() { IDomainObject objectThatHasBeenAddedToMap = new DomainObject(1); IDomainObject objectThatHasNotBeenAddedToMap = new DomainObject(2); IIdentityMap <IDomainObject> map = CreateSUT( ); map.Add(objectThatHasBeenAddedToMap); Assert.IsTrue(map.ContainsObjectWithIdOf(objectThatHasBeenAddedToMap.ID( ))); Assert.IsFalse(map.ContainsObjectWithIdOf(objectThatHasNotBeenAddedToMap.ID( ))); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> protected virtual void ValidateData([NotNull] IModel model) { Check.NotNull(model, nameof(model)); var identityMaps = new Dictionary <IKey, IIdentityMap>(); var sensitiveDataLogged = Dependencies.Logger.ShouldLogSensitiveData(); foreach (var entityType in model.GetEntityTypes().Where(et => !et.IsQueryType)) { var key = entityType.FindPrimaryKey(); IIdentityMap identityMap = null; foreach (var seedDatum in entityType.GetData()) { foreach (var property in entityType.GetProperties()) { if (!seedDatum.TryGetValue(property.Name, out var value) || value == null) { if (!property.IsNullable && (!property.RequiresValueGenerator() || property.IsKey())) { throw new InvalidOperationException(CoreStrings.SeedDatumMissingValue(entityType.DisplayName(), property.Name)); } } else if (property.RequiresValueGenerator() && property.IsKey() && property.ClrType.IsDefaultValue(value)) { if (property.ClrType.IsSignedInteger()) { throw new InvalidOperationException(CoreStrings.SeedDatumSignedNumericValue(entityType.DisplayName(), property.Name)); } throw new InvalidOperationException(CoreStrings.SeedDatumDefaultValue(entityType.DisplayName(), property.Name, property.ClrType.GetDefaultValue())); } else if (!property.ClrType.GetTypeInfo().IsAssignableFrom(value.GetType().GetTypeInfo())) { if (sensitiveDataLogged) { throw new InvalidOperationException( CoreStrings.SeedDatumIncompatibleValueSensitive( entityType.DisplayName(), value, property.Name, property.ClrType.DisplayName())); } throw new InvalidOperationException( CoreStrings.SeedDatumIncompatibleValue( entityType.DisplayName(), property.Name, property.ClrType.DisplayName())); } } var keyValues = new object[key.Properties.Count]; for (var i = 0; i < key.Properties.Count; i++) { keyValues[i] = seedDatum[key.Properties[i].Name]; } foreach (var navigation in entityType.GetNavigations()) { if (seedDatum.TryGetValue(navigation.Name, out var value) && ((navigation.IsCollection() && value is IEnumerable collection && collection.Any()) || (!navigation.IsCollection() && value != null))) { if (sensitiveDataLogged) { throw new InvalidOperationException( CoreStrings.SeedDatumNavigationSensitive( entityType.DisplayName(), string.Join(", ", key.Properties.Select((p, i) => p.Name + ":" + keyValues[i])), navigation.Name, navigation.GetTargetType().DisplayName(), Property.Format(navigation.ForeignKey.Properties))); } throw new InvalidOperationException( CoreStrings.SeedDatumNavigation( entityType.DisplayName(), navigation.Name, navigation.GetTargetType().DisplayName(), Property.Format(navigation.ForeignKey.Properties))); } } if (identityMap == null) { if (!identityMaps.TryGetValue(key, out identityMap)) { identityMap = key.GetIdentityMapFactory()(sensitiveDataLogged); identityMaps[key] = identityMap; } } var entry = identityMap.TryGetEntry(keyValues); if (entry != null) { if (sensitiveDataLogged) { throw new InvalidOperationException( CoreStrings.SeedDatumDuplicateSensitive( entityType.DisplayName(), string.Join(", ", key.Properties.Select((p, i) => p.Name + ":" + keyValues[i])))); } throw new InvalidOperationException( CoreStrings.SeedDatumDuplicate( entityType.DisplayName(), Property.Format(key.Properties))); } entry = new InternalShadowEntityEntry(null, entityType); identityMap.Add(keyValues, entry); } } }
public void RegisterForTracking <TAggregate>(TAggregate aggregateRoot) where TAggregate : class, IOrginator, IEventProvider <TDomainEvent>, new() { _eventProviders.Add(aggregateRoot); _identityMap.Add(aggregateRoot); }