Created
January 5, 2012 02:36
-
-
Save CoreyKaylor/1563424 to your computer and use it in GitHub Desktop.
Transforms InputModel for FubuMVC into the route for asset files, this makes routes refactor friendly and safe from route changes
This file contains hidden or 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
public class ModelUrlResolutionCache : IModelUrlResolver | |
{ | |
static Cache<string, string> _inputModelTypeCache; | |
public ModelUrlResolutionCache(IUrlRegistry urlRegistry, BehaviorGraph graph) | |
{ | |
if (_inputModelTypeCache == null) | |
_inputModelTypeCache = new Cache<string, string>(inputModel => | |
{ | |
var inputType = Type.GetType(inputModel) | |
?? | |
graph.Routes.Where(act => act.Input != null | |
&& | |
act.Input.InputType.Name == inputModel) | |
.First().Input.InputType; | |
var parameters = new RouteParameters(); | |
parameters["ItemId"] = string.Empty; | |
return urlRegistry.UrlFor(inputType, parameters).Substring(1); | |
}); | |
} | |
public string GetUrlForInputModelName(string modelType) | |
{ | |
return _inputModelTypeCache[modelType]; | |
} | |
} |
This file contains hidden or 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
var urlTransformer = new JavascriptTransformerPolicy<UrlTransformer>(ActionType.Transformation, ".js"); | |
registry.Services(x => | |
{ | |
x.FillType<IModelUrlResolver, ModelUrlResolutionCache>(); | |
x.AddService<ITransformerPolicy>(urlTransformer); | |
}); |
This file contains hidden or 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
public class UrlTransformer : ITransformer | |
{ | |
readonly IModelUrlResolver _urlResolver; | |
public UrlTransformer(IModelUrlResolver urlResolver) | |
{ | |
_urlResolver = urlResolver; | |
} | |
public string Transform(string contents, IEnumerable<AssetFile> files) | |
{ | |
var regex = new Regex(@"url::(?<InputModel>[A-Za-z\.]+)"); | |
var matches = regex.Matches(contents); | |
var replacements = findReplacements(matches).Distinct(); | |
var replacedContents = contents; | |
replacements.Each(r => | |
{ | |
string url = _urlResolver.GetUrlForInputModelName(r); | |
var alteredUrl = @"url::{0}".ToFormat(r); | |
replacedContents = replacedContents.Replace(alteredUrl, url); | |
}); | |
if (replacedContents.Contains("url::")) | |
throw new UrlTransformationException(contents, files); | |
return replacedContents; | |
} | |
private IEnumerable<string> findReplacements(MatchCollection matchCollection) | |
{ | |
return from Match match in matchCollection select match.Groups["InputModel"].Value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm don't see IModelUrlResolver anywhere, but I got the idea..