public string GetDescription(int error_code) { var unitOfWork = _unitOfWorkManager.GetUnitOfWork(); var repository = unitOfWork.Repository <IGenericRepository <ErrorDescription>, ErrorDescription>(); var error = repository.GetbyFilter(x => x.error_code == error_code, 0, 0).FirstOrDefault(); return((error != null) ? error.description : "Unidentified error"); }
/// <summary> /// 初始化一个<see cref="Repository{TEntity, TKey}"/>类型的新实例 /// </summary> public Repository(IUnitOfWorkManager unitOfWorkManager) { UnitOfWork = unitOfWorkManager.GetUnitOfWork <TEntity, TKey>(); _dbContext = (DbContext)UnitOfWork.GetDbContext <TEntity, TKey>(); _dbSet = _dbContext.Set <TEntity>(); _logger = ServiceLocator.Instance.GetLogger <Repository <TEntity, TKey> >(); }
/// <summary> /// 根据给定的事件批量获取聚合根,通过重演事件方式重新得到聚合根,并将聚合根添加到UnitOfWork进行管理 /// </summary> public IList <T> GetFromEvents <TAggregateRootEvent>(IEnumerable <TAggregateRootEvent> evnts) where TAggregateRootEvent : AggregateRootEvent { var aggregateRootList = new List <T>(); var aggregateRootType = typeof(T); var evntGroups = evnts.GroupBy(x => x.AggregateRootId).Where(x => x.Count() > 0); var unitOfWork = _unitOfWorkManager.GetUnitOfWork(); foreach (var evntGroup in evntGroups) { var aggregateRoot = DependencyResolver.Resolve <IAggregateRootFactory>().CreateAggregateRoot(aggregateRootType) as T; aggregateRoot.ReplayEvents(evntGroup); unitOfWork.TrackingAggregateRoot(aggregateRoot); aggregateRootList.Add(aggregateRoot); } return(aggregateRootList); }
/// <summary> /// 获取指定实体类型所在的上下文对象 /// </summary> public static IDbContext GetDbContext(this IUnitOfWorkManager unitOfWorkManager, Type entityType) { if (!entityType.IsEntityType()) { throw new OsharpException($"类型“{entityType}”不是实体类型"); } IUnitOfWork unitOfWork = unitOfWorkManager.GetUnitOfWork(entityType); return(unitOfWork?.GetDbContext(entityType)); }
/// <summary> /// 获取指定实体类的上下文所在工作单元 /// </summary> public static IUnitOfWork GetUnitOfWork <TEntity, TKey>(this IServiceProvider provider) where TEntity : IEntity <TKey> { IUnitOfWorkManager unitOfWorkManager = provider.GetService <IUnitOfWorkManager>(); return(unitOfWorkManager.GetUnitOfWork <TEntity, TKey>()); }
public bool AddDevice(string userId, DeviceAddDomainModel model) { var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(5)); DeviceExistenceResponseMessage deviceIsExist; try { var message = new DeviceVerificationMessage { serial_number = model.serial_number, CheckType = "EXISTENCE_IN_NEW" }; deviceIsExist = this._serviceBusSender.SendCallbackMessage <DeviceExistenceResponseMessage, DeviceVerificationMessage>(message, cancellationTokenSource.Token); } catch (OperationCanceledException) { _logger.LogError($"Failed to add a new device. DETAILS: Not received response about the existence of a registered device\n\t" + $"UserId: {userId}, deviceType: {model.type}, serial number: {model.serial_number}, device name: {model.name}"); throw new ResourceException(-203, 404); } if (!deviceIsExist.is_exist) { _logger.LogError($"Failed to add a new device. DETAILS: Not Found in mongo db\n\t" + $"UserId: {userId}, deviceType: {model.type}, serial number: {model.serial_number}, device name: {model.name}"); throw new ResourceException(-201, 404); } var unitOfWork = _unitOfWorkManager.GetUnitOfWork(); var deviceRepository = unitOfWork.Repository <IGenericRepository <Device>, Device>(); var deviceParameterBindingsRepository = unitOfWork.Repository <IGenericRepository <DeviceParameterBinding>, DeviceParameterBinding>(); var deviceTypesRepository = unitOfWork.Repository <IGenericRepository <DeviceType>, DeviceType>(); var type = deviceTypesRepository.GetbyFilter(x => x.description.ToLower() == deviceIsExist.device_info.type_text.ToLower(), 0, 0).FirstOrDefault(); if (type == null) { _logger.LogError($"Failed to add a new device. DETAILS: Unknown device type\n\t" + $"UserId: {userId}, deviceType: {model.type}, serial number: {model.serial_number}, device name: {model.name}"); throw new ResourceException(-204, 400); } if (type.device_type_id != model.type) { throw new ResourceException(-205, 400); // the type of device being added does not match the real device } Device device = new Device { serial_number = model.serial_number, name = model.name, device_type_id = model.type, user_id = userId, is_online = deviceIsExist.device_info.is_online, is_enable = false, mqtt_client_id = deviceIsExist.device_info.mqtt_client_id }; deviceRepository.Insert(device); var parameters = deviceParameterBindingsRepository.GetbyFilter(x => x.device_type_id == model.type, 0, 0).Select(x => x.Parameter); List <DeviceParameterValue> parameterValues = new List <DeviceParameterValue>(); foreach (var parameter in parameters) { parameterValues.Add(new DeviceParameterValue { date_on = DateTime.UtcNow, device_id = device.device_id, parameter_id = parameter.parameter_id, value = deviceIsExist.parameters.TryGetValue(parameter.description, out dynamic val) ? val : parameter.default_value });