DevExpress与Autofac简单的网格绑定

时间:2011-11-22 20:14:39

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

我试图整理一个真正简单的MVC DataBind示例。我在MVC3中比较Telerik与DevExpress Grid。其中一个目标是在DDD方法中使用Enitiy Framework和Autofac,这使得它与我们的项目当前和将来使用新控件的方式非常接近。创造最公平的测试。

Telerik是一件轻而易举的事,我不得不想象DevExpress同样易于使用,但我仍然遇到了一个我无法解决的例外情况。

  

{"此解析操作已经结束。注册时   使用lambdas的组件,IComponentContext' c'参数   lambda无法存储。相反,要么解析IComponentContext   再次来自'或解析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>();
        }
    }
}

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查看

@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

1 个答案:

答案 0 :(得分:4)

你的问题在这里:


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