Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Created June 2, 2010 23:55
Show Gist options
  • Save jmarnold/423214 to your computer and use it in GitHub Desktop.
Save jmarnold/423214 to your computer and use it in GitHub Desktop.
public class EntityMatcher
{
private readonly CompositeFilter<Type> _typeFilters = new CompositeFilter<Type>();
private readonly EntityDetailsUrlPolicy _urlPolicy = new EntityDetailsUrlPolicy();
private readonly IEntityViewModelResolver _entityViewModelResolver;
private BehaviorGraph _graph;
public EntityMatcher(IEntityViewModelResolver entityViewModelResolver)
{
_entityViewModelResolver = entityViewModelResolver;
}
public CompositeFilter<Type> TypeFilters { get { return _typeFilters; } set { } }
public void BuildBehaviors(TypePool pool, BehaviorGraph graph)
{
_graph = graph;
pool.TypesMatching(TypeFilters.Matches).Each(RegisterBehavior);
_graph = null;
}
private void RegisterBehavior(Type entityType)
{
var requestModelType = _entityViewModelResolver.GetRequestModel(entityType);
var detailsModelType = _entityViewModelResolver.GetDetailsModel(entityType);
bool detailsEndpointNotRequired = requestModelType == null && detailsModelType == null;
if(detailsEndpointNotRequired)
{
return;
}
var behaviorType = typeof(RenderEntityDetailsAction<,,>)
.MakeGenericType(entityType, requestModelType, detailsModelType);
var targetMethod = behaviorType
.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Single(m => m.Name == "Get");
var call = new ActionCall(behaviorType, targetMethod);
var chain = new BehaviorChain();
chain.AddToEnd(call);
chain.Route = _urlPolicy.Build(call);
_graph.AddChain(chain);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment