예제 #1
0
        public static T MapValue <T>(object cypherValue)
        {
            var targetType = typeof(T);

            if (typeof(IEnumerable).IsAssignableFrom(targetType))
            {
                if (!(cypherValue is IEnumerable enumerable))
                {
                    throw new InvalidOperationException($"The cypher value is not a list and cannot be mapped to target type: {targetType.UnderlyingSystemType}");
                }

                if (targetType == typeof(string))
                {
                    return(enumerable.As <T>());
                }

                var elementType      = targetType.GetGenericArguments()[0];
                var genericType      = typeof(CollectionMapper <>).MakeGenericType(elementType);
                var collectionMapper = (ICollectionMapper)genericType.CreateInstance();

                return((T)collectionMapper.MapValues(enumerable, targetType));
            }

            if (cypherValue is INode node)
            {
                var entity = node.Properties.FromObjectDictionary <T>();
                EntityAccessor.SetNodeId(entity, node.Id);

                return(entity);
            }

            if (cypherValue is IRelationship relationship)
            {
                var entity = relationship.Properties.FromObjectDictionary <T>();

                return(entity);
            }

            if (cypherValue is IReadOnlyDictionary <string, object> map)
            {
                return(map.FromObjectDictionary <T>());
            }

            if (cypherValue is IEnumerable)
            {
                throw new InvalidOperationException($"The cypher value is a list and cannot be mapped to target type: {targetType.UnderlyingSystemType}");
            }

            return(cypherValue.As <T>());
        }
        public static async Task <IResultCursor> SetNodeAsync <TEntity>(
            this IAsyncTransaction asyncTransaction,
            TEntity entity) where TEntity : class
        {
            var nodeId = EntityAccessor.GetNodeId(entity);

            if (nodeId == null)
            {
                throw new InvalidOperationException(Constants.NodeIdUnspecifiedMessage);
            }

            var parameters = new Neo4jParameters()
                             .WithValue("p1", nodeId)
                             .WithEntity("p2", entity);

            return(await asyncTransaction.RunAsync(Constants.Statement.SetNode, parameters)
                   .ConfigureAwait(false));
        }