Skip to content

Instantly share code, notes, and snippets.

@Cyberlane
Last active October 19, 2015 21:42
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 Cyberlane/9dbcc37fff85777716f1 to your computer and use it in GitHub Desktop.
Save Cyberlane/9dbcc37fff85777716f1 to your computer and use it in GitHub Desktop.
public class HomeMetadataModule : MetadataModule<MetadataModel>
{
public HomeMetadataModule()
{
Describe["/"] = _ => new MetadataModel
{
Index = 1
};
}
}
public class MetadataModel
{
public int Index { get; set; }
}
public class MetadataRouteProvider : IRouteMetadataProvider
{
public Type GetMetadataType(INancyModule module, RouteDescription routeDescription)
{
return typeof (MetadataModel);
}
public object GetMetadata(INancyModule module, RouteDescription routeDescription)
{
var moduleType = module.GetType();
var moduleName = moduleType.FullName;
var parts = moduleName.Split('.').ToArray();
if (parts[0] != GetType().FullName.Split('.')[0]) return null;
if (parts[parts.Length - 2] != "Modules") return null;
parts[parts.Length - 2] = "Metadata";
parts[parts.Length - 1] = ReplaceModuleWithMetadataModule(parts[parts.Length - 1]);
var metadataModuleName = string.Join(".", parts);
var type = Type.GetType(metadataModuleName);
return type == null ? null : TinyIoCContainer.Current.Resolve(type);
}
private string ReplaceModuleWithMetadataModule(string moduleName)
{
var i = moduleName.LastIndexOf("Module", StringComparison.Ordinal);
return moduleName.Substring(0, i) + "MetadataModule";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment