Skip to main content

Inversion of Control, Dependency Injection and Service Location

The ELEFLEX® platform uses the open source component StructureMap for inversion of control and dependency injection. This system component helps to keep modular software designs maintainable by dynamically allowing configuration and resolution of objects and their dependencies. The system also incorporates the open source component CommonServiceLocator for service locator to resolve objects as other integrated open source components of the system rely on this.

The platform provides a static entry-point object, Eleflex.ObjectLocator which is consumed throughout the various layers of the application for dynamic object resolution. This object contains two properties:

Object location registration takes place when the system is startup (discussed in the next sections). Integrators can register configurations for object location by inheriting from the Eleflex.RegistrationTask object and adding the [ObjectLocationRegistrationTask] class attribute. The following examples demonstrates how to create a class used to register any object location configurations you define.


    using Eleflex;
    [ObjectLocationRegistrationTask]
    public partial class ExampleObjectLocationRegistrationTask : RegistrationTask
    {
        public ExampleObjectLocationRegistrationTask()
        {
            Description = "This tasks registers MyApp object location configurations.";
        }
        public override bool Register(ITaskOptions taskOptions)
        {
            StructureMap.IContainer container = ObjectLocator.Container as StructureMap.IContainer;
            container.Configure(x =>
            {
                x.For<MyApp.Interfaces.IExampleService>().Use<MyApp.Model.ExampleService>()
            });
            return base.Register(taskOptions);
            }
        }

The next example shows how to get an instance of the IExampleService configured in the above registration object example. The system uses the Eleflex.ObjectLocator object and Current property:


    //Get Example Service
    var exampleService = ObjectLocator.Current.GetInstance<MyApp.Interfaces.IExampleService>();