Asp.net mvc 模型中的Sitecore Glass Mapper构造函数依赖项注入-在MVC中是否可能?
在web表单中,我会在模型中使用构造函数依赖项注入,如下所示:Asp.net mvc 模型中的Sitecore Glass Mapper构造函数依赖项注入-在MVC中是否可能?,asp.net-mvc,dependency-injection,sitecore,sitecore7.2,glass-mapper,Asp.net Mvc,Dependency Injection,Sitecore,Sitecore7.2,Glass Mapper,在web表单中,我会在模型中使用构造函数依赖项注入,如下所示: [SitecoreType(AutoMap = true)] public class Article { private readonly ICommonService _commonService; public Article(ICommonService commonService) { _commonService = commonService; } [Site
[SitecoreType(AutoMap = true)]
public class Article
{
private readonly ICommonService _commonService;
public Article(ICommonService commonService)
{
_commonService = commonService;
}
[SitecoreId]
private Guid Id { get; set; }
public string Title { get; set; }
[SitecoreIgnore]
public string GetTestString
{
get { return _commonService.GetTestString(); }
}
}
这里的想法是将逻辑移动到服务中,并保持与DI的松散耦合。因此,Glass提供原始Sitecore数据,然后这些服务帮助处理这些数据或拉入额外数据以完成模型
是否可以使用对解析DI的模型的引用进行视图渲染,并且该模型已准备好使用
@继承Glass.Mapper.Sc.Web.Mvc.GlassView
当前,当我尝试此操作时,我得到
没有为此对象定义无参数构造函数
我可以通过使用控制器并通过控制器将依赖项传递到模型中来实现上述功能
有没有可能在简单视图渲染中完成这项工作,以避免为除了简单数据之外还需要逻辑的模型创建控制器视图
目前发布在Glass Mapper Google Group上:您可以使用
ObjectConstruction
管道进行此操作。您需要添加一个新类,该类实现IObjectConstructionTask
,并使用IoC容器解析您的类,即:
public class IoCResolvingTask : IObjectConstructionTask
{
public virtual void Execute(ObjectConstructionArgs args)
{
// check that no other task has created an object and that this is a dynamic object
if (args.Result != null || args.Configuration.Type.IsAssignableFrom(typeof(IDynamicMetaObjectProvider))) return;
// create instance using your container
var obj = Container.Resolve(args.Configuration.Type);
// map properties from item to model
args.Configuration.MapPropertiesToObject(obj, args.Service, args.AbstractTypeCreationContext);
// set the new object as the returned result
args.Result = obj;
}
}
然后您需要将您的任务注册到Glass:
public static void CastleConfig(IWindsorContainer container){
var config = new Config();
container.Register(Component.For<IObjectConstructionTask>().ImplementedBy<IoCResolvingTask>().LifestylePerWebRequest());
container.Install(new SitecoreInstaller(config));
}
publicstaticvoidcastleconfig(IWindsorContainer){
var config=new config();
container.Register(Component.For().ImplementedBy().LifestylePerWebRequest());
container.Install(新的SitecoreInstaller(配置));
}
是否可以参照模型进行视图渲染
哪个解析DI并且模型可以使用?:@inherits
Glass.Mapper.Sc.Web.Mvc.GlassView
答案是肯定的,这是可以做到的
在模型中,DI是自动解析的。从控制器和其他区域,我使用服务定位器模式:
我遇到的问题是设置和配置。下面是我实现的代码,以使其成功运行
glassMapperCustom.cs
public static void CastleConfig(IWindsorContainer container)
{
var config = new Config { UseWindsorContructor = true };
container.Install(new SitecoreInstaller(config));
container.Install(new ServiceInstaller());
}
服务安装程序代码
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<ICustomService>().ImplementedBy<CustomService>().LifestyleTransient(),
Component.For<SitecoreController>().ImplementedBy<SitecoreController>().LifestylePerWebRequest()
.DependsOn(new { databaseName = Sitecore.Context.Database }));
// Set up the servicelocator. We can use this in the code to get instances of services.
ServiceLocator.SetLocatorProvider(() =>
new WindsorServiceLocator(container));
}
public void安装(IWindsorContainer,IConfigurationStore)
{
集装箱。登记(
Component.For().ImplementedBy().LifestyleTransient(),
Component.For().ImplementedBy().LifestylePerWebRequest()实现
.DependsOn(新的{databaseName=Sitecore.Context.Database});
//设置servicelocator。我们可以在代码中使用它来获取服务实例。
ServiceLocator.SetLocatorProvider(()=>
新WindsorService运输机(集装箱);
}
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"SiteName", // Route name
"SiteName/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = System.Web.Mvc.UrlParameter.Optional }); // Parameter defaults
}
/// <summary>
/// Registers the global filters.
/// </summary>
/// <param name="filters">Global filter collection.</param>
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
/// <summary>
/// Will be called when the application first starts.
/// </summary>
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
publicstaticvoidregisterOutes(路由收集路由)
{
routes.MapRoute(
“SiteName”,//路由名称
“SiteName/{controller}/{action}/{id}”,//带参数的URL
新建{controller=“Home”,action=“Index”,id=System.Web.Mvc.urlparmeter.Optional});//参数默认值
}
///
///注册全局筛选器。
///
///全局筛选器集合。
公共静态无效注册表全局过滤器(全局过滤器集合过滤器)
{
filters.Add(新HandleErrorAttribute());
}
///
///将在应用程序首次启动时调用。
///
受保护的无效应用程序\u Start()
{
RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
注册地址(RouteTable.Routes);
}
我将此代码添加到您的第一个代码段中,因为我没有对容器的引用:专用只读IWindsorContainer\u容器;publicIOCResolvingTask(IWindsorContainer){u container=container;}应该是这样吗?然后我也添加了最终代码,但仍然得到相同的错误您是否也将容器传递给castle注册,如下所示:container.Register(Component.For().ImplementedBy().DependsOn(Dependency.OnValue(container.LifestylePerWebRequest())
?对于您的代码,您必须将容器作为构造函数参数传递。我不太了解castle,也不知道此注释中的代码段是否有效,还没有对其进行测试。您好,我没有这样注册,但是我现在已经更新了,但仍然会收到相同的错误。您的IConmonService是否已在您传递到构造函数中的容器中注册r?它是否与Glass注册在同一个容器中?或者它是另一个franework/容器?调试器怎么说?是否调用构造任务?这里是Glass关于如何使用它的教程:感谢您的帮助Kevin。