private void BuildDTOObjectCollections(RESTModel.PersistentObject dto, ERModel.PersistentObject real) { Type dtoType = dto.GetType(); var collectionProps = from x in dtoType.GetProperties() where x.IsDefined(typeof(ObjectCollectionAttribute), false) select x; foreach (var collectionProp in collectionProps) { PropertyInfo realCollectionProp = real.GetType().GetProperty(collectionProp.Name); var realItems = realCollectionProp.GetValue(real, null) as IList; if (realItems == null) { throw new InvalidOperationException(String.Format("Real object's object collection property {0} is not IList", realCollectionProp.GetType())); } var items = collectionProp.GetValue(dto, null) as IList; if (items == null) { throw new InvalidOperationException(String.Format("DTO's object collection property {0} is not IList", collectionProp.GetType())); } foreach (ERModel.PersistentObject itemReal in realItems) { var itemDTO = GetDTO(itemReal); items.Add(itemDTO); } } }
private void BuildRealObjectProperties(ERModel.PersistentObject real, RESTModel.PersistentObject dto, ISession session) { // TODO: Consider scenario when real object does not exists but should be created from dto, // which is sent with the current dto, and can be found somewhere in the hierarchy. Type dtoType = dto.GetType(); var objectProps = from x in dtoType.GetProperties() where x.IsDefined(typeof(ObjectPropertyAttribute), false) select x; foreach (var propDTO in objectProps) { // No checks, since it's guaranteed that it is defined, and only once. var attr = propDTO.GetCustomAttributes(typeof(ObjectPropertyAttribute), false)[0] as ObjectPropertyAttribute; // TODO: this just cuts an Id at the end. // In case more sophisticated logic is required, it should be possible to set name as a property of ObjectPropertyAttribute. var realPropName = propDTO.Name.Substring(0, propDTO.Name.Length - 2); var propReal = real.GetType().GetProperty(realPropName); // If object does not exist, assign null. If property is not nullable, we'll get a database error. var obj = session.Get(attr.ObjectType, propDTO.GetValue(dto, null)); propReal.SetValue(real, obj, null); } }
private void BuildDTOCalculatedProperties(RESTModel.PersistentObject dto, ERModel.PersistentObject real) { Type dtoType = dto.GetType(); var calcProps = from x in dtoType.GetProperties() where x.IsDefined(typeof(CalculatedPropertyAttribute), false) select x; foreach (var propDTO in calcProps) { // No checks, since it's guaranteed that it is defined, and only once. var attr = propDTO.GetCustomAttributes(typeof(CalculatedPropertyAttribute), false)[0] as CalculatedPropertyAttribute; object currentObject = real; var propChain = attr.PropertyChain.Split('.'); foreach (string propName in propChain) { var propReal = currentObject.GetType().GetProperty(propName); if (propReal == null) { throw new InvalidOperationException(String.Format("Object {0} does not have property {1}.", currentObject.GetType().ToString(), propName)); } currentObject = propReal.GetValue(currentObject, null); if (currentObject == null) { break; } } propDTO.SetValue(dto, currentObject, null); } }
private void BuildRealSimpleProperties(ERModel.PersistentObject real, RESTModel.PersistentObject dto, ISession session) { Type dtoType = dto.GetType(); var simpleProps = from x in dtoType.GetProperties() where x.IsDefined(typeof(SimplePropertyAttribute), false) select x; foreach (var propDTO in simpleProps) { var propReal = real.GetType().GetProperty(propDTO.Name); propReal.SetValue(real, propDTO.GetValue(dto, null), null); } }
private void BuildDTOObjectViewProperties(RESTModel.PersistentObject dto, ERModel.PersistentObject real) { Type dtoType = dto.GetType(); var objectViewProps = from x in dtoType.GetProperties() where x.IsDefined(typeof(ObjectViewPropertyAttribute), false) select x; foreach (var propDTO in objectViewProps) { var propReal = real.GetType().GetProperty(propDTO.Name); var obj = propReal.GetValue(real, null) as ERModel.PersistentObject; propDTO.SetValue(dto, GetDTO(obj), null); } }
private void SetParent(dynamic real, RESTModel.PersistentObject dto, ISession session) { Type dtoType = dto.GetType(); var parentProps = from x in dtoType.GetProperties() where x.IsDefined(typeof(ParentObjectAttribute), false) select x; foreach (var propDTO in parentProps) { // No checks, since it's guaranteed that it is defined, and only once. var attr = propDTO.GetCustomAttributes(typeof(ParentObjectAttribute), false)[0] as ParentObjectAttribute; var parentReal = session.Get(attr.ObjectType, propDTO.GetValue(dto, null)); if (parentReal != null) { // TODO: this just cuts an Id at the end. // In case more sophisticated logic is required, it should be possible to set name as a property of ObjectPropertyAttribute. var realPropName = propDTO.Name.Substring(0, propDTO.Name.Length - 2); var propReal = real.GetType().GetProperty(realPropName); propReal.SetValue(real, parentReal, null); } } }
private void BuildDTOAnyObjectProperties(RESTModel.PersistentObject dto, ERModel.PersistentObject real, Type AttributeType) { Type dtoType = dto.GetType(); var objectProps = from x in dtoType.GetProperties() where x.IsDefined(AttributeType, false) select x; foreach (var propDTO in objectProps) { // TODO: this just cuts an Id at the end. // In case more sophisticated logic is required, it should be possible to set name as a property of ObjectPropertyAttribute. var realPropName = propDTO.Name.Substring(0, propDTO.Name.Length - 2); var propReal = real.GetType().GetProperty(realPropName); var obj = propReal.GetValue(real, null) as ERModel.PersistentObject; if (obj == null) { propDTO.SetValue(dto, Guid.Empty, null); } else { propDTO.SetValue(dto, obj.Id, null); } } }
private void BuildRealObjectCollections(ERModel.PersistentObject real, RESTModel.PersistentObject dto, ISession session) { Type dtoType = dto.GetType(); var collectionProps = from x in dtoType.GetProperties() where x.IsDefined(typeof(ObjectCollectionAttribute), false) select x; foreach (var collectionProp in collectionProps) { // No checks, since it's guaranteed that it is defined, and only once. var attr = collectionProp.GetCustomAttributes(typeof(ObjectCollectionAttribute), false)[0] as ObjectCollectionAttribute; PropertyInfo realCollectionProp = real.GetType().GetProperty(collectionProp.Name); var realItems = realCollectionProp.GetValue(real, null) as IList; if (realItems == null) { throw new InvalidOperationException(String.Format("Real object's object collection property {0} is not IList", realCollectionProp.GetType())); } var items = collectionProp.GetValue(dto, null) as IList; if (items == null) { throw new InvalidOperationException(String.Format("DTO's object collection property {0} is not IList", collectionProp.GetType())); } var realItemsDictionary = new Dictionary <Guid, ERModel.PersistentObject>(); foreach (ERModel.PersistentObject itemReal in realItems) { realItemsDictionary.Add(itemReal.Id, itemReal); } foreach (RESTModel.PersistentObject itemDTO in items) { dynamic itemReal; bool isNew = false; if (realItemsDictionary.ContainsKey(itemDTO.Id)) { // Update in the collection itemReal = realItemsDictionary[itemDTO.Id]; realItemsDictionary.Remove(itemDTO.Id); } else { // Look if exists detached itemReal = session.Get(itemDTO.PersistentType, itemDTO.Id); if (itemReal == null) { // Insert itemReal = Activator.CreateInstance(itemDTO.PersistentType); itemReal.Id = itemDTO.Id; isNew = true; } // Attach realItems.Add(itemReal); } // Update link to parent if (!String.IsNullOrWhiteSpace(attr.ParentProperty)) { var parentProp = itemDTO.PersistentType.GetProperty(attr.ParentProperty); if (parentProp == null) { throw new InvalidOperationException( String.Format("Object collection mapping error. Type {0} does not have property {1}", itemDTO.PersistentType.ToString(), attr.ParentProperty)); } parentProp.SetValue(itemReal, real, null); } AssembleRealObjectFromDTO(itemReal, itemDTO, session, isNew); } foreach (var objectReal in realItemsDictionary.Values) { // Detach realItems.Remove(objectReal); if (attr.DeletionBehavior == ObjectCollectionDeletionBehavior.Delete) { // Delete session.Delete(objectReal); } } } }