Skip to content

Commit

Permalink
Fixed and simplified hub cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
pszmyd committed Apr 14, 2012
1 parent b535408 commit cd18cf9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
45 changes: 10 additions & 35 deletions SignalR/Hubs/Lookup/ReflectedHubDescriptorProvider.cs
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using SignalR.Infrastructure;

namespace SignalR.Hubs
{
Expand Down Expand Up @@ -38,22 +37,17 @@ public bool TryGetHub(string hubName, out HubDescriptor descriptor)
.SelectMany(GetTypesSafe)
.Where(IsHubType);

// Building a list of descriptors for each type
var descriptors = types.Select(type =>
new HubDescriptor
{
Name = GetHubName(type),
Type = type
});

// Building cache entries for each descriptor
// Each descriptor is stored in dictionary under several keys for allowing quick lookup:
// full type name, short type name and optionally a name given by attribute
var cacheEntries = descriptors
.SelectMany(desc => CacheKeysFor(desc.Type)
.Select(key => new { Descriptor = desc, Key = key }))
.ToDictionary(anon => anon.Key,
anon => anon.Descriptor,
// Each descriptor is stored in dictionary under a key
// that is it's name or the name provided by an attribute
var cacheEntries = types
.Select(type => new HubDescriptor
{
Name = type.GetHubName(),
Type = type
})
.ToDictionary(hub => hub.Name,
hub => hub,
StringComparer.OrdinalIgnoreCase);

return cacheEntries;
Expand All @@ -74,19 +68,6 @@ private static bool IsHubType(Type type)
}
}

private static IEnumerable<string> CacheKeysFor(Type type)
{
yield return type.FullName;
yield return type.Name;

var attributeName = GetHubName(type);

if (!String.Equals(attributeName, type.Name, StringComparison.OrdinalIgnoreCase))
{
yield return attributeName;
}
}

private static IEnumerable<Type> GetTypesSafe(Assembly a)
{
try
Expand All @@ -98,11 +79,5 @@ private static IEnumerable<Type> GetTypesSafe(Assembly a)
return Enumerable.Empty<Type>();
}
}

private static string GetHubName(Type type)
{
return ReflectionHelper.GetAttributeValue<HubNameAttribute, string>(type, attr => attr.HubName)
?? type.Name;
}
}
}
3 changes: 2 additions & 1 deletion SignalR/SignalR.csproj
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -54,6 +54,7 @@
<Compile Include="Hosting\IResponse.cs" />
<Compile Include="CommandType.cs" />
<Compile Include="ConnectionExtensions.cs" />
<Compile Include="Hubs\Extensions\HubTypeExtensions.cs" />
<Compile Include="Hubs\Extensions\HubManagerExtensions.cs" />
<Compile Include="Hubs\Lookup\DefaultParameterResolver.cs" />
<Compile Include="Hubs\Lookup\IParameterResolver.cs" />
Expand Down

0 comments on commit cd18cf9

Please sign in to comment.