mardi 21 mai 2019

Writing a wrapper for different dependency injection containers in .net core

I wanted to write a wrapper around dependency injection containers in my .net core project, so that whenever I need to inject something in my application, I can use my own injector which is actually using third-party containers like Autofac and SimpleInjection for injection. This way I can change my injector without ever changing my code.

I've written an interface for this purpose which has some needed methods:

  interface IDependencyBuilder
    {
        void CreateContainer();

        IContainer Build();

        void RegisterModule<T>() where T : Module, new();

    }

And I've implemented it for the Autofac like this:

public class AutofacContainerBuilder : IDependencyBuilder
    {
        private readonly ContainerBuilder _containerBuilder;

        public AutofacContainerBuilder()
        {
            _containerBuilder = new ContainerBuilder();
        }
        public void CreateContainer()
        {
            throw new NotImplementedException();
        }
        public IContainer Build()
        {
            return _containerBuilder.Build();
        }

        public void RegisterModule<T>() where T : Autofac.Module,new()
        {
            _containerBuilder.RegisterModule<T>();
        }
    }

I think something is wrong with this kind of implementation and writing wrapper.

1.Signatures and Input/Output models: I don't exactly know what functions with what signatures should be written in the wrapper.

2. Implementations for Different Third Parties: For creating a container I've to have it in the constructor and the create container method cannot be implemented.

I expect to handle the dependency injection in my modular application with my wrapper.

What is the correct way of doing this for a modular web application?





Aucun commentaire:

Enregistrer un commentaire