public static AdaptedContainerBuilderOptions <TTenant> WithStructureMap <TTenant>(
            this ContainerBuilderOptions <TTenant> options,
            Action <TTenant, IServiceCollection> configureTenant)
            where TTenant : class
        {
            var adaptorFactory = new Func <ITenantContainerAdaptor>(() =>
            {
                // host level container.
                var container = new StructureMap.Container();
                container.Populate(options.Builder.Services);
                var adaptedContainer = container.GetInstance <ITenantContainerAdaptor>();

                var containerEventsPublisher = container.TryGetInstance <ITenantContainerEventsPublisher <TTenant> >();
                // add ITenantContainerBuilder<TTenant> service to the host container
                // This service can be used to build a child container (adaptor) for a particular tenant, when required.
                var defaultServices = options.DefaultServices;
                container.Configure(_ =>
                                    _.For <ITenantContainerBuilder <TTenant> >()
                                    .Use(new TenantContainerBuilder <TTenant>(defaultServices, adaptedContainer, configureTenant, containerEventsPublisher))
                                    );

                // NOTE: Im not sure why I was resolving ITenantContainerAdaptor twice, changed to just return previous instance.
                //var adaptor = container.GetInstance<ITenantContainerAdaptor>();
                //return adaptor;
                return(adaptedContainer);
            });

            var adapted = new AdaptedContainerBuilderOptions <TTenant>(options, adaptorFactory);

            return(adapted);
        }
 public static MultitenancyOptionsBuilder<TTenant> ConfigureTenantContainers<TTenant>(this MultitenancyOptionsBuilder<TTenant> optionsBuilder, Action<ContainerBuilderOptions<TTenant>> configure)
     where TTenant : class
 {
     var containerOptionsBuilder = new ContainerBuilderOptions<TTenant>(optionsBuilder);
     configure?.Invoke(containerOptionsBuilder);
     return optionsBuilder;
 }
        public static MultitenancyOptionsBuilder <TTenant> ConfigureTenantContainers <TTenant>(this MultitenancyOptionsBuilder <TTenant> optionsBuilder, Action <ContainerBuilderOptions <TTenant> > configure)
            where TTenant : class
        {
            var containerOptionsBuilder = new ContainerBuilderOptions <TTenant>(optionsBuilder);

            if (configure != null)
            {
                configure(containerOptionsBuilder);
            }
            return(optionsBuilder);
        }
Пример #4
0
        public static ContainerBuilderOptions <TTenant> WithStructureMap <TTenant>(this ContainerBuilderOptions <TTenant> options,
                                                                                   Action <TTenant, ConfigurationExpression> configureTenant)
            where TTenant : class
        {
            // Set a func, that is only invoked when the ServiceProvider is required. This ensures it runs after all other configuration methods
            // which is important as other methods can still add new services to the servicecollection after this one is invoked and we
            // dont want them to be missed when populating the container.
            options.Builder.BuildServiceProvider = new Func <IServiceProvider>(() =>
            {
                var container = new StructureMap.Container();
                container.Populate(options.Builder.Services);
                container.Configure(_ =>
                                    _.For <ITenantContainerBuilder <TTenant> >()
                                    .Use(new StructureMapTenantContainerBuilder <TTenant>(container, configureTenant))
                                    );
                return(container.GetInstance <IServiceProvider>());
            });

            return(options);
        }
Пример #5
0
        public static AdaptedContainerBuilderOptions <TTenant> Autofac <TTenant>(
            this ContainerBuilderOptions <TTenant> options,
            Action <TTenant, IServiceCollection> configureTenant)
            where TTenant : class
        {
            Func <ITenantContainerAdaptor> adaptorFactory = new Func <ITenantContainerAdaptor>(() =>
            {
                // host level container.
                ContainerBuilder builder = new ContainerBuilder();
                builder.Populate(options.Builder.Services);
                builder.AddDotnettencyContainerServices();


                // Build the root container.
                IContainer container = builder.Build();
                ITenantContainerAdaptor adaptedContainer = container.Resolve <ITenantContainerAdaptor>();

                // Get the service that allows us to publish events relating to tenant container events.
                container.TryResolve <ITenantContainerEventsPublisher <TTenant> >(out ITenantContainerEventsPublisher <TTenant> containerEventsPublisher);

                // Update the root container with a service that can be used to build per tenant container!
                ContainerBuilder updateBuilder = new ContainerBuilder();
                var defaultServices            = options.DefaultServices;
                updateBuilder.RegisterInstance(new TenantContainerBuilder <TTenant>(defaultServices, adaptedContainer, configureTenant, containerEventsPublisher)).As <ITenantContainerBuilder <TTenant> >();
                updateBuilder.Update(container);

                return(adaptedContainer);
                //ITenantContainerAdaptor adaptor = container.Resolve<ITenantContainerAdaptor>();
                //return adaptor;

                // return adaptedContainer;
            });

            AdaptedContainerBuilderOptions <TTenant> adapted = new AdaptedContainerBuilderOptions <TTenant>(options, adaptorFactory);

            return(adapted);
        }
        public static AdaptedContainerBuilderOptions <TTenant> WithStructureMap <TTenant>(this ContainerBuilderOptions <TTenant> options,
                                                                                          Action <TTenant, ConfigurationExpression> configureTenant)
            where TTenant : class
        {
            var adaptorFactory = new Func <ITenantContainerAdaptor>(() =>
            {
                // host level container.
                var container = new StructureMap.Container();
                container.Populate(options.Builder.Services);

                var hostContainerConfigurators = container.GetAllInstances <IStructureMapHostContainerConfigurator>();
                if (hostContainerConfigurators != null)
                {
                    foreach (var configurator in hostContainerConfigurators)
                    {
                        container.Configure(configurator.ConfigureHostContainer);
                    }
                }

                var adaptedContainer = container.GetInstance <ITenantContainerAdaptor>();
                // add ITenantContainerBuilder<TTenant> service to the host container
                // This service can be used to build a child container (adaptor) for a particular tenant, when required.
                container.Configure(_ =>
                                    _.For <ITenantContainerBuilder <TTenant> >()
                                    .Use(new StructureMapTenantContainerBuilder <TTenant>(adaptedContainer, configureTenant))
                                    );

                var adaptor = container.GetInstance <ITenantContainerAdaptor>();
                return(adaptor);
            });

            var adapted = new AdaptedContainerBuilderOptions <TTenant>(options, adaptorFactory);

            options.Builder.Services.TryAddScoped((_) => adapted);

            return(adapted);
        }