0

I'm developing an application by modules. Each module correspond to a legacy system we have here in the company. Using a code like this, I can inject the Controllers from a Class Library into an MVC Application using Unity Container:

using System.Linq;
using System.Web.Mvc;
using Microsoft.Practices.Unity.Mvc;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MyProject.Common.UnityMvcActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(MyProject.Common.UnityMvcActivator), "Shutdown")]

namespace MyProject.Common
{
    public static class UnityMvcActivator
    {
        /// <summary>Integrates Unity when the application starts.</summary>
        public static void Start()
        {
            var container = UnityConfig.GetConfiguredContainer();

            FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
            FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            // TODO: Uncomment if you want to use PerRequestLifetimeManager
            // Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
        }

        /// <summary>Disposes the Unity container when the application is shut down.</summary>
        public static void Shutdown()
        {
            var container = UnityConfig.GetConfiguredContainer();
            container.Dispose();
        }
    }
}

This code is automatically added in the Class Library when I install the Unity.Mvc bootstrapper.

All the Class Libraries implemented as modules will be web-oriented.

Works like a charm, but I still have to define all the routes in the RouteConfig from the MVC Application. My challenge is to define these routes in the class library. I haven't found any material explaning how to do.

Is this possible? If yes, how?

Leonel Sanches da Silva
  • 6,972
  • 9
  • 46
  • 66

1 Answers1

0

I would rework the design if possible. I'd recommend injecting shared components in the class library into application-specific controllers, but the controllers and application configuration should stay out of the modules. UI considerations like that should be application-specific.

As for the container, I'd recommend having a unity configuration for each assembly, but don't reference the container in the modules (to do so is to make the container a service locator). Instead, make a module-specific configuration class in each assembly, and at application start pass the container in from the UI application to each assembly's configuration (you'll rely on reflection or similar to access the configurations).

Example (the answer that uses UnityContainerExtension): IoC in class library. Where to bootstrap

Community
  • 1
  • 1
moarboilerplate
  • 1,633
  • 9
  • 23
  • Well, the objective is exactly inject new Controllers from Class Library to Web Application. The Controllers are prefixed by the Module identification. I would like to have some code examples in the answer. Thank you. – Leonel Sanches da Silva Nov 03 '15 at 22:02
  • @CiganoMorrisonMendez Your difficulty configuring routes from a dependent assembly is a sign that you should be taking a different approach. A class library should not be web-dependent. – moarboilerplate Nov 03 '15 at 22:08
  • @CiganoMorrisonMendez a code example for the injection approach I recommend is the answer in the link that uses `UnityContainerExtension`. – moarboilerplate Nov 03 '15 at 22:12
  • But the Class Libraries will be all web-dependent. I installed Unity and Unity.Mvc in each of them (sorry for the omission). Unity.Mvc installs System.Web.Mvc and System.Web.Razor. I can use the container provided in the link's answer, but I don't see any problem to allow the Class Library register the specific route by itself. – Leonel Sanches da Silva Nov 03 '15 at 22:26
  • @CiganoMorrisonMendez It becomes a maintenance issue when you need to make changes. If all apps share the configuration and controllers, then you need to make sure that every single one of your apps is compatible with the change. Then if you do make changes, you have to either redeploy all of your apps, or you end up with different versions of the shared code in your applications. You could even end up with scenarios where you make certain apps use an old specific "version" of the configuration code and controllers. – moarboilerplate Nov 03 '15 at 22:37
  • Well, none of the Class Libraries will make use of configuration files. Redeploying is not a problem in my scenario. As I explained, the Class Libraries' Controllers will have a naming convention that prevent naming collisions. The DbContexts will be isolated as well. These concerns are not important by now. I just want how to solve the routes injection. – Leonel Sanches da Silva Nov 03 '15 at 22:45