DevExpress 与 Autofac 简单网格绑定
我正在尝试构建一个真正简单的 MVC DataBind 示例。我正在比较 MVC3 中的 Telerik 与 DevExpress Grid。目标之一是在 DDD 方法中使用 Enitiy Framework 和 Autofac,这使其尽可能接近我们的项目当前以及使用新控件时的情况。创建最公平的测试。
Telerik 轻而易举,我想象 DevExpress 也同样易于使用,但我不断遇到无法解决的异常。
{"本次解析操作已经结束。注册时 使用 lambda 的组件,将 IComponentContext 'c' 参数传递给 lambda 无法存储。相反,要么解析 IComponentContext 再次从“c”开始,或者解析一个 Func<>基础工厂来创建后续 组件来自。"}
做了一些研究,我已经调用了 c.Resolve(),很多人说这是解决方案,所以我不确定为什么我不断遇到这个问题,Telerik 没有遇到同样的问题 我很确定
这不是 DevExpress 问题,我认为 autofac 做错了。但是,如果这就是 DevExpress 和 autofac 一起工作的方式,这将是一个问题,因为我们严重依赖 autofac,我真的很讨厌。做一些做作的事来达到目的当 Telerik 开箱即用时可以轻松工作
有人可以告诉我我是否做错了什么并指出正确的方向,或者告诉我这是否是 DevExpress 和 autofac 问题而不是可以解决的问题。轻松修复并需要解决方法吗?
VIEW
@using System.Web.UI.WebControls
@model IEnumerable<Domain.Entities.FactResellerSale>
@{
ViewBag.Title = "GridView";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>GridView</h2>
@Html.DevExpress().GridView(
settings =>
{
settings.Name = "gvData";
settings.Width = Unit.Percentage(100);
settings.SettingsText.Title = "Fact Resllers Sale";
settings.Settings.ShowTitlePanel = true;
settings.Settings.ShowStatusBar = GridViewStatusBarMode.Visible;
settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
settings.SettingsPager.AllButton.Text = "All";
settings.SettingsPager.PageSize = 10;
}
).Bind(Model).GetHtml()
Controller
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Domain.Entities;
using Domain.Repository;
namespace DevExpressMvcRazor.Controllers
{
public class GridViewController : Controller
{
private readonly IAdventureRepository _repository;
public GridViewController(IAdventureRepository repository)
{
_repository = repository;
}
//
// GET: /GridView/
public ActionResult GridView()
{
return View("GridView", GetFactResllerSales());
}
private IList<FactResellerSale> GetFactResllerSales()
{
return _repository.GetFactResllerSales().Take(10).ToList();
}
}
}
Global.asax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
namespace DevExpressMvcRazor
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
var builder = new ContainerBuilder();
builder.RegisterModule<DevExpressModule>();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
DevExpressModule
using Autofac;
using Autofac.Integration.Mvc;
using Domain;
using Infrastructure;
namespace DevExpressMvcRazor
{
public class DevExpressModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterModule<InfrastructureModule>();
builder.RegisterModule<DomainModule>();
builder.RegisterModule<AutofacWebTypesModule>();
}
}
}
InfrastructureModule< /强>
public class InfrastructureModule : Module
{
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
builder.Register(c => new PropertyInjectedLazyLoadedObjectContextFactory(c.IsRegistered, c.Resolve))
.As<IObjectContextFactory>()
.InstancePerLifetimeScope();
builder.Register(c => new UnitOfWork(c.Resolve<IObjectContextFactory>()))
.As<ISession>()
.As<IObjectContextProvider>()
.InstancePerLifetimeScope();
//Repositories
builder.Register(c => new AdventureRepository(c.Resolve<IObjectContextProvider>()))
.As<IAdventureRepository>()
.InstancePerLifetimeScope();
}
}
存储库
public class AdventureRepository : IAdventureRepository
{
private readonly IObjectContextProvider _contextProvider ;
public AdventureRepository(IObjectContextProvider contextProvider)
{
_contextProvider = contextProvider;
}
public IQueryable<FactResellerSale> GetFactResllerSales()
{
return _contextProvider.GetContext<TelerikVsDevExpressModelContext>().GetIQueryable<FactResellerSale>();
}
}
对于 Telerik,其他所有内容都是相同的,因此我将仅发布 Telerik 工作正常的视图。 Telerik View
@model IEnumerable<Domain.Entities.FactResellerSale>
@{
ViewBag.Title = "GridView";
}
<h2>GridView</h2>
@(Html.Telerik().Grid(Model)
.Name("Grid")
.PrefixUrlParameters(false)
.Columns(columns =>
{
columns.Bound(o => o.ProductKey).Width(50);
columns.Bound(o => o.DimDate.FullDateAlternateKey);
columns.Bound(o => o.DimReseller.ResellerName);
columns.Bound(o => o.DimEmployee.FullName);
columns.Bound(o => o.SalesOrderNumber);
})
.Groupable()
.Pageable()
.Sortable()
.Filterable()
)
我正在使用:
- MVC 3
- Autofac 2.5.2.830
- DevExpress 11.1.8.0
- Telerik 2011.3.1115.340
I'm trying to put together a real simple MVC DataBind sample. I'm comparing Telerik vs DevExpress Grid in MVC3. One of the goals was to use Enitiy Framework and Autofac in a DDD approach, this making it as close to how our projects are currently and will be when using the new controls. Creating the fairest test.
Telerik was a breeze and I have to imagine that DevExpress is just as easy to use but I keep running into an exception which I can't get solved.
{"This resolve operation has already ended. When registering
components using lambdas, the IComponentContext 'c' parameter to the
lambda cannot be stored. Instead, either resolve IComponentContext
again from 'c', or resolve a Func<> based factory to create subsequent
components from."}
I did some research on it and I was already calling the c.Resolve() which many said was the fix, so I'm not sure why I keep getting this problem, Telerik had no trouble with the same exact setup.
I'm pretty sure its not a DevExpress issue and something I'm doing wrong with autofac I think. However if this is how DevExpress and autofac work together this will be a problem since we heavily rely on autofac and I really would hate to do something hokey to get it to work when Telerik works so easily out of the box.
Can someone please tell me if I am doing something wrong and point me in the right direction, or tell me if this is a DevExpress and autofac issue and not something that can be fixed easily and requires a workaround?
VIEW
@using System.Web.UI.WebControls
@model IEnumerable<Domain.Entities.FactResellerSale>
@{
ViewBag.Title = "GridView";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>GridView</h2>
@Html.DevExpress().GridView(
settings =>
{
settings.Name = "gvData";
settings.Width = Unit.Percentage(100);
settings.SettingsText.Title = "Fact Resllers Sale";
settings.Settings.ShowTitlePanel = true;
settings.Settings.ShowStatusBar = GridViewStatusBarMode.Visible;
settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
settings.SettingsPager.AllButton.Text = "All";
settings.SettingsPager.PageSize = 10;
}
).Bind(Model).GetHtml()
Controller
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Domain.Entities;
using Domain.Repository;
namespace DevExpressMvcRazor.Controllers
{
public class GridViewController : Controller
{
private readonly IAdventureRepository _repository;
public GridViewController(IAdventureRepository repository)
{
_repository = repository;
}
//
// GET: /GridView/
public ActionResult GridView()
{
return View("GridView", GetFactResllerSales());
}
private IList<FactResellerSale> GetFactResllerSales()
{
return _repository.GetFactResllerSales().Take(10).ToList();
}
}
}
Global.asax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
namespace DevExpressMvcRazor
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
var builder = new ContainerBuilder();
builder.RegisterModule<DevExpressModule>();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
DevExpressModule
using Autofac;
using Autofac.Integration.Mvc;
using Domain;
using Infrastructure;
namespace DevExpressMvcRazor
{
public class DevExpressModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterModule<InfrastructureModule>();
builder.RegisterModule<DomainModule>();
builder.RegisterModule<AutofacWebTypesModule>();
}
}
}
InfrastructureModule
public class InfrastructureModule : Module
{
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
builder.Register(c => new PropertyInjectedLazyLoadedObjectContextFactory(c.IsRegistered, c.Resolve))
.As<IObjectContextFactory>()
.InstancePerLifetimeScope();
builder.Register(c => new UnitOfWork(c.Resolve<IObjectContextFactory>()))
.As<ISession>()
.As<IObjectContextProvider>()
.InstancePerLifetimeScope();
//Repositories
builder.Register(c => new AdventureRepository(c.Resolve<IObjectContextProvider>()))
.As<IAdventureRepository>()
.InstancePerLifetimeScope();
}
}
Repository
public class AdventureRepository : IAdventureRepository
{
private readonly IObjectContextProvider _contextProvider ;
public AdventureRepository(IObjectContextProvider contextProvider)
{
_contextProvider = contextProvider;
}
public IQueryable<FactResellerSale> GetFactResllerSales()
{
return _contextProvider.GetContext<TelerikVsDevExpressModelContext>().GetIQueryable<FactResellerSale>();
}
}
Everything else is the same for Telerik so I will just post the View which the Telerik one works no problem.
Telerik View
@model IEnumerable<Domain.Entities.FactResellerSale>
@{
ViewBag.Title = "GridView";
}
<h2>GridView</h2>
@(Html.Telerik().Grid(Model)
.Name("Grid")
.PrefixUrlParameters(false)
.Columns(columns =>
{
columns.Bound(o => o.ProductKey).Width(50);
columns.Bound(o => o.DimDate.FullDateAlternateKey);
columns.Bound(o => o.DimReseller.ResellerName);
columns.Bound(o => o.DimEmployee.FullName);
columns.Bound(o => o.SalesOrderNumber);
})
.Groupable()
.Pageable()
.Sortable()
.Filterable()
)
I'm using:
- MVC 3
- Autofac 2.5.2.830
- DevExpress 11.1.8.0
- Telerik 2011.3.1115.340
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题在这里:
为了向 c (IComponentContext) 注入句柄,您必须首先解决它。像这样更改您的代码:
Your problem is here:
In order to inject a handle to c (IComponentContext) you must resolve it first. Change your code like so: