示例#1
0
 public static T BestMatch <T>(this IEnumerable <T> collection,
                               Func <T, bool> condition, Func <T, string> fieldFunc, string value,
                               INameMatchResolver matchResolver)
     where T : class
 {
     return(collection.FirstOrDefault(x => matchResolver.IsMatch(fieldFunc(x), value) && condition(x)));
 }
示例#2
0
    public static bool NamedKeyValuesContainKeyNames(
        IDictionary <string, object>?namedKeyValues,
        INameMatchResolver resolver,
        IEnumerable <string>?keyNames,
        out IEnumerable <KeyValuePair <string, object> >?matchingNamedKeyValues)
    {
        matchingNamedKeyValues = null;
        if (namedKeyValues is null || keyNames is null)
        {
            return(false);
        }

        var tmpMatchingNamedKeyValues = new List <KeyValuePair <string, object> >();

        foreach (var namedKeyValue in namedKeyValues)
        {
            var keyProperty = keyNames.FirstOrDefault(x => resolver.IsMatch(x, namedKeyValue.Key));
            if (keyProperty != null)
            {
                tmpMatchingNamedKeyValues.Add(new KeyValuePair <string, object>(keyProperty, namedKeyValue.Value));
            }
        }

        if (tmpMatchingNamedKeyValues.Any())
        {
            matchingNamedKeyValues = tmpMatchingNamedKeyValues;
            return(true);
        }

        return(false);
    }
示例#3
0
 protected IODataClient CreateClientWithNameResolver(INameMatchResolver nameMatchResolver)
 {
     return(new ODataClient(new ODataClientSettings
     {
         BaseUri = _serviceUri,
         OnTrace = (x, y) => Console.WriteLine(string.Format(x, y)),
         NameMatchResolver = nameMatchResolver,
     }));
 }
 protected ODataClientSettings CreateDefaultSettingsWithNameResolver(INameMatchResolver nameMatchResolver)
 {
     return(new ODataClientSettings(_serviceUri)
     {
         PayloadFormat = _payloadFormat,
         IgnoreResourceNotFoundException = true,
         OnTrace = (x, y) => Console.WriteLine(string.Format(x, y)),
         NameMatchResolver = nameMatchResolver,
     });
 }
    public IODataClient CreateClient(string metadataFile, INameMatchResolver matchResolver = null)
    {
        var baseUri        = new Uri("http://localhost/" + metadataFile);
        var metadataString = GetResourceAsString(@"Resources." + metadataFile);
        var settings       = new ODataClientSettings
        {
            BaseUri           = baseUri,
            MetadataDocument  = metadataString,
            NameMatchResolver = matchResolver,
        };

        _session = Session.FromMetadata(baseUri, metadataString);
        return(new ODataClient(settings));
    }
示例#6
0
        public static T BestMatch <T>(this IEnumerable <T> collection,
                                      Func <T, string> fieldFunc, string value, INameMatchResolver matchResolver)
            where T : class
        {
            if (ReferenceEquals(matchResolver, ODataNameMatchResolver.Strict))
            {
                return(collection.FirstOrDefault(x => matchResolver.IsMatch(fieldFunc(x), value)));
            }

            return(collection
                   .Where(x => matchResolver.IsMatch(fieldFunc(x), value))
                   .Select(x => new { Match = x, IsStrictMatch = ODataNameMatchResolver.Strict.IsMatch(fieldFunc(x), value) })
                   .OrderBy(x => x.IsStrictMatch ? 0 : 1)
                   .Select(x => x.Match).FirstOrDefault());
        }
示例#7
0
        /// <summary>
        /// Creates a new instance of the <see cref="TypeCacheResolver"/> class.
        /// </summary>
        /// <param name="type">Type of the cached properties.</param>
        /// <param name="nameMatchResolver">Name match resolver.</param>
        /// <param name="dynamicType">Whether the cached type is dynamic.</param>
        /// <param name="dynamicContainerName">Dynamic container name.</param>
        public TypeCacheResolver(Type type, INameMatchResolver nameMatchResolver = null, bool dynamicType = false, string dynamicContainerName = "DynamicProperties")
        {
            _nameMatchResolver = nameMatchResolver;

            Type                      = type;
            IsDynamicType             = dynamicType;
            DynamicPropertiesName     = dynamicContainerName;
            TypeInfo                  = type.GetTypeInfo();
            AllProperties             = type.GetAllProperties().ToList();
            DeclaredProperties        = type.GetDeclaredProperties().ToList();
            AllFields                 = type.GetAllFields().ToList();
            DeclaredFields            = type.GetDeclaredFields().ToList();
            MappedName                = type.GetMappedName();
            MappedProperties          = type.GetMappedProperties().ToList();
            MappedPropertiesWithNames = type.GetMappedPropertiesWithNames().ToList();

            IsAnonymousType = type.IsAnonymousType();
        }
        /// <summary>
        /// Creates a new instance of the <see cref="TypeCacheResolver"/> class.
        /// </summary>
        /// <param name="type">Type of the cached properties.</param>
        /// <param name="nameMatchResolver">Name match resolver.</param>
        /// <param name="dynamicType">Whether the cached type is dynamic.</param>
        /// <param name="dynamicContainerName">Dynamic container name.</param>
        public TypeCacheResolver(Type type, INameMatchResolver nameMatchResolver, bool dynamicType = false, string dynamicContainerName = "DynamicProperties")
        {
            _nameMatchResolver = nameMatchResolver;

            Type                      = type;
            DerivedTypes              = type.DerivedTypes().ToList();
            IsDynamicType             = dynamicType;
            DynamicPropertiesName     = dynamicContainerName;
            TypeInfo                  = type.GetTypeInfo();
            AllProperties             = type.GetAllProperties().ToList();
            DeclaredProperties        = type.GetDeclaredProperties().ToList();
            AllFields                 = type.GetAllFields().ToList();
            DeclaredFields            = type.GetDeclaredFields().ToList();
            MappedName                = type.GetMappedName();
            MappedProperties          = type.GetMappedProperties().ToList();
            MappedPropertiesWithNames = type.GetMappedPropertiesWithNames().ToList();

            IsAnonymousType     = type.IsAnonymousType();
            AnnotationsProperty = AllProperties.FirstOrDefault(x => x.PropertyType == typeof(ODataEntryAnnotations));
        }
示例#9
0
    public static bool NamedKeyValuesMatchKeyNames(
        IDictionary <string, object>?namedKeyValues,
        INameMatchResolver resolver,
        IEnumerable <string>?keyNames,
        out IEnumerable <KeyValuePair <string, object> >?matchingNamedKeyValues)
    {
        matchingNamedKeyValues = null;
        if (namedKeyValues is null || keyNames is null)
        {
            return(false);
        }

        if (keyNames.Count() == namedKeyValues.Count)
        {
            var tmpMatchingNamedKeyValues = new List <KeyValuePair <string, object> >();
            foreach (var keyProperty in keyNames)
            {
                var namedKeyValue = namedKeyValues.FirstOrDefault(x => resolver.IsMatch(x.Key, keyProperty));
                if (namedKeyValue.Key != null)
                {
                    tmpMatchingNamedKeyValues.Add(new KeyValuePair <string, object>(keyProperty, namedKeyValue.Value));
                }
                else
                {
                    break;
                }
            }

            if (tmpMatchingNamedKeyValues.Count == keyNames.Count())
            {
                matchingNamedKeyValues = tmpMatchingNamedKeyValues;
                return(true);
            }
        }

        return(false);
    }
示例#10
0
 /// <summary>
 /// Creates a new instance of the <see cref="TypeCache"/> class.
 /// </summary>
 /// <param name="converter"></param>
 /// <param name="nameMatchResolver"></param>
 public TypeCache(ITypeConverter converter, INameMatchResolver nameMatchResolver)
 {
     _cache            = new ConcurrentDictionary <Type, TypeCacheResolver>();
     NameMatchResolver = nameMatchResolver ?? ODataNameMatchResolver.Strict;;
     Converter         = converter;
 }
 protected MetadataBase(INameMatchResolver nameMatchResolver, bool ignoreUnmappedProperties, bool unqualifiedNameCall)
 {
     IgnoreUnmappedProperties = ignoreUnmappedProperties;
     NameMatchResolver        = nameMatchResolver;
     UnqualifiedNameCall      = unqualifiedNameCall;
 }
 public static ODataClientSettings WithNameResolver(this ODataClientSettings settings, INameMatchResolver resolver)
 {
     settings.NameMatchResolver = resolver;
     return(settings);
 }
示例#13
0
 public static bool AllMatch(IEnumerable <string> subset, IEnumerable <string> superset, INameMatchResolver matchResolver)
 {
     return(subset.All(x => superset.Any(y => matchResolver.IsMatch(x, y))));
 }
示例#14
0
 public static bool ContainsMatch(IEnumerable <string> actualNames, string requestedName, INameMatchResolver matchResolver)
 {
     return(actualNames.Any(x => matchResolver.IsMatch(x, requestedName)));
 }
示例#15
0
 internal static ITypeCache TypeCache(string uri, INameMatchResolver nameMatchResolver)
 {
     return(_typeCaches.GetOrAdd(uri, new TypeCache(CustomConverters.Converter(uri), nameMatchResolver)));
 }
示例#16
0
 public Metadata(IEdmModel model, INameMatchResolver nameMatchResolver, bool ignoreUnmappedProperties, bool unqualifiedNameCall) : base(nameMatchResolver, ignoreUnmappedProperties, unqualifiedNameCall)
 {
     _model = model;
 }
 protected IODataClient CreateClientWithNameResolver(INameMatchResolver nameMatchResolver)
 {
     return(new ODataClient(CreateDefaultSettingsWithNameResolver(nameMatchResolver)));
 }