public void DesposeTest() { var test = new EntityReader<Foo>(Dummy.CreateDataReader(Dummy.Foos.ToArray()), Dummy.EntityMapping); var x = (IDisposable) test; bool wasClosed = test.IsClosed; x.Dispose(); Assert.True(test.IsClosed && !wasClosed); }
public void CountTest() { var reader = Dummy.CreateDataReader(Dummy.Foos.ToArray()); var mapping = Dummy.EntityMapping; var test = new EntityReader<Foo>(reader, mapping); int count = 0; while (test.Read()) { count++; } Assert.Equal(3, count); }
public async Task Event_EventDetail_Read() { var reader = new EntityReader <EventDetail>(); var testEntity = new EventDetail(); var lastKey = Defaults.Guid; await Event_EventDetail_Create(); lastKey = EventDetailTests.RecycleBin.LastOrDefault(); testEntity = reader.Read(x => x.Key == lastKey).FirstOrDefaultSafe(); Assert.IsTrue(!testEntity.IsNew); Assert.IsTrue(testEntity.Id != Defaults.Integer); Assert.IsTrue(testEntity.Key != Defaults.Guid); Assert.IsTrue(testEntity.CreatedDate.Date == DateTime.UtcNow.Date); }
private void mnuEditApprove_Click(object sender, EventArgs e) { Nullable <Guid> selectedId = UIHelper.GetSelectedRowId( grdDevicesPendingApproval, EntityReader <DevicePendingApproval> .GetPropertyName(p => p.DevicePendingApprovalId, true), true); DevicePendingApproval devicePendingApproval = _devicePendingApprovalCache[selectedId.Value]; using (ApproveDeviceForm f = new ApproveDeviceForm(devicePendingApproval)) { if (f.ShowDialog() == DialogResult.OK) { Refresh(true); } } }
public PublicHoliday GetPublicHoliday(Guid publicHolidayId, bool throwExceptionOnNotFound) { List <PublicHoliday> queryResult = (from p in DB.GetTable <PublicHoliday>() where p.PublicHolidayId == publicHolidayId select p).ToList(); PublicHoliday result = queryResult.Count < 1 ? null : queryResult.First(); if (result == null && throwExceptionOnNotFound) { throw new ArgumentNullException(string.Format("Could not find {0} with {1} of {2}.", typeof(PublicHoliday).Name, EntityReader <PublicHoliday> .GetPropertyName(p => p.PublicHolidayId, false), publicHolidayId)); } return(result); }
public async Task Resource_ResourceItem_Read() { var testEntity = new ResourceItem(); var lastKey = Defaults.Guid; await Resource_ResourceItem_Create(); lastKey = ResourceItemTests.RecycleBin.LastOrDefault(); testEntity = new EntityReader <ResourceItem>().GetByKey(lastKey); Assert.IsTrue(!testEntity.IsNew); Assert.IsTrue(testEntity.Id != Defaults.Integer); Assert.IsTrue(testEntity.Key != Defaults.Guid); Assert.IsTrue(testEntity.CreatedDate.Date == DateTime.UtcNow.Date); Assert.IsTrue(!testEntity.FailedRules.Any()); }
public void CreateEntity() { var expected = Dummy.Foos.ToArray(); var reader = Dummy.CreateDataReader(expected); var mapping = Dummy.EntityMapping; var test = new EntityReader<Foo>(reader, mapping); var actual = new List<Foo>(); while (test.Read()) { actual.Add(test.CreateEntity()); } Assert.Equal(expected, actual.ToArray()); }
public async Task Venture_VentureAppointment_Read() { var reader = new EntityReader <VentureAppointment>(); var testEntity = new VentureAppointment(); var lastKey = Defaults.Guid; await Venture_VentureAppointment_Create(); lastKey = VentureAppointmentTests.RecycleBin.LastOrDefault(); testEntity = reader.Read(x => x.Key == lastKey).FirstOrDefaultSafe(); Assert.IsTrue(!testEntity.IsNew); Assert.IsTrue(testEntity.Id != Defaults.Integer); Assert.IsTrue(testEntity.Key != Defaults.Guid); Assert.IsTrue(testEntity.CreatedDate.Date == DateTime.UtcNow.Date); }
public bool RefreshFromServer(Guid aplexDataTableId) { ServiceFunctionResultOfListOfAplexDataColumn result = GlobalDataCache.Instance.Service.GetAplexDataColumnByField( EntityReader <AplexDataColumn> .GetPropertyName(p => p.AplexDataTableId, false), aplexDataTableId, false, GlobalDataCache.Instance.CurrentUser); if (ServiceResultHandler.HandleServiceResult(result)) { return(true); } Clear(); result.Contents.ToList().ForEach(p => _entities.Add(p.AplexDataColumnId, p)); return(false); }
public async Task Resource_ResourceTimeRecurring_Read() { var reader = new EntityReader <ResourceTimeRecurring>(); var testEntity = new ResourceTimeRecurring(); var lastKey = Defaults.Guid; await Resource_ResourceTimeRecurring_Create(); lastKey = ResourceTimeRecurringTests.RecycleBin.LastOrDefault(); testEntity = reader.Read(x => x.Key == lastKey).FirstOrDefaultSafe(); Assert.IsTrue(!testEntity.IsNew); Assert.IsTrue(testEntity.Id != Defaults.Integer); Assert.IsTrue(testEntity.Key != Defaults.Guid); Assert.IsTrue(testEntity.CreatedDate.Date == DateTime.UtcNow.Date); }
public async Task Person_PersonInfo_Read() { var testEntity = new PersonInfo(); var lastKey = Defaults.Guid; await Person_PersonInfo_Create(); lastKey = PersonInfoTests.RecycleBin.LastOrDefault(); testEntity = new EntityReader <PersonInfo>().GetByKey(lastKey); Assert.IsTrue(!testEntity.IsNew); Assert.IsTrue(testEntity.Id != Defaults.Integer); Assert.IsTrue(testEntity.Key != Defaults.Guid); Assert.IsTrue(testEntity.CreatedDate.Date == DateTime.UtcNow.Date); Assert.IsTrue(!testEntity.FailedRules.Any()); }
public async Task Schedule_SlotLocation_Read() { var reader = new EntityReader <SlotLocation>(); var testEntity = new SlotLocation(); var lastKey = Defaults.Guid; await Schedule_SlotLocation_Create(); lastKey = SlotLocationTests.RecycleBin.LastOrDefault(); testEntity = reader.Read(x => x.Key == lastKey).FirstOrDefaultSafe(); Assert.IsTrue(!testEntity.IsNew); Assert.IsTrue(testEntity.Id != Defaults.Integer); Assert.IsTrue(testEntity.Key != Defaults.Guid); Assert.IsTrue(testEntity.CreatedDate.Date == DateTime.UtcNow.Date); }
private static void ApproveDevicePendingApprovalVisitUs(string deviceId, string applicationName, string companyName) { Console.Write("Approving device pending approval for Visit Us... "); //string applicationWebServiceURL = "http://41.86.108.77/PebbleRock.CarTracker.Web.Service/Service.asmx"; //string applicationReplicationWebServiceURL = "http://41.86.108.77/PebbleRockMobileSync/sqlcesa35.dll"; //string applicationWebServiceURL = "http://41.86.108.77/Caribbean.CarTracker.Web.Service/Service.asmx"; //string applicationReplicationWebServiceURL = "http://41.86.108.77/CaribbeanRockMobileSync/sqlcesa35.dll"; //string applicationWebServiceURL = "http://41.86.108.77/Monaghan.VisitUs.Web.Service/Service.asmx"; //string applicationReplicationWebServiceURL = null; //string FiglutWebServiceURL = "http://41.76.214.230/Figlut.Web.Service/Service.asmx"; //string applicationWebServiceURL = "http://41.86.108.77/EcoPark.CarTracker.Web.Service/Service.asmx"; //string applicationReplicationWebServiceURL = "http://41.86.108.77/EcoPark.CarTracker.Web.Service/SyncService.svc"; //string FiglutWebServiceURL = "http://184.22.83.112/Figlut.Web.Service/Service.asmx"; string applicationWebServiceURL = "http://41.86.108.77/Dainfern.CarTracker.Web.Service/Service.asmx"; string applicationReplicationWebServiceURL = "http://41.86.108.77/Dainfern.CarTracker.Web.Service/SyncService.svc"; string FiglutWebServiceURL = "http://41.76.214.230/Figlut.Web.Service/Service.asmx"; ServiceFunctionResultOfListOfCustomer resultCustomer = GlobalDataCache.Instance.Service.GetCustomerByField( EntityReader <Customer> .GetPropertyName(p => p.CompanyName, false), companyName, false, GlobalDataCache.Instance.CurrentUser); if (resultCustomer.Code != ServiceResultCode.Success) { throw new Exception(resultCustomer.Message); } Customer customer = resultCustomer.Contents[0]; ServiceProcedureResult approvalResult = GlobalDataCache.Instance.Service.ApproveDevicePendingApproval( deviceId, applicationName, applicationWebServiceURL, applicationReplicationWebServiceURL, FiglutWebServiceURL, 730, customer, GlobalDataCache.Instance.CurrentUser); if (approvalResult.Code != ServiceResultCode.Success) { throw new Exception(approvalResult.Message); } Console.WriteLine("done"); }
public IActionResult Get(string key = "-1", string firstName = "", string lastName = "") { var model = new CustomerSearchModel() { Id = key.TryParseInt32(), Key = key.TryParseGuid(), FirstName = firstName, LastName = lastName }; var reader = new EntityReader <CustomerInfo>(); var searchResults = reader.GetByWhere(x => x.Key == model.Key || x.FirstName.Contains(model.FirstName) || x.LastName.Contains(model.LastName) || x.BirthDate == model.BirthDate); if (searchResults.Any()) { model.Results.FillRange(searchResults); } return(Ok(model)); }
public virtual async Task <IHttpActionResult> Get([FromUri] string fields) { try { if (string.IsNullOrEmpty(fields)) { return(Ok(EntityReader.GetIncludeList())); } var entities = await EntityReader.GetAllAsync(fields.Split(',')); return(Ok(Mapper.Map <IEnumerable <TModel> >(entities))); } catch (Exception ex) { return(InternalServerError(ex)); } }
public virtual async Task <IHttpActionResult> GetById(int id, [FromUri] string fields) { try { var entity = await EntityReader.GetById(id, fields.Split(',')); if (entity == null) { return(NotFound()); } return(Ok(Mapper.Map <TModel>(entity))); } catch (Exception ex) { return(InternalServerError(ex)); } }
public ActionResult Edit(CustomerModel model) { var reader = new EntityReader <CustomerInfo>(); var customer = model.CastOrFill <CustomerInfo>(); customer = customer.Save(); if (!customer.IsNew) { TempData[ResultMessage] = "Successfully saved"; } else { TempData[ResultMessage] = "Failed to save"; } return(View(CustomerController.SummaryView, customer.CastOrFill <CustomerModel>())); }
public ActionResult Delete(CustomerModel model) { var reader = new EntityReader <CustomerInfo>(); var customer = reader.GetByKey(model.Key); customer = customer.Delete(); if (customer.IsNew) { TempData[ResultMessage] = "Successfully deleted"; } else { TempData[ResultMessage] = "Failed to delete"; } return(View(CustomerSearchController.SearchView, customer.CastOrFill <CustomerSearchModel>())); }
public async Task Schedule_ScheduleInfo_Read() { var reader = new EntityReader <ScheduleInfo>(); var testEntity = new ScheduleInfo(); var lastKey = Defaults.Guid; await Schedule_ScheduleInfo_Create(); lastKey = ScheduleInfoTests.RecycleBin.LastOrDefault(); testEntity = reader.Read(x => x.Key == lastKey).FirstOrDefaultSafe(); Assert.IsTrue(!testEntity.IsNew); Assert.IsTrue(testEntity.Id != Defaults.Integer); Assert.IsTrue(testEntity.Key != Defaults.Guid); Assert.IsTrue(testEntity.CreatedDate.Date == DateTime.UtcNow.Date); Assert.IsTrue(!testEntity.FailedRules.Any()); }
private void mnuEditDelete_Click(object sender, EventArgs e) { Nullable <Guid> selectedId = UIHelper.GetSelectedRowId( grdCustomers, EntityReader <Customer> .GetPropertyName(p => p.CustomerId, true), true); Customer customer = _customerCache[selectedId.Value]; if (UIHelper.AskQuestion( "Deleting a customer will delete all the data associated with this customer. Are you sure you want to delete the selected customer?") != DialogResult.Yes) { return; } _customerCache.Delete(selectedId.Value); Refresh(false); _unsavedChanges = true; }
public void RefreshGrid(Guid idToSelect) { RefreshGrid(); foreach (DataGridViewRow r in grdDeviceConfigs.Rows) { r.Selected = false; } foreach (DataGridViewRow r in grdDeviceConfigs.Rows) { DataRow drv = ((DataRowView)(r.DataBoundItem)).Row; Guid currentId = new Guid(drv[EntityReader <DeviceConfig> .GetPropertyName(p => p.DeviceConfigId, true)].ToString()); if (currentId == idToSelect) { r.Selected = true; return; } } }
public override void AddColumnsByEntityType(Type entityType) { foreach (PropertyInfo p in entityType.GetProperties()) { if (p.PropertyType == typeof(IntPtr) || p.PropertyType == typeof(UIntPtr) || (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(List <>))) { continue; } SqliteDatabaseTableColumnWindows c = new SqliteDatabaseTableColumnWindows(); c.ColumnName = p.Name; c.DataType = SqliteTypeConverterWindows.Instance.GetSqlTypeNameFromDotNetType( p.PropertyType, EntityReader.IsTypeIsNullable(p.PropertyType)); _columns.Add(c); } }
public IEnumerable <IPerson> Get(string idOrKey, string firstName, string lastName) { var returnValue = new List <PersonInfo>(); var id = idOrKey.TryParseInt32(); var key = idOrKey.TryParseGuid(); using (var entityReader = new EntityReader <PersonInfo>()) { var searchResults = entityReader.GetByWhere(x => x.FirstName.Contains(firstName) || x.LastName.Contains(lastName) || x.Key == key || x.Id == id).Take(MaxRecords); if (searchResults.Any()) { returnValue.FillRange(searchResults); } } return(returnValue); }
public void Core_Data_EntityReader_GetByPage() { var reader = new EntityReader <CustomerInfo>(new ConnectionStringFactory().GetDefaultConnection()); var item = new CustomerInfo(); var pageCount = 3; var pageCurrent = 1; var testKeys = reader.GetAll().Take(10).Select(x => x.Key); var firstSet = reader.GetByPage(x => testKeys.Contains(x.Key), y => y.Id, pageCount, pageCurrent).ToList(); Assert.IsTrue(firstSet.Count() == pageCount); Assert.IsTrue(firstSet.First().Id <= firstSet.Last().Id); pageCurrent += 1; var secondSet = reader.GetByPage(x => testKeys.Contains(x.Key), y => y.Id, pageCount, pageCurrent).ToList(); Assert.IsTrue(secondSet.Count() == pageCount); Assert.IsTrue(secondSet.First().Id <= secondSet.Last().Id); Assert.IsTrue(firstSet.Last().Id <= secondSet.First().Id); }
public User GetUser(Guid userId, bool throwExceptionOnNotFound) { List <User> queryResult = (from u in DB.GetTable <User>() where u.UserId == userId orderby u.UserName select u).ToList(); User result = queryResult.Count < 1 ? null : queryResult.First(); if (throwExceptionOnNotFound && result == null) { throw new NullReferenceException(string.Format( "Could not find {0} with {1} of '{2}'.", typeof(User).Name, EntityReader <User> .GetPropertyName(p => p.UserId, false), userId.ToString())); } return(result); }
public void RefreshGrid() { string originalStatus = Status; try { int selectedRowIndex = -1; if (grdDeviceConfigs.SelectedRows.Count > 0) { selectedRowIndex = grdDeviceConfigs.SelectedRows[0].Index; } if (_filtersEnabled) { Dictionary <string, object> properties = new Dictionary <string, object>(); properties.Add(EntityReader <DeviceConfig> .GetPropertyName(p => p.DeviceId, false), txtFilterDeviceId.Text); properties.Add(EntityReader <DeviceConfig> .GetPropertyName(p => p.ApplicationName, false), txtFilterApplicationName.Text); grdDeviceConfigs.DataSource = _deviceConfigCache.GetDataTable( properties, false, true, chkFilterExpired.Checked, _filteredDeviceConfigCache); //After this call the filtered cache will only contain the items included while the filtered was applied. } else { grdDeviceConfigs.DataSource = _deviceConfigCache.GetDataTable(null, false, true); _filteredDeviceConfigCache.Clear(); _deviceConfigCache.ToList().ForEach(p => _filteredDeviceConfigCache.Add(p)); } _hiddenColumns.ForEach(c => grdDeviceConfigs.Columns[c].Visible = false); if (selectedRowIndex < grdDeviceConfigs.Rows.Count && selectedRowIndex > -1) { grdDeviceConfigs.Rows[selectedRowIndex].Selected = true; } grdDeviceConfigs.Refresh(); } finally { if (Status != originalStatus) { Status = originalStatus; } } }
public void EntityReader_ShouldDeserializeEntity() { // Arrange var expectedEntity = new Entity { Id = 1, Name = "Test Name", Type = EntityType.NotAwesomeType }; var reader = new EntityReader(); // Act var result = reader.ReadEntity(_json); // Assert Assert.AreEqual(expectedEntity.Id, result.Id); Assert.AreEqual(expectedEntity.Name, result.Name); Assert.AreEqual(expectedEntity.Type, result.Type); }
public User GetUser(string userName, bool caseSensitive, bool throwExceptionOnNotFound) { string userNameToSearch = caseSensitive ? userName : userName.Trim().ToLower(); List <User> queryResult = (from u in DB.GetTable <User>() where u.UserName.ToLower() == userNameToSearch orderby u.UserName select u).ToList(); User result = queryResult.Count < 1 ? null : queryResult.First(); if (throwExceptionOnNotFound && result == null) { throw new NullReferenceException(string.Format( "Could not find {0} with {1} of '{2}'.", typeof(User).Name, EntityReader <User> .GetPropertyName(p => p.UserName, false), userNameToSearch)); } return(result); }
/// <summary> /// Queries for and returns an entity from the table corresponding to the entity type. The query is performed /// on the surrogate key of the entity. /// </summary> /// <typeparam name="E">The entity type i.e. which table the entity will be queried from.</typeparam> /// <param name="keyValue">The value of the surrogate to search by.</param> /// <param name="loadChildren">Whether or not to load the children entities onto this entity.</param> /// <param name="throwExceptionOnNotFound">Whether or not to throw an exception if the result is null.</param> /// <returns>Returns an entity with the specified type and surrogate key. Returns null if one is not found.</returns> public virtual E GetEntityBySurrogateKey <E>(object keyValue, bool loadChildren, bool throwExceptionOnNotFound) where E : class { SetDeferredLoadingEnabled(loadChildren); Type entityType = typeof(E); PropertyInfo surrogateKey = GetEntitySurrogateKey(entityType); object keyValueConverted = EntityReader.ConvertValueTypeTo(keyValue, surrogateKey.PropertyType); ParameterExpression e = Expression.Parameter(entityType, "e"); MemberExpression memberExpression = Expression.MakeMemberAccess(e, surrogateKey); //Name of surrogate key : left hand side of the expression. ConstantExpression constantExpression = Expression.Constant(keyValueConverted, surrogateKey.PropertyType); //Value of the surrogate key : right hand side of the expression. BinaryExpression binaryExpression = Expression.Equal(memberExpression, constantExpression); Expression <Func <E, bool> > lambdaExpression = Expression.Lambda <Func <E, bool> >(binaryExpression, e); E result = DB.GetTable <E>().SingleOrDefault(lambdaExpression); if (result == null && throwExceptionOnNotFound) { throw new NullReferenceException($"{nameof(GetEntityBySurrogateKey)} could not find entity {typeof(E).Name} with key value of '{keyValue}'."); } return(result); }
public PublicHoliday GetPublicHoliday(string countryCode, string dateIdentifier, bool throwExceptionOnNotFound) { string countryCodeLower = countryCode.ToLower(); List <PublicHoliday> queryResult = (from p in DB.GetTable <PublicHoliday>() where p.CountryCode == countryCodeLower && p.DateIdentifier == dateIdentifier select p).ToList(); PublicHoliday result = queryResult.Count < 1 ? null : queryResult.First(); if (result == null && throwExceptionOnNotFound) { throw new ArgumentNullException(string.Format("Could not find {0} with {1} of {2} and {3} of {4}.", typeof(PublicHoliday).Name, EntityReader <PublicHoliday> .GetPropertyName(p => p.CountryCode, false), countryCode, EntityReader <PublicHoliday> .GetPropertyName(p => p.DateIdentifier, false), dateIdentifier)); } return(result); }
public async Task <IEnumerable <AubEvent> > GetEvents(int tenantId, string filter = null) { var apiContext = new ApiContext(tenantId); var reader = new EntityReader <AubEvent> { Context = apiContext, Filter = filter, ListName = _listFullName, Namespace = _listNameSpace, PageSize = 200 }; var entities = new List <AubEvent>(); while (await reader.ReadAsync().ConfigureAwait(false)) { entities.AddRange(reader.Items); } return(entities); }
public async Task Schedule_ScheduleInfo_Update() { var reader = new EntityReader <ScheduleInfo>(); var resultEntity = new ScheduleInfo(); var testEntity = new ScheduleInfo(); var uniqueValue = Guid.NewGuid().ToString().Replace("-", ""); var lastKey = Defaults.Guid; var originalId = Defaults.Integer; var originalKey = Defaults.Guid; await Schedule_ScheduleInfo_Create(); lastKey = ScheduleInfoTests.RecycleBin.LastOrDefault(); testEntity = reader.Read(x => x.Key == lastKey).FirstOrDefaultSafe(); originalId = testEntity.Id; originalKey = testEntity.Key; Assert.IsTrue(!testEntity.IsNew); Assert.IsTrue(testEntity.Id != Defaults.Integer); Assert.IsTrue(testEntity.Key != Defaults.Guid); Assert.IsTrue(!testEntity.FailedRules.Any()); testEntity.Name = uniqueValue; using (var writer = new EntityWriter <ScheduleInfo>(testEntity, new ScheduleInfoSPConfig())) { resultEntity = await writer.SaveAsync(); } Assert.IsTrue(!resultEntity.IsNew); Assert.IsTrue(resultEntity.Id != Defaults.Integer); Assert.IsTrue(resultEntity.Key != Defaults.Guid); Assert.IsTrue(testEntity.Id == resultEntity.Id && resultEntity.Id == originalId); Assert.IsTrue(testEntity.Key == resultEntity.Key && resultEntity.Key == originalKey); Assert.IsTrue(!testEntity.FailedRules.Any()); testEntity = reader.Read(x => x.Id == originalId).FirstOrDefaultSafe(); Assert.IsTrue(!testEntity.IsNew); Assert.IsTrue(testEntity.Id == resultEntity.Id && resultEntity.Id == originalId); Assert.IsTrue(testEntity.Key == resultEntity.Key && resultEntity.Key == originalKey); Assert.IsTrue(testEntity.Id != Defaults.Integer); Assert.IsTrue(testEntity.Key != Defaults.Guid); Assert.IsTrue(!testEntity.FailedRules.Any()); }
public async Task MixedActionWriteAsync() { var connection = TestConfiguration.GetConnection(); var entityCollection = new EntityCollection <TestModel>(); var pipeline = new EntityWriterPipeline <TestModel>(connection); var reader = new EntityReader <TestModel>(connection); pipeline.AddCollection(entityCollection); var updateEntity = new TestModel { Title = "EntityWriterPipelineTests.MixedActionWriteAsync-UpdateEntity" }; var deleteEntity = new TestModel { Title = "EntityWriterPipelineTests.MixedActionWriteAsync-DeleteEntity" }; entityCollection.Update(updateEntity, EntityEntryState.Added); entityCollection.Update(deleteEntity, EntityEntryState.Added); await pipeline.WriteAsync().ConfigureAwait(false); entityCollection.Clear(); var addedEntity = new TestModel { Title = "EntityWriterPipelineTests.MixedActionWriteAsync-AddEntity" }; updateEntity.Title = "EntityWriterPipelineTests.MixedActionWriteAsync-UpdateEntity-Updated"; entityCollection.Update(addedEntity, EntityEntryState.Added); entityCollection.Update(updateEntity, EntityEntryState.Updated); entityCollection.Update(deleteEntity, EntityEntryState.Deleted); await pipeline.WriteAsync().ConfigureAwait(false); Assert.IsTrue(reader.AsQueryable().Where(e => e.Id == addedEntity.Id).Any()); Assert.IsFalse(reader.AsQueryable().Where(e => e.Id == deleteEntity.Id).Any()); var dbEntity = reader.AsQueryable().Where(e => e.Id == updateEntity.Id).FirstOrDefault(); Assert.AreEqual("EntityWriterPipelineTests.MixedActionWriteAsync-UpdateEntity-Updated", dbEntity.Title); }
/// <summary> /// <para> /// Overloaded. Attempts to read an entity from the cache. If it is not found then the <paramref name="reader"/> /// is used to retrieve a fresh copy of the entity. /// </para> /// </summary> /// <param name="identity"> /// A <see cref="IIdentity"/> /// </param> /// <param name="reader"> /// A <see cref="EntityReader"/> /// </param> /// <returns> /// A <see cref="IEntity"/> /// </returns> public IEntity Read (IIdentity identity, EntityReader reader) { return this.Read(identity, this.DefaultItemPolicy, reader); }
/// <summary> /// <para> /// Overloaded. Attempts to read an entity from the cache. If it is not found then the <paramref name="reader"/> /// is used to retrieve a fresh copy of the entity. If a fresh copy is retrieved, the given /// <paramref name="itemPolicy"/> is used to provide additional control over the entity's lifetime in the cache. /// </para> /// </summary> /// <remarks> /// <para> /// This is the core method that attempts to read entities from this cache instance and then either returns them /// from the <see cref="BackingStore"/> or reads them in afresh using the <paramref name="reader"/>. When entities /// are retrieved using the reader they are implicitly added to the cache as part of the action. This method can /// result in two core outcomes: /// </para> /// <list type="table"> /// <item> /// <term>Cache hit</term> /// <description> /// The desired entity was found within the cache and is suitable to be returned directly from the cache. /// </description> /// </item> /// <item> /// <term>Cache miss</term> /// <description> /// The desired entity was either not found within the cache or a policy deemed that is must not be returned from /// the cache. The entity is fetched using the <paramref name="reader"/>, added to the cache and then returned. /// </description> /// </item> /// </list> /// </remarks> /// <param name="identity"> /// A <see cref="IIdentity"/> /// </param> /// <param name="itemPolicy"> /// A <see cref="IItemPolicy"/> /// </param> /// <param name="reader"> /// A <see cref="EntityReader"/> /// </param> /// <returns> /// A <see cref="IEntity"/> /// </returns> public IEntity Read (IIdentity identity, IItemPolicy itemPolicy, EntityReader reader) { IEntity output; if(identity == null) { throw new ArgumentNullException("identity"); } else if(reader == null) { throw new ArgumentNullException("reader"); } try { this.SyncRoot.EnterUpgradeableReadLock(); if(this.IsCacheHit(identity)) { // This is a cache hit, downgrade our lock immediately and return the entity from the cache this.SyncRoot.EnterReadLock(); this.SyncRoot.ExitUpgradeableReadLock(); this.OnCacheHit(identity); output = this.BackingStore.Read(identity); } else { // This is a cache miss, upgrade our lock and go get the entity from the reader this.SyncRoot.EnterWriteLock(); this.OnCacheMiss(identity); output = reader(identity); this.LockedAdd(output, itemPolicy); } } finally { if(this.SyncRoot.IsWriteLockHeld) { this.SyncRoot.ExitWriteLock(); } if(this.SyncRoot.IsUpgradeableReadLockHeld) { this.SyncRoot.ExitUpgradeableReadLock(); } if(this.SyncRoot.IsReadLockHeld) { this.SyncRoot.ExitReadLock(); } } return output; }