【问题标题】:DevExpress with Autofac simple grid bindingDevExpress 与 Autofac 简单的网格绑定
【发布时间】:2011-11-22 20:14:39
【问题描述】:

我正在尝试组合一个真正简单的 MVC DataBind 示例。我在 MVC3 中比较 Telerik 和 DevExpress Grid。目标之一是在 DDD 方法中使用 Enitiy Framework 和 Autofac,这使得它与我们的项目当前和使用新控件时的情况一样接近。创建最公平的测试。

Telerik 轻而易举,我不得不想象 DevExpress 也同样易于使用,但我一直遇到无法解决的异常。

{"此解析操作已经结束。注册时 使用 lambdas 的组件,将 IComponentContext 'c' 参数传递给 无法存储 lambda。相反,要么解决 IComponentContext 再次从“c”,或解析基于 Func 的工厂以创建后续 组件来自。"}

我对它进行了一些研究,并且我已经调用了 c.Resolve(),许多人都说这是修复程序,所以我不确定为什么我一直遇到这个问题,Telerik 使用相同的确切设置没有任何问题。

我很确定这不是 DevExpress 问题,而且我认为我在使用 autofac 时做错了。但是,如果这是 DevExpress 和 autofac 一起工作的方式,这将是一个问题,因为我们严重依赖 autofac,而且当 Telerik 开箱即用如此轻松地工作时,我真的不想做一些蠢事来让它工作。

谁能告诉我我做错了什么并指出正确的方向,或者告诉我这是否是 DevExpress 和 autofac 问题,而不是可以轻松修复并需要解决方法的问题?

查看

@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()

控制器

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>();
        }
    }
}

基础设施模块

    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 视图

@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

【问题讨论】:

    标签: asp.net-mvc-3 devexpress autofac telerik-mvc


    【解决方案1】:

    你的问题在这里:

    
     builder.Register(c => new PropertyInjectedLazyLoadedObjectContextFactory(c.IsRegistered, c.Resolve))
                    .As<IObjectContextFactory>()
                    .InstancePerLifetimeScope();
    

    为了向 c (IComponentContext) 注入句柄,您必须先解析它。像这样更改您的代码:

    
     builder.Register(c => {
        var context = c.Resolve<IComponentContext>();
        return new PropertyInjectedLazyLoadedObjectContextFactory(context.IsRegistered, context.Resolve))
        }
      .As<IObjectContextFactory>()
      .InstancePerLifetimeScope();
    

    【讨论】:

    • 这可以修复它,但它仍然不能解释为什么 Telerik 没有这个就可以工作并且 DevExpress 抱怨了。
    • 这必须是一些间接的联系——例如每个人如何使用工厂。在任何情况下都需要进行更正 - 与 Autofac 一起使用时,原件是不正确的。很高兴它是固定的! :) nb
    猜你喜欢
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    相关资源
    最近更新 更多