protected virtual void SettingIfSysData(IDto dto) { if ((dto.GetType().GetProperty("SystemData")) != null) { string systemDataValue = CommonMethod.ParseString(dto.GetType().GetProperty("SystemData").GetValue(dto, null)); if (this.IsAuthority(CommonData.OperId.Edit) == CommonData.IsAuthority.Allow) { btnClear.Enabled = systemDataValue.Equals(CommonData.SystemData.Yes) ? false : true; } } }
protected virtual bool IsFormChanged() { if (this.ListDto != null) { IDto oldDto = this.CurrentDto; IDto newDto = this.Dto; Type dtoType = oldDto.GetType(); System.Reflection.PropertyInfo[] properties = dtoType.GetProperties(); foreach (System.Reflection.PropertyInfo property in properties) { object oldVar = dtoType.GetProperty(property.Name).GetValue(oldDto, null); object newVar = dtoType.GetProperty(property.Name).GetValue(newDto, null); if (oldVar == null && newVar == null) { continue; } if (oldVar != null && newVar == null) { return(true); } if (oldVar == null && newVar != null) { return(true); } if (!oldVar.Equals(newVar)) { return(true); } } } return(false); }
/// <summary> /// The table name. /// </summary> /// <param name="dto"> /// The dto. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public static Tuple <string, int> TableNameColumnCount(this IDto dto) { var tableName = ((TableNameAttribute)dto.GetType().GetCustomAttribute(typeof(TableNameAttribute), false)).Value; var columnCount = dto.GetColumnProperties().Count(); return(new Tuple <string, int>(tableName, columnCount)); }
void _referencedObjectDisposed(object o, EventArgs a) { IDto dto = o as IDto; if (dto == null) { return; } var delegatesToRemove = _delegates.Keys.Where(k => k.Item1 == dto.DtoGuid); foreach (var dk in delegatesToRemove) { Delegate delegateToRemove; if (_delegates.TryRemove(dk, out delegateToRemove)) { EventInfo ei = dto.GetType().GetEvent(dk.Item2); ei.RemoveEventHandler(dto, delegateToRemove); Debug.WriteLine($"Server: Delegate {dk.Item2} on {dto} removed;"); } } WebSocketMessage message = new WebSocketMessage() { DtoGuid = dto.DtoGuid, MessageType = WebSocketMessage.WebSocketMessageType.ObjectDisposed, #if DEBUG DtoName = dto.ToString(), #endif }; Send(_serialize(message)); Debug.WriteLine($"Server: ObjectDisposed notification on {dto} sent"); }
public static TViewModel ToViewModel <TViewModel>(this IDto dto, IMapper mapper) where TViewModel : IViewModel { var viewModel = mapper.Map <TViewModel>(dto); if (viewModel == null) { throw new ViewModelDtoMappingException($"Missing map from {dto.GetType()} to {typeof(TViewModel)}"); } return(viewModel); }
public static TEntity ToEntity <TEntity>(this IDto dto, IMapper mapper) where TEntity : IEntity { var entity = mapper.Map <TEntity>(dto); if (entity == null) { throw new DomainDtoMappingException($"Missing map from {dto.GetType()} to {typeof(TEntity)}"); } return(entity); }
public virtual object[] GetKeys(IDto dto) { if (dto == null) { throw new ArgumentNullException(nameof(dto)); } TypeInfo dtoType = dto.GetType().GetTypeInfo(); PropertyInfo[] props = GetKeyColums(dtoType); return(props.Select(p => p.GetValue(dto)).ToArray() !); }
private void Save(IDto state, Action onSuccess) { var stopwatch = Stopwatch.StartNew(); Persist(state, _ => { Logger.Info("State {Type} persisted in {Duration}ms.", state.GetType().Name, stopwatch.ElapsedMilliseconds); _state = _state.Update(state); onSuccess.Invoke(); if (LastSequenceNr % SnapshotInterval == 0 && LastSequenceNr != 0) { SaveSnapshot(_state.Events.ToProtobufContracts()); } }); }
public void CreateData() { using (client = new HttpClient()) { var jsonstr = JsonConvert.SerializeObject(new { name = _dto.Name }); var value = new StringContent(jsonstr, Encoding.UTF8, "application/json"); var url = "http://localhost:10412/api/"; if (_dto is MainBody) { url += $"{_dto.GetType().Name}/"; } else if (_dto is Document) { var doc = _dto as Document; url += $"/domains/{doc.DomainId}/documents"; } else { url += $"{_dto.GetType().Name}s/"; } var res = client.PostAsync(url, value); try { res.Result.EnsureSuccessStatusCode(); //MessageBox.Show("Data successfully registered to API"); } catch (Exception e) { MessageBox.Show(e.ToString()); } } //var value = JsonConvert.SerializeObject($"name: {_documentName}"); //var content = new StringContent(value, Encoding.UTF8, "application/json"); //var response = await _client.PostAsync("http://localhost:10412/api/domains/" + _domainId + "/documents", content); }
protected override void OnClose(CloseEventArgs e) { foreach (delegateKey d in _delegates.Keys) { IDto havingDelegate = _referenceResolver.ResolveReference(d.Item1); if (havingDelegate != null) { EventInfo ei = havingDelegate.GetType().GetEvent(d.Item2); _removeDelegate(havingDelegate, ei); } } _referenceResolver.ReferencePropertyChanged -= _referenceResolver_ReferencePropertyChanged; _referenceResolver.ReferenceDisposed -= _referencedObjectDisposed; _referenceResolver.Dispose(); Debug.WriteLine("Server: connection closed."); Logger.Info("Connection closed."); base.OnClose(e); }
private void NotifyClient(IDto dto, EventArgs e, string eventName) { //Debug.Assert(_referenceResolver.ResolveReference(dto.DtoGuid) != null, "Null reference notified"); try { if (e is WrappedEventArgs ea && ea.Args is PropertyChangedEventArgs propertyChangedEventArgs && eventName == nameof(INotifyPropertyChanged.PropertyChanged)) { var p = dto.GetType().GetProperty(propertyChangedEventArgs.PropertyName); PropertyChangedValueReader valueReader; if (p?.CanRead == true) { valueReader = new PropertyChangedValueReader(propertyChangedEventArgs.PropertyName, () => p.GetValue(dto, null)); } else { valueReader = new PropertyChangedValueReader(propertyChangedEventArgs.PropertyName, () => null); Debug.WriteLine(dto, $"{GetType()}: Couldn't get value of {propertyChangedEventArgs.PropertyName}"); } Debug.WriteLine($"Server: PropertyChanged {propertyChangedEventArgs.PropertyName} on {dto} sent"); Send(new SocketMessage(valueReader) { MessageType = SocketMessage.SocketMessageType.EventNotification, DtoGuid = dto.DtoGuid, MemberName = eventName, }); } else { Send(new SocketMessage(e) { MessageType = SocketMessage.SocketMessageType.EventNotification, DtoGuid = dto.DtoGuid, MemberName = eventName, }); } }
private void NotifyClient(IDto dto, EventArgs e, string eventName) { try { if (e is WrappedEventArgs ea && ea.Args is PropertyChangedEventArgs propertyChangedEventArgs && eventName == nameof(INotifyPropertyChanged.PropertyChanged)) { var p = dto.GetType().GetProperty(propertyChangedEventArgs.PropertyName); PropertyChangedWithValueEventArgs value; if (p?.CanRead == true) { value = new PropertyChangedWithValueEventArgs(propertyChangedEventArgs.PropertyName, p.GetValue(dto, null)); } else { value = new PropertyChangedWithValueEventArgs(propertyChangedEventArgs.PropertyName, null); Debug.WriteLine(dto, $"{GetType()}: Couldn't get value of {propertyChangedEventArgs.PropertyName}"); } Debug.WriteLine($"Server: PropertyChanged {propertyChangedEventArgs.PropertyName} on {dto} sent"); Send(new SocketMessage(value) { MessageType = SocketMessage.SocketMessageType.EventNotification, DtoGuid = dto.DtoGuid, MemberName = eventName, }); } else { Send(new SocketMessage(e) { MessageType = SocketMessage.SocketMessageType.EventNotification, DtoGuid = dto.DtoGuid, MemberName = eventName }); } }
protected virtual bool IsFormChanged() { if (this.ListDto != null) { IDto oldDto = this.CurrentDto; IDto newDto = this.Dto; Type dtoType = oldDto.GetType(); System.Reflection.PropertyInfo[] properties = dtoType.GetProperties(); // Start Update by Trong on 20130801 for DNP project foreach (System.Reflection.PropertyInfo property in properties) { object oldVar = dtoType.GetProperty(property.Name).GetValue(oldDto, null); object newVar = dtoType.GetProperty(property.Name).GetValue(newDto, null); if (CommonMethod.IsNullOrEmpty(oldVar) && CommonMethod.IsNullOrEmpty(newVar)) { continue; } if (!CommonMethod.IsNullOrEmpty(oldVar) && CommonMethod.IsNullOrEmpty(newVar)) { return(true); } if (CommonMethod.IsNullOrEmpty(oldVar) && !CommonMethod.IsNullOrEmpty(newVar)) { return(true); } if (!CommonMethod.IsNullOrEmpty(oldVar) && !oldVar.Equals(newVar)) { return(true); } } // End Update by Trong on 20130801 for DNP project } return(false); }
/// <summary> /// Fills the data. /// </summary> /// <param name="dto">The dto.</param> /// <param name="entity">The entity.</param> /// <param name="entityFromDto">if private set to <c>true</c> [entity from dto].</param> private static void FillData(IDto dto, object entity, bool entityFromDto) { var dtoType = dto.GetType(); var entityType = entity.GetType(); MappingType mappingType; if (!VerifyForEntityType(entityType, dtoType, out mappingType)) { throw new EntityConversionException(string.Format(Thread.CurrentThread.CurrentCulture, "Entity type '{0}' must match with type specified in EntityMappingAttribute on type '{1}' !", entityType.ToString(), dtoType.ToString())); } var properties = dtoType.GetProperties(); foreach (PropertyInfo property in properties) { bool skipThisProperty = false; object[] customAttributes = property.GetCustomAttributes(typeof(EntityPropertyMappingAttribute), false); if (mappingType == MappingType.TotalExplicit && customAttributes.Length == 0) { continue; } foreach (object customAttribute in customAttributes) { EntityPropertyMappingAttribute entityPropertyMappingAttribute = (EntityPropertyMappingAttribute)customAttribute; if (entityPropertyMappingAttribute.MappingDirection == MappingDirectionType.None) { skipThisProperty = true; break; } } if (skipThisProperty) { continue; } var entityPropertyName = GetEntityPropertyName(property, mappingType, entityFromDto); if (!string.IsNullOrEmpty(entityPropertyName)) { var entityProperty = entityType.GetProperty(entityPropertyName); if (entityProperty == null) { throw new EntityConversionException(entityPropertyName, entity); } var sourceProperty = entityFromDto ? property : entityProperty; var destinationProperty = entityFromDto ? entityProperty : property; var sourceObject = entityFromDto ? dto : entity; var destinationObject = entityFromDto ? entity : dto; var sourceValue = sourceProperty.GetValue(sourceObject, null); if (destinationProperty.CanWrite) { if (sourceProperty.PropertyType.IsEnum && destinationProperty.PropertyType == typeof(byte)) { sourceValue = (byte)(int)sourceValue; } destinationProperty.SetValue(destinationObject, sourceValue, null); } } } }
public static string[] GetActionFields(this IDto dto) { return(GetActionFields(dto.GetType())); }
public static TEntity ToEntity <TEntity>(this IDto dto) { var factory = EntityFactory.GetFactory <IFactory <TEntity> >(dto.GetType()); return(factory.Create(dto)); }
public static IEntity ToEntity(this IDto dto) { var factory = EntityFactory.GetFactory(dto.GetType()); return(factory.Create(dto) as IEntity); }
/// <summary> /// The get section of system. /// </summary> /// <param name="dto"> /// The dto. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public static string GetSectionOfSystem(this IDto dto) { Type type = dto.GetType(); return(GetSectionOfSystem(type)); }
/// <summary> /// Gets the column properties. /// </summary> /// <param name="dto"> /// The dto. /// </param> /// <returns> /// The <see cref="IEnumerable{PropertyInfo}"/>. /// </returns> internal static IEnumerable <PropertyInfo> GetColumnProperties(this IDto dto) { return(dto.GetType().GetProperties().Where(x => Attribute.IsDefined(x, typeof(ColumnAttribute), false) && x.GetCustomAttribute <ColumnAttribute>().GetType() != typeof(ResultColumnAttribute))); }
protected override void OnMessage(MessageEventArgs e) { WebSocketMessage message = Deserialize <WebSocketMessage>(e.Data); try { if (message.MessageType == WebSocketMessage.WebSocketMessageType.RootQuery) { _sendResponse(message, _initialObject); } else // method of particular object { IDto objectToInvoke = _referenceResolver.ResolveReference(message.DtoGuid); if (objectToInvoke != null) { if (message.MessageType == WebSocketMessage.WebSocketMessageType.Query || message.MessageType == WebSocketMessage.WebSocketMessageType.Invoke) { Type objectToInvokeType = objectToInvoke.GetType(); MethodInfo methodToInvoke = objectToInvokeType.GetMethods().FirstOrDefault(m => m.Name == message.MemberName && m.GetParameters().Length == message.Parameters.Length); if (methodToInvoke != null) { ParameterInfo[] methodParameters = methodToInvoke.GetParameters(); _alignContentTypes(ref message.Parameters, methodParameters.Select(p => p.ParameterType).ToArray()); for (int i = 0; i < methodParameters.Length; i++) { MethodParametersAlignment.AlignType(ref message.Parameters[i], methodParameters[i].ParameterType); } object response = methodToInvoke.Invoke(objectToInvoke, BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, message.Parameters, null); if (message.MessageType == WebSocketMessage.WebSocketMessageType.Query) { _sendResponse(message, response); } } else { throw new ApplicationException(string.Format("Server: unknown method: {0}:{1}", objectToInvoke, message.MemberName)); } } else if (message.MessageType == WebSocketMessage.WebSocketMessageType.Get || message.MessageType == WebSocketMessage.WebSocketMessageType.Set) { PropertyInfo property = objectToInvoke.GetType().GetProperty(message.MemberName); if (property != null) { if (message.MessageType == WebSocketMessage.WebSocketMessageType.Get && property.CanRead) { object response = property.GetValue(objectToInvoke, null); _sendResponse(message, response); } else // Set { if (property.CanWrite) { _alignContentTypes(ref message.Parameters, new Type[] { property.PropertyType }); property.SetValue(objectToInvoke, message.Parameters[0], null); } else { throw new ApplicationException(string.Format("Server: not writable property: {0}:{1}", objectToInvoke, message.MemberName)); } } } else { throw new ApplicationException(string.Format("Server: unknown property: {0}:{1}", objectToInvoke, message.MemberName)); } } else if (message.MessageType == WebSocketMessage.WebSocketMessageType.EventAdd || message.MessageType == WebSocketMessage.WebSocketMessageType.EventRemove) { EventInfo ei = objectToInvoke.GetType().GetEvent(message.MemberName); if (ei != null) { if (message.MessageType == WebSocketMessage.WebSocketMessageType.EventAdd) { _addDelegate(objectToInvoke, ei); } else if (message.MessageType == WebSocketMessage.WebSocketMessageType.EventRemove) { _removeDelegate(objectToInvoke, ei); } } else { throw new ApplicationException(string.Format("Server: unknown event: {0}:{1}", objectToInvoke, message.MemberName)); } } } else { _sendResponse(message, null); } //throw new ApplicationException(string.Format("Server: unknown DTO: {0} on {1}", message.DtoGuid, message)); } } catch (Exception ex) { message.MessageType = WebSocketMessage.WebSocketMessageType.Exception; message.Response = ex; Send(_serialize(message)); Debug.WriteLine(ex); } }
public static TEntity DtoConvertEntity <TEntity>(this IDto dto, TEntity entity) where TEntity : BaseEntity, new () { return((TEntity)Mapper.Map(dto, entity, dto.GetType(), entity.GetType())); }
protected override void OnMessage(byte[] data) { var message = new SocketMessage(data); try { if (message.MessageType == SocketMessage.SocketMessageType.RootQuery) { _sendResponse(message, _initialObject); } else // method of particular object { IDto objectToInvoke = _referenceResolver.ResolveReference(message.DtoGuid); if (objectToInvoke != null) { if (message.MessageType == SocketMessage.SocketMessageType.Query || message.MessageType == SocketMessage.SocketMessageType.Invoke) { Type objectToInvokeType = objectToInvoke.GetType(); MethodInfo methodToInvoke = objectToInvokeType.GetMethods() .FirstOrDefault(m => m.Name == message.MemberName && m.GetParameters().Length == message.ValueCount); if (methodToInvoke != null) { var parameters = DeserializeDto <SocketMessageArrayValue>(message.ValueStream); ParameterInfo[] methodParameters = methodToInvoke.GetParameters(); for (int i = 0; i < methodParameters.Length; i++) { MethodParametersAlignment.AlignType(ref parameters.Value[i], methodParameters[i].ParameterType); } object response = methodToInvoke.Invoke(objectToInvoke, BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, parameters.Value, null); if (message.MessageType == SocketMessage.SocketMessageType.Query) { _sendResponse(message, response); } } else { throw new ApplicationException( $"Server: unknown method: {objectToInvoke}:{message.MemberName}"); } } else if (message.MessageType == SocketMessage.SocketMessageType.Get || message.MessageType == SocketMessage.SocketMessageType.Set) { PropertyInfo property = objectToInvoke.GetType().GetProperty(message.MemberName); if (property != null) { if (message.MessageType == SocketMessage.SocketMessageType.Get && property.CanRead) { object response = property.GetValue(objectToInvoke, null); _sendResponse(message, response); } else // Set { if (property.CanWrite) { var parameter = DeserializeDto <object>(message.ValueStream); MethodParametersAlignment.AlignType(ref parameter, property.PropertyType); property.SetValue(objectToInvoke, parameter, null); } else { throw new ApplicationException( $"Server: not writable property: {objectToInvoke}:{message.MemberName}"); } } } else { throw new ApplicationException( $"Server: unknown property: {objectToInvoke}:{message.MemberName}"); } } else if (message.MessageType == SocketMessage.SocketMessageType.EventAdd || message.MessageType == SocketMessage.SocketMessageType.EventRemove) { EventInfo ei = objectToInvoke.GetType().GetEvent(message.MemberName); if (ei != null) { if (message.MessageType == SocketMessage.SocketMessageType.EventAdd) { _addDelegate(objectToInvoke, ei); } else if (message.MessageType == SocketMessage.SocketMessageType.EventRemove) { _removeDelegate(objectToInvoke, ei); } } else { throw new ApplicationException( $"Server: unknown event: {objectToInvoke}:{message.MemberName}"); } } } else { _sendResponse(message, null); throw new ApplicationException($"Server: unknown DTO: {message.DtoGuid} on {message}"); } } } catch (Exception ex) { Logger.Error(ex); } }
protected override void OnMessage(MessageEventArgs e) { WebSocketMessage message = new WebSocketMessage(e.RawData); try { var user = AuthenticationService.FindUser(AuthenticationSource.IpAddress, Context?.UserEndPoint.Address.ToString()); if (user == null) { throw new UnauthorizedAccessException($"Access from {Context?.UserEndPoint.Address} not allowed"); } Thread.CurrentPrincipal = new GenericPrincipal(user, new string[0]); if (message.MessageType == WebSocketMessage.WebSocketMessageType.RootQuery) { _sendResponse(message, InitialObject); } else // method of particular object { IDto objectToInvoke = _referenceResolver.ResolveReference(message.DtoGuid); if (objectToInvoke != null) { if (message.MessageType == WebSocketMessage.WebSocketMessageType.Query || message.MessageType == WebSocketMessage.WebSocketMessageType.Invoke) { Type objectToInvokeType = objectToInvoke.GetType(); MethodInfo methodToInvoke = objectToInvokeType.GetMethods() .FirstOrDefault(m => m.Name == message.MemberName && m.GetParameters().Length == message.ValueCount); if (methodToInvoke != null) { var parameters = DeserializeDto <WebSocketMessageArrayValue>(message.GetValueStream()); ParameterInfo[] methodParameters = methodToInvoke.GetParameters(); for (int i = 0; i < methodParameters.Length; i++) { MethodParametersAlignment.AlignType(ref parameters.Value[i], methodParameters[i].ParameterType); } object response = methodToInvoke.Invoke(objectToInvoke, BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, parameters.Value, null); if (message.MessageType == WebSocketMessage.WebSocketMessageType.Query) { _sendResponse(message, response); } } else { throw new ApplicationException( $"Server: unknown method: {objectToInvoke}:{message.MemberName}"); } } else if (message.MessageType == WebSocketMessage.WebSocketMessageType.Get || message.MessageType == WebSocketMessage.WebSocketMessageType.Set) { PropertyInfo property = objectToInvoke.GetType().GetProperty(message.MemberName); if (property != null) { if (message.MessageType == WebSocketMessage.WebSocketMessageType.Get && property.CanRead) { object response = property.GetValue(objectToInvoke, null); _sendResponse(message, response); } else // Set { if (property.CanWrite) { var parameter = DeserializeDto <object>(message.GetValueStream()); MethodParametersAlignment.AlignType(ref parameter, property.PropertyType); property.SetValue(objectToInvoke, parameter, null); } else { throw new ApplicationException( $"Server: not writable property: {objectToInvoke}:{message.MemberName}"); } } } else { throw new ApplicationException( $"Server: unknown property: {objectToInvoke}:{message.MemberName}"); } } else if (message.MessageType == WebSocketMessage.WebSocketMessageType.EventAdd || message.MessageType == WebSocketMessage.WebSocketMessageType.EventRemove) { EventInfo ei = objectToInvoke.GetType().GetEvent(message.MemberName); if (ei != null) { if (message.MessageType == WebSocketMessage.WebSocketMessageType.EventAdd) { _addDelegate(objectToInvoke, ei); } else if (message.MessageType == WebSocketMessage.WebSocketMessageType.EventRemove) { _removeDelegate(objectToInvoke, ei); } } else { throw new ApplicationException( $"Server: unknown event: {objectToInvoke}:{message.MemberName}"); } } } else { _sendResponse(message, null); throw new ApplicationException($"Server: unknown DTO: {message.DtoGuid} on {message}"); } } } catch (Exception ex) { message.MessageType = WebSocketMessage.WebSocketMessageType.Exception; //SerializeDto(message, ex); //Send(message.Serialize()); Logger.Error(ex); Debug.WriteLine(ex); } }