フレームワーク サイトの Mvc3 実装を行うという使命に着手しましたが、現在は既存の WebForms 実装を使用しています。
このタスクにより、IoC と DI を Structuremap と統合して柔軟性を高めることができました。
背景を説明すると、次のプロジェクト構造があります。
App.Core <- コア クラス ライブラリ
App.Mvc <- Mvc クラス ライブラリ
App.Mvc.Web <- Mvc3 APP
App.WebForms <- Webform クラス ライブラリ
App.WebForms.Web <- Webform アプリまたはサイト
Mvc と WebForms の両方の実装で Mvc のルーティングを使用しています。IRouteProvider を使用して、オーチャード プロジェクトと同じ方法でルートを公開します。IRouteProvider の N 個の実装を作成して、パブリッシャーが登録するルートを追加できます。優先度順に並べます。
これは正常に機能しています。MVC の UrlHelper または WebForms の Page.GetRouteUrl でルートを登録して使用できます。
問題は、App.Core がこれらのルート (すべてではなく、最も一般的なルートの一部) を解決できるようにする必要があり、これらは実装されているサイトに応じて変更される可能性があることです。
たとえば、製品詳細のデフォルト ルートは「/{category}/{product_name}/{product_id}」ですが、これをオーバーライドする機能が必要で、特定のサイトでは「/{brand}/{product_name}/{product_id}」となります}" 代わりは。
つまり、Core では、RouteTable.Routes.GetVirtualPath を単純に一連の固定パラメーターで使用することはできません。これは、これらがサイトごとに変わる可能性があるためです。
最も一般的なルートのメソッドを備えた IRouteResolver インターフェイスを作成しました。これには、各クラス ライブラリ (Mvc または Webforms) で SM に登録されている既定の実装がありますが、各サイトでオーバーライドすることもできます。
インターフェイスは次のようになります。
public interface IRouteResolver
{
string GetRouteUrl(object routeParameters);
string GetRouteUrl(RouteValueDictionary routeParameters);
string GetRouteUrl(string routeName, object routeParameters);
string GetRouteUrl(string routeName, RouteValueDictionary routeParameters);
string GetUrlFor(Product product);
string GetUrlFor(Category category);
string GetUrlFor(Brand brand);
}
インターフェイスのデフォルトの Mvc 実装は次のようになります。
public class MvcRouteResolver : IRouteResolver
{
UrlHelper _urlHelper;
ICategoryModelBroker _categoryModelBroker;
IBrandModelBroker _brandModelBroker;
IProductModelBroker _productModelBroker;
public MvcRouteResolver(UrlHelper urlHelper)
{
_urlHelper = urlHelper;
_categoryModelBroker = ObjectFactory.GetInstance<ICategoryModelBroker>();
_brandModelBroker = ObjectFactory.GetInstance<IBrandModelBroker>();
_productModelBroker = ObjectFactory.GetInstance<IProductModelBroker>();
}
public string GetRouteUrl(object routeParameters)
{
return GetRouteUrl(new RouteValueDictionary(routeParameters));
}
public string GetRouteUrl(System.Web.Routing.RouteValueDictionary routeParameters)
{
return GetRouteUrl(null, new RouteValueDictionary(routeParameters));
}
public string GetRouteUrl(string routeName, object routeParameters)
{
return GetRouteUrl(routeName, new RouteValueDictionary(routeParameters));
}
public string GetRouteUrl(string routeName, System.Web.Routing.RouteValueDictionary routeParameters)
{
return _urlHelper.RouteUrl(routeName, routeParameters);
}
public string GetUrlFor(Product product)
{
string category = string.Empty;
if (product.Categories.Count > 0)
category = product.Categories[0].Breadcrumb.Replace("@@", "-");
else if (product.Brands.Any())
category = product.Brands.FirstOrDefault().Name;
else
category = "detail";
return GetRouteUrl(new { controller="Product", action="Detail", productId = product.Id, brandName = _productModelBroker.GetSlug(product), productName = _productModelBroker.GetSluggedName(product) });
}
public string GetUrlFor(Category category)
{
return GetRouteUrl(new { controller = "Product", action = "ListByCategory", id = category.Id, name = _categoryModelBroker.GetSlug(category) });
}
public string GetUrlFor(Brand brand)
{
return GetRouteUrl(new { controller = "Product", action = "ListByBrand", id = brand.Id, name = _brandModelBroker.GetSlug(brand) });
}
}
デフォルトの WebForms 実装は次のようになります。
public class WebRouteResolver : IRouteResolver
{
Control _control;
HttpContext _context;
public WebRouteResolver()
:this(HttpContext.Current)
{
}
public WebRouteResolver(HttpContext context)
{
_context = context;
}
public WebRouteResolver(Control control)
{
_control = control;
}
public WebRouteResolver(Page page)
{
_control = page as Control;
}
public string GetRouteUrl(object routeParameters)
{
return GetRouteUrl(new RouteValueDictionary(routeParameters));
}
public string GetRouteUrl(System.Web.Routing.RouteValueDictionary routeParameters)
{
return GetRouteUrl(null, new RouteValueDictionary(routeParameters));
}
public string GetRouteUrl(string routeName, object routeParameters)
{
return GetRouteUrl(routeName, new RouteValueDictionary(routeParameters));
}
public string GetRouteUrl(string routeName, System.Web.Routing.RouteValueDictionary routeParameters)
{
VirtualPathData virtualPath = null;
if(_control.IsNotNull())
virtualPath = RouteTable.Routes.GetVirtualPath(_control.Page.Request.RequestContext, routeName, routeParameters);
else
virtualPath = RouteTable.Routes.GetVirtualPath(_context.Request.RequestContext, routeName, routeParameters);
if (virtualPath != null)
{
return virtualPath.VirtualPath;
}
return null;
}
private string ResolveUrl(string originalUrl)
{
if(_control.IsNotNull())
return _control.ResolveUrl(originalUrl);
// *** Absolute path - just return
if (originalUrl.IndexOf("://") != -1)
return originalUrl;
// *** Fix up image path for ~ root app dir directory
if (originalUrl.StartsWith("~"))
{
string newUrl = "";
if (_context != null)
newUrl = _context.Request.ApplicationPath +
originalUrl.Substring(1).Replace("//", "/");
else
// *** Not context: assume current directory is the base directory
throw new ArgumentException("Invalid URL: Relative URL not allowed.");
// *** Just to be sure fix up any double slashes
return newUrl;
}
return originalUrl;
}
public string GetUrlFor(Product product)
{
string category = string.Empty;
if (product.Categories.Count > 0)
category = product.Categories[0].Breadcrumb.Replace("@@", "-");
else if (product.Brands.Any())
category = product.Brands.FirstOrDefault().Name;
else
category = "detail";
if (Config.RoutingEnabled)
{
return GetRouteUrl(new { @category = CommonHelper.ToFriendlyUrl(category), name = CommonHelper.ToFriendlyUrl(product.Name), id = product.Id });
}
return ResolveUrl(Config.GetStoreSetting("productDetailUrl")) + "?id={0}&name={1}&category={2}".Fill(product.Id, CommonHelper.ToFriendlyUrl(product.Name), CommonHelper.ToFriendlyUrl(category));
}
public string GetUrlFor(Category category)
{
string breadcrumb = category.Breadcrumb.Replace("@@", "-");
if (Config.RoutingEnabled)
return GetRouteUrl(new { @category = CommonHelper.ToFriendlyUrl(breadcrumb), category_id = category.Id});
return ResolveUrl(Config.GetStoreSetting("productListingUrl") + "?category_id={0}&category={1}".Fill(category.Id, CommonHelper.ToFriendlyUrl(category.Name)));
}
public string GetUrlFor(Brand brand)
{
if (Config.RoutingEnabled)
return GetRouteUrl(new { @brand = CommonHelper.ToFriendlyUrl(brand.Name), brand_id = brand.Id });
return ResolveUrl(Config.GetStoreSetting("productListingUrl") + "?brand_id={0}&brand={1}".Fill(brand.Id, CommonHelper.ToFriendlyUrl(brand.Name)));
}
}
問題は、コンストラクターの引数 (Mvc の UrlHelper および Web フォームの Page または Control) のために、IRouteResolver プラグインのインスタンスを取得するために SM を使用する代わりに具象型を使用することを余儀なくされることです。
たとえば、ページまたはコントロールでリゾルバーを使用できるようにするために、次の拡張機能があります。
public static IRouteResolver RouteResolver(this Control control)
{
return new WebRouteResolver(control);
}
public static IRouteResolver RouteResolver(this Page page)
{
return new WebRouteResolver(page);
}
これは Web または Mvc のデフォルトの動作をカバーしますが、サイトごとにリゾルバーを具体的にオーバーライドしたい場合はカバーしません。
これらのコンストラクター引数をプラグインとして SM に追加しても安全ですか?
この機能要求に対して推奨できる別のアプローチ/パターンはありますか?
どんなアイデアや提案も大歓迎です。
どうもありがとう、P.