예제 #1
0
        /// <summary>
        /// Registers a custom resolver for the given service reference client type using a sequence of uri resolvers.
        /// If a resolver returns null, the next one will be used. If they all return null, then a null value will be returned for the dependency.
        /// </summary>
        /// <typeparam name="TClient">The service reference client type contract</typeparam>
        /// <param name="container">The dependency injection container</param>
        /// <param name="serviceUriResolvers">The uri resolvers to try</param>
        public static void RegisterServiceReference <TClient>(this DependencyInjectionContainer container, params Func <Uri>[] serviceUriResolvers)
        {
            ExceptionUtilities.CheckArgumentNotNull(container, "container");
            ExceptionUtilities.CheckCollectionNotEmpty(serviceUriResolvers, "serviceUriResolvers");
            ExceptionUtilities.CheckCollectionDoesNotContainNulls(serviceUriResolvers, "serviceUriResolvers");

            var options = container.RegisterCustomResolver(
                typeof(TClient),
                t =>
            {
                Uri serviceUri = null;
                foreach (var uriResolver in serviceUriResolvers)
                {
                    serviceUri = uriResolver();
                    if (serviceUri != null)
                    {
                        break;
                    }
                }

                if (serviceUri == null)
                {
                    return(null);
                }

                var factory = container.Resolve <IServiceReferenceFactory>();
                return(factory.CreateInstance <TClient>(serviceUri));
            });

            options.IsTransient = true;
        }
예제 #2
0
        /// <summary>
        /// Resolves an assembly
        /// </summary>
        /// <param name="assemblyName">Assembly Name</param>
        /// <returns>Will return either the original result or a fully resolved assemblypath</returns>
        public string ResolveAssemblyLocation(string assemblyName)
        {
            ExceptionUtilities.CheckArgumentNotNull(assemblyName, "assemblyName");

            if (Path.IsPathRooted(assemblyName))
            {
                return(assemblyName);
            }

            List <string> foundFilePaths = FindFilePaths(this.GetDirectoryLookupLocations(), assemblyName);

            ExceptionUtilities.CheckCollectionDoesNotContainNulls <string>(foundFilePaths, "foundFilePaths");

            ExceptionUtilities.Assert(foundFilePaths.Count < 2, "Found '{0}' in the following places '{1}'", assemblyName, string.Join(",", foundFilePaths));
            string fullPath = foundFilePaths.SingleOrDefault();

            if (fullPath != null)
            {
                return(fullPath);
            }
            else
            {
                return(assemblyName);
            }
        }
예제 #3
0
        private static TChangeset Changeset <TChangeset, TOperation>(string boundary, string charset, Func <TChangeset> createChangeset, params IMimePart[] parts)
            where TChangeset : BatchChangeset <TOperation>
            where TOperation : class, IMimePart
        {
            ExceptionUtilities.CheckCollectionDoesNotContainNulls(parts, "parts");

            var changeset = createChangeset();

            changeset.Headers[HttpHeaders.ContentType] = HttpUtilities.BuildContentType(MimeTypes.MultipartMixed, charset, boundary);

            foreach (var part in parts)
            {
                var wrappedRequest = part as MimePartData <TOperation>;
                if (wrappedRequest != null)
                {
                    changeset.Add(wrappedRequest);
                }
                else
                {
                    var operation = part as TOperation;
                    ExceptionUtilities.CheckObjectNotNull(operation, "Given part was of unexpected type '{0}'", part.GetType());
                    string contentId;
                    if (operation.Headers.TryGetValue(HttpHeaders.ContentId, out contentId))
                    {
                        operation.Headers.Remove(HttpHeaders.ContentId);
                    }

                    changeset.Add(operation.AsBatchFragment(contentId));
                }
            }

            return(changeset);
        }
        /// <summary>
        /// Initializes a new instance of the DelegatedRandomDataGenerator class.
        /// </summary>
        /// <param name="random">Random number generator.</param>
        /// <param name="generateDataDelegates">Delegates to use when generating data. Delegate is picked randomly each time if more than one delegate provided.</param>
        /// <param name="interestingData">Interesting data that should be generated randomly.</param>
        public DelegatedRandomDataGenerator(IRandomNumberGenerator random, IEnumerable <Func <IRandomNumberGenerator, TData> > generateDataDelegates, params TData[] interestingData)
        {
            ExceptionUtilities.CheckArgumentNotNull(random, "random");
            ExceptionUtilities.CheckCollectionNotEmpty(generateDataDelegates, "generateDataDelegates");
            ExceptionUtilities.CheckCollectionDoesNotContainNulls(generateDataDelegates, "generateDataDelegates");
            ExceptionUtilities.CheckArgumentNotNull(interestingData, "interestingData");

            this.random = random;
            this.generateDataDelegates = generateDataDelegates.ToArray();
            this.interestingData       = interestingData.ToArray();
        }
예제 #5
0
        /// <summary>
        /// Adds given methods to <see cref="Methods"/> collection.
        /// </summary>
        /// <param name="methods">methods to add.</param>
        /// <returns>This object (useful for chaining multiple calls).</returns>
        public SpatialDataType WithMethods(params Function[] methods)
        {
            ExceptionUtilities.CheckCollectionDoesNotContainNulls(methods, "methods");
            ExceptionUtilities.Assert(this.Methods.Count == 0, "This method can only be called on spatial type without any methods defined.");

            foreach (var m in methods)
            {
                this.Methods.Add(m);
            }

            return(this);
        }
예제 #6
0
        /// <summary>
        /// Adds given properties to <see cref="Properties"/> collection.
        /// </summary>
        /// <param name="properties">Properties to add.</param>
        /// <returns>This object (useful for chaining multiple calls).</returns>
        public SpatialDataType WithProperties(params MemberProperty[] properties)
        {
            ExceptionUtilities.CheckCollectionDoesNotContainNulls(properties, "properties");
            ExceptionUtilities.Assert(this.Properties.Count == 0, "This method can only be called on spatial type without any properties defined.");

            foreach (var p in properties)
            {
                this.Properties.Add(p);
            }

            return(this);
        }
예제 #7
0
        /// <summary>
        /// Adds given columns to <see cref="Columns"/> collection.
        /// </summary>
        /// <typeparam name="TConstraint">The type of the constraint.</typeparam>
        /// <param name="columns">Columns that participate in the constraint.</param>
        /// <returns>This object (useful for chaining multiple calls).</returns>
        protected TConstraint WithColumns <TConstraint>(params Column[] columns) where TConstraint : ColumnsConstraintBase
        {
            ExceptionUtilities.CheckCollectionDoesNotContainNulls(columns, "columns");
            ExceptionUtilities.Assert(this.Columns.Count == 0, "This method can only be called on a constraint that doesn't have any columns defined.");

            foreach (Column c in columns)
            {
                this.Add(c);
            }

            return((TConstraint)this);
        }
예제 #8
0
        public static string ConcatenateUriSegments(params string[] segments)
        {
            ExceptionUtilities.CheckCollectionDoesNotContainNulls(segments, "segments");

            if (segments.Length == 0)
            {
                return(null);
            }

            if (segments.Length == 1)
            {
                return(segments[0]);
            }

            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < segments.Length; i++)
            {
                // if this is the first segment, then only trim trailing slashes
                if (i == 0)
                {
                    builder.Append(segments[i].TrimEnd('/'));
                }
                else
                {
                    // if this is not the first segment, then we need to add a slash before concatenating the next
                    builder.Append('/');

                    // if we're not at the end, then trim slashes from both ends
                    if (i < segments.Length - 1)
                    {
                        builder.Append(segments[i].Trim('/'));
                    }
                    else
                    {
                        // if we're at the end, then trim only the leading slashes
                        builder.Append(segments[i].TrimStart('/'));
                    }
                }
            }

            return(builder.ToString());
        }
예제 #9
0
        /// <summary>
        /// Calculates the key for an entity, which is used to build uris for link update and delete operations
        /// </summary>
        /// <param name="entity">The entity to build a key for</param>
        /// <returns>The key of the entity</returns>
        public string CalculateEntityKey(object entity)
        {
            var entityType    = entity.GetType();
            var keyProperties = entityType.GetCustomAttributes(typeof(KeyAttribute), true)
                                .Cast <KeyAttribute>().SelectMany(att => att.KeyNames)
                                .Concat(entityType.GetProperties().Select(p => p.Name).Where(p => p.EndsWith("ID", StringComparison.Ordinal)))
                                .Distinct()
                                .OrderBy(p => p, Comparer <string> .Default)
                                .Select(p => entityType.GetProperty(p))
                                .ToList();

            ExceptionUtilities.CheckCollectionNotEmpty(keyProperties, "keyProperties");
            ExceptionUtilities.CheckCollectionDoesNotContainNulls(keyProperties, "keyProperties");

            StringBuilder builder = new StringBuilder();

            this.LiteralConverter.AppendKeyExpression(builder, keyProperties.Select(p => new NamedValue(p.Name, p.GetValue(entity, null))));
            return(builder.ToString());
        }
예제 #10
0
        private static TItem WithDataGenerationHintsInternal <TItem>(this TItem annotatedItem, params DataGenerationHint[] hints) where TItem : AnnotatedItem
        {
            ExceptionUtilities.CheckCollectionDoesNotContainNulls(hints, "hints");

            if (hints.Length == 0)
            {
                return(annotatedItem);
            }

            DataGenerationHintsAnnotation dataGenHintsAnnotation = annotatedItem.Annotations.OfType <DataGenerationHintsAnnotation>().SingleOrDefault();

            if (dataGenHintsAnnotation == null)
            {
                dataGenHintsAnnotation = new DataGenerationHintsAnnotation();
                annotatedItem.Annotations.Add(dataGenHintsAnnotation);
            }

            foreach (var hint in hints)
            {
                dataGenHintsAnnotation.Add(hint);
            }

            return(annotatedItem);
        }
예제 #11
0
 /// <summary>
 /// Initializes a new instance of the CompositeEntityModelFixup class
 /// </summary>
 /// <param name="fixups">The initial set of fixups</param>
 public CompositeEntityModelFixup(IEnumerable <IEntityModelFixup> fixups)
 {
     ExceptionUtilities.CheckCollectionDoesNotContainNulls(fixups, "fixups");
     this.Fixups = new List <IEntityModelFixup>(fixups);
 }