Created
September 22, 2010 22:53
-
-
Save pocheptsov/592735 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region | |
using System; | |
using System.Collections.Generic; | |
using System.Linq.Expressions; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using JazzMeeting.Reporting; | |
using JazzMeeting.Utils.Extensions; | |
using Lokad.Reflection; | |
#endregion | |
namespace JazzMeeting.Mvc | |
{ | |
public class PathResolver : IPathResolver | |
{ | |
private readonly Dictionary<Type, Dictionary<string, Func<object, object>>> _map = | |
new Dictionary<Type, Dictionary<string, Func<object, object>>> | |
{ | |
{ | |
typeof (ShowOrganizerReport), | |
new Dictionary<string, Func<object, object>> | |
{ | |
{ | |
Express.Property(() => new ShowOrganizerReport().LogoFileName).Name, | |
delegate(object obj) | |
{ | |
var _ = obj.As<ShowOrganizerReport>(); | |
return new {_.LogoFileName}; | |
} | |
}, | |
} | |
}, | |
}; | |
private readonly UrlHelper _urlHelper; | |
public PathResolver(RequestContext requestContext, RouteCollection routeCollection) | |
{ | |
_urlHelper = new UrlHelper(requestContext, routeCollection); | |
} | |
public string Resolve(object routeValues) | |
{ | |
return _urlHelper.RouteUrl(routeValues); | |
} | |
public string Resolve<TModel, T>(Expression<Func<T>> property) | |
{ | |
var body = property.Body as MemberExpression; | |
string propertyName = Express.Property(property).Name; | |
TModel owner = default(TModel); | |
if (body != null) | |
{ | |
owner = Expression.Lambda<Func<TModel>>(body.Expression).Compile()(); | |
} | |
return _urlHelper.RouteUrl(GetRouteDictionary(owner, propertyName)); | |
} | |
private object GetRouteDictionary<TModel>(TModel model, string propertyName) | |
{ | |
return GetMapping(model.GetType(), propertyName)(model); | |
} | |
private Func<dynamic, dynamic> GetMapping(Type type, string propertyName) | |
{ | |
return _map[type][propertyName]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment