protected void RegisterUnitTestType(UnitTestType unitTestType) { var fromDb = Context.UnitTestTypes.SingleOrDefault(x => x.Id == unitTestType.Id); if (fromDb == null) { Context.UnitTestTypes.Add(unitTestType); Context.SaveChanges(); } else { fromDb.SystemName = unitTestType.SystemName; fromDb.DisplayName = unitTestType.DisplayName; fromDb.IsSystem = unitTestType.IsSystem; Context.SaveChanges(); } }
public IUnitTestTypeCacheReadObject GetOrCreateUnitTestType(Guid accountId, GetOrCreateUnitTestTypeRequestData data) { if (data == null) { throw new ArgumentNullException("data"); } if (string.IsNullOrEmpty(data.SystemName)) { throw new ParameterRequiredException("SystemName"); } var lockObj = typeof(UnitTestTypeService); lock (lockObj) { var unitTestTypeCache = AllCaches.UnitTestTypes.FindByName(accountId, data.SystemName); if (unitTestTypeCache != null) { return(unitTestTypeCache); } var accountDbContext = Context.GetAccountDbContext(accountId); // Проверим лимит var checker = AccountLimitsCheckerManager.GetCheckerForAccount(accountId); var checkResult = checker.CheckMaxUnitTestTypes(accountDbContext); if (!checkResult.Success) { throw new OverLimitException(checkResult.Message); } Guid unitTestTypeId; // создаем отдельный контекст, чтобы после обработки объектов кэшем основной контекст загрузил актуальную версию из БД using (var accountDbContextNew = AccountDbContext.CreateFromAccountIdLocalCache(accountId)) { if (string.IsNullOrEmpty(data.DisplayName)) { data.DisplayName = data.SystemName; } var unitTestType = new UnitTestType() { SystemName = data.SystemName, DisplayName = data.DisplayName, IsSystem = false, ActualTimeSecs = data.ActualTimeSecs, NoSignalColor = data.NoSignalColor }; var unitTestTypeRepository = accountDbContextNew.GetUnitTestTypeRepository(); unitTestTypeRepository.Add(unitTestType); unitTestTypeId = unitTestType.Id; } checker.RefreshUnitTestTypesCount(); unitTestTypeCache = AllCaches.UnitTestTypes.Read(new AccountCacheRequest() { AccountId = accountId, ObjectId = unitTestTypeId }); return(unitTestTypeCache); } }