DotVvmServiceCollectionExtensions.cs

47 lines | 1.718 kB Blame History Raw Download
namespace Web.Helpers
{
    using Castle.MicroKernel.Registration;
    using Castle.Windsor;
    using DotVVM.Framework.ViewModel.Serialization;
    using Microsoft.Extensions.DependencyInjection;
    using System.IO;
    using System.Web.Hosting;
    using Infrastructure;

    public static class DotVvmServiceCollectionExtensions
    {
        private static WindsorContainer _windsorContainer;

        // https://www.dotvvm.com/docs/2.0/pages/advanced-ioc-di-container-owin
        public static IDotvvmServiceCollection ConfigureCastleWindsor(this IDotvvmServiceCollection svc)
        {
            _windsorContainer = new WindsorContainer();

            // ReSharper disable once PossibleNullReferenceException
            _windsorContainer.Register(Classes.FromThisAssembly()
                .InNamespace(Constants.ViewModelsNamespace)
                .LifestyleTransient()
                .WithServiceDefaultInterfaces());
            _windsorContainer.Kernel.Resolver.AddSubResolver(new ServiceCollectionResolver(svc.Services));

            svc.Services.AddSingleton<IViewModelLoader>(_ => new WindsorViewModelLoader(_windsorContainer));
            svc.RegisterDependencies();
            return svc;
        }

        public static IDotvvmServiceCollection ConfigureDefaultTemporaryStorage(this IDotvvmServiceCollection svc)
        {
            var path = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, Constants.DefaultTemporaryStoragePath);
            svc.AddDefaultTempStorages(path);
            return svc;
        }

        #region Private Methods
        private static IDotvvmServiceCollection RegisterDependencies(this IDotvvmServiceCollection svc)
        {
            return svc;
        }
        #endregion
    }
}