tesicg
tesicg

Reputation: 4053

How NopCommerce uses route tables from external plugins?

We try to build modular architecture in our MVC application and use the approach with external DLLs that are in fact placed and hidden under Areas folder of main application, but they are visible as separated projects in the solution. All modules (plugins) have their own area registration files with RegisterArea method that contains MapRoute call. And it works for us. But, we are not fully satisfied with that areas/hidden approach because it looks as some kind of workaround.

In the meantime, we found NopCommerce project and downloaded the source code. We've figured out that code is loading all plugin DLLs from special folder, but we don't understand how the main application uses route tables from plugins. We found where in code program flow goes through plugins' route tables, but when we try to do the similar in our test application it didn't work even program flow went through the route tables of our plugins.

Actually, there is the following code in Global.asax file in NopCommerce application:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("favicon.ico");
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //register custom routes (plugins, etc)
        var routePublisher = EngineContext.Current.Resolve<IRoutePublisher>();
        routePublisher.RegisterRoutes(routes);

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] { "Nop.Web.Controllers" }
        );
    }

In that code there are 2 lines I think are responsible for that what I'm asking for, but I'm not sure. Here are those 2 lines:

        //register custom routes (plugins, etc)
        var routePublisher = EngineContext.Current.Resolve<IRoutePublisher>();
        routePublisher.RegisterRoutes(routes);

Should it be done about plugins' route tables in that way or can be done in another way? We would like to have Modules (Plugins) folder out of the structure of main MVC application with all module projects placed there.

If somebody has more experience with NopCommerce it would be great if he/she could help us.

Upvotes: 1

Views: 207

Answers (1)

Marco Regueira
Marco Regueira

Reputation: 1088

I suppose there is a number of ways you can do the same, so I'll center my answer in what nopcommerce does, that makes a good start.

  1. First it locates an instance of IRoutePublisher from the container and starts the route registration process (that's the code you show in your question).

  2. Crawl all loaded types (here) looking for route providers, they're classes implementing IRouteProvider like this one. At some point, when performing this step, it eagerly loads the assemblies for the enabled plugins from the plugin folder. Note, all required assemblies need to be in memory to be crawled.

  3. Call RegisterRoutes for all the route providers.

That's all. I've used a quite similar procedure for my projects and it works pretty well.

Upvotes: 1

Related Questions