3
votes

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
1

1 Answers

3
votes

Your problem is here:


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

In order to inject a handle to c (IComponentContext) you must resolve it first. Change your code like so:


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