Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child Containers #490

Open
AlexandrSitdikov opened this issue Jun 26, 2019 · 0 comments
Open

Child Containers #490

AlexandrSitdikov opened this issue Jun 26, 2019 · 0 comments

Comments

@AlexandrSitdikov
Copy link

AlexandrSitdikov commented Jun 26, 2019

I have solutions with many modules/components. Often, modules logically encapsulate internal containers. Services in these containers should not be visible from the outside, or this is not necessary. At the same time, services may need upstream container services through dependency injection.

For example:

  1. Container Web Controllers. Controllers exist at a high UI / API level, but have a dependency on the level of data access. Model levels neither about UI, nor about API do not reflect.
  2. Calculator function container. Calculator functions exist in a low-level sandbox. There is no sense in having access to them from outside (their descriptions are enough), but the functions themselves may require access to the functionality of individual modules.

Thus, registering such services in a common container will, firstly, overload it, and secondly, give access to logically inaccessible components.

To solve this problem, I use child containers. They are not an extension of the outer scope or context. They expand the registration of services. What do you think about it?

public static class ContainerExtension
    {
        private static readonly ConcurrentDictionary<ServiceContainer, Action<object>> proccessors = new ConcurrentDictionary<ServiceContainer, Action<object>>();

        public static TContainer CreateSubContainer<TContainer>(this ServiceContainer parent)
            where TContainer : ServiceContainer, new()
        {
            return parent.CreateChildContainer(() => new TContainer());
        }

        public static TContainer CreateChildContainer<TContainer>(this ServiceContainer parent, Func<TContainer> childFactory)
            where TContainer : ServiceContainer
        {
            proccessors.TryGetValue(parent, out var parentProccessor);
            if (parentProccessor == null)
            {
                parentProccessor = x => parent.InjectProperties(x);
            }

            var child = childFactory();

            child.Initialize(x => true, (factory, instance) => parentProccessor(instance));

            proccessors.TryAdd(child, instance =>
            {
                parentProccessor(instance);
                child.InjectProperties(instance);
            });

            return child;
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant