Skip to content

Instantly share code, notes, and snippets.

Created April 14, 2014 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7df42a490d3aca2606b1 to your computer and use it in GitHub Desktop.
Save anonymous/7df42a490d3aca2606b1 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using FubuMVC.Core.Registration;
namespace FieldBook.Interface.Navigation
{
public class NavigationPolicy : IConfigurationAction
{
public void Configure(BehaviorGraph graph)
{
var visitor = new NavigationVisitor();
graph.Behaviors.Each(x => visitor.VisitBehavior(x));
// graph.VisitBehaviors(visitor);
visitor.Register(graph.Services.AddService);
}
}
}
using System;
using System.Collections.Generic;
using FubuCore.Reflection;
using FubuMVC.Core.Registration.Nodes;
using FubuMVC.Core.Registration.ObjectGraph;
using StructureMap;
namespace FieldBook.Interface.Navigation
{
public class NavigationVisitor //: IBehaviorVisitor
{
private readonly ListDependency _policies = new ListDependency(typeof(IEnumerable<IMenuPolicy>));
private readonly List<IMenuPolicy> _menuPolicies = new List<IMenuPolicy>();
public NavigationVisitor()
{
}
public void VisitBehavior(BehaviorChain chain)
{
if(chain != null && chain.FirstCall() != null && chain.FirstCall().Method.HasAttribute<NavigationAttribute>() && chain.InputType() != null)
{
ActionCall actionCall = chain.FirstCall();
var attr = actionCall.Method.GetAllAttributes<NavigationAttribute>();
foreach (var navigationAttribute in attr)
{
// _observer.RecordCallStatus(actionCall, "NavigationVisitor: Matched on has navigation attribute filter");
Type ruleType = typeof(ContextualMenuRule<>).MakeGenericType(chain.InputType());
var depDef = _policies.AddType(typeof(MenuPolicy<>).MakeGenericType(chain.InputType()));
var ruleDef = new ObjectDef(ruleType);
ruleDef.Dependency(new ValueDependency(typeof(ActionCall), actionCall));
ruleDef.Dependency(new ValueDependency(typeof(NavigationAttribute), navigationAttribute));
depDef.Dependency(new ConfiguredDependency(typeof(IMenuRule<>).MakeGenericType(chain.InputType()), ruleDef));
}
}
}
public IEnumerable<ObjectDef> AllMenus
{
get
{
return _policies.Items;
}
}
public void Register(Action<Type, ObjectDef> register)
{
var cacheDef = new ObjectDef(typeof (MenuRegistry));
cacheDef.Dependency(_policies);
register(typeof (IMenuRegistry), cacheDef);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment