Exemplo n.º 1
0
        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);
                }
            }
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
 private void AssembleDTOFromRealObject(RESTModel.PersistentObject dto, ERModel.PersistentObject real)
 {
     BuildDTOSimpleProperties(dto, real);
     BuildDTOCalculatedProperties(dto, real);
     BuildDTOObjectProperties(dto, real);
     BuildDTOObjectViewProperties(dto, real);
     BuildDTOParentObjectProperties(dto, real);
     BuildDTOObjectCollections(dto, real);
 }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 6
0
        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);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Returns DTO assembled from the real object.
        /// </summary>
        /// <param name="real">The real object.</param>
        /// <returns>DTO.</returns>
        public RESTModel.PersistentObject GetDTO(ERModel.PersistentObject real)
        {
            if (real == null)
            {
                return(null);
            }

            var handle = Activator.CreateInstance(
                typeof(RESTModel.PersistentObject).Assembly.FullName,
                typeof(RESTModel.PersistentObject).Namespace + "." + real.GetType().Name.Replace("Proxy", String.Empty));                 // TODO: is there any better way to get the name?

            var dto = handle.Unwrap() as RESTModel.PersistentObject;

            dto.Id = real.Id;
            AssembleDTOFromRealObject(dto, real);

            return(dto);
        }
Exemplo n.º 8
0
        private void AssembleRealObjectFromDTO(ERModel.PersistentObject real, RESTModel.PersistentObject dto, ISession session, bool isNew)
        {
            if (!BypassValidation)
            {
                var error = new Validator().Validate(dto);
                if (error != null)
                {
                    throw new ValidationException(error.ToString());
                }
            }

            BuildRealSimpleProperties(real, dto, session);
            BuildRealObjectProperties(real, dto, session);
            if (isNew)
            {
                session.Save(real);
            }
            BuildRealObjectCollections(real, dto, session);
        }
Exemplo n.º 9
0
        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);
                }
            }
        }
Exemplo n.º 10
0
        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);
                    }
                }
            }
        }
Exemplo n.º 11
0
 private void BuildDTOParentObjectProperties(RESTModel.PersistentObject dto, ERModel.PersistentObject real)
 {
     BuildDTOAnyObjectProperties(dto, real, typeof(ParentObjectAttribute));
 }