Exemplo n.º 1
0
        public void RegisterServices(Container container)
        {
            // Required: How to instantiate the DbContext; and share it amongst objects participating in a single request.
            var webApiRequestLifestyle = new WebApiRequestLifestyle(true);
            var hybridLifestyle = Lifestyle.CreateHybrid(() => webApiRequestLifestyle.GetCurrentScope(container) == null, Lifestyle.Transient, webApiRequestLifestyle);
            container.Register(() =>
                               {
                                   var db = new ScrumDb();
                                   db.AttachDbEnums();
                                   return db;
                               },
                               hybridLifestyle);
            container.RegisterLazy<ScrumDb>();

            // Required: Register global datamodel metadata
            var metadataRegistration = hybridLifestyle.CreateRegistration<DbContextMetadata<ScrumDb>>(container);
            container.AddRegistration(typeof(IContainerMetadata<ScrumDb>), metadataRegistration);
            container.AddRegistration(typeof(IContainerMetadata), metadataRegistration);

            // Query validation settings could be specified here
            //container.RegisterInstance(new ODataValidationSettings
            //{
            //	MaxExpansionDepth = 15,
            //	MaxTop = 200
            //}); //.Named<ODataValidationSettings>("Edit");  TODO: Figure out how to separate ODataValidationSettings for Edit controllers vs ReadOnly controllers
        }
        private static void RegisterForAssembly(Container container, WebApiRequestLifestyle webApiLifestyle, Assembly assembly, string nameSpace)
        {
            var registrations =
                from type in assembly.GetExportedTypes()
                where type.Namespace == nameSpace
                where type.GetInterfaces().Any()
                select new { Service = type.GetInterfaces().Single(), Implementation = type };

            foreach (var reg in registrations)
            {
                container.Register(reg.Service, reg.Implementation, webApiLifestyle);
            }
        }
        protected void InitializeContainer(Container container)
        {
            var webApiLifestyle = new WebApiRequestLifestyle();

            container.Register<IDataContext, DataContext>(webApiLifestyle);
            container.Register<ICurrentIdentity, CurrentIdentity>(webApiLifestyle);

            RegisterForAssembly(container, webApiLifestyle, typeof(IAddEntityCommand).Assembly, "FinalYearProjectBlog.CommandQuery.Commands");
            RegisterForAssembly(container, webApiLifestyle, typeof(IGetAllEntityQuery).Assembly, "FinalYearProjectBlog.CommandQuery.Queries");
            RegisterForAssembly(container, webApiLifestyle, typeof (IBlogPostMapper).Assembly, "FinalYearProjectBlog.Model.Mappers");
            RegisterForAssembly(container, webApiLifestyle, typeof(IBlogPostControllerValidator).Assembly, "FinalYearProjectBlog.Web.Controllers.Api.Validators");
            RegisterForAssembly(container, webApiLifestyle, typeof(IEmailSenderService).Assembly, "FinalYearProjectBlog.CommandQuery.Services");
            RegisterForAssembly(container, webApiLifestyle, typeof(IBlogPostViewModelBuilder).Assembly, "FinalYearProjectBlog.Web.View.Builders");

            container.RegisterSingleton<ISimpleInjectorConfig>(() => this);

            container.RegisterWebApiRequest(() => Thread.CurrentPrincipal);
        }
        protected void Application_Start(object sender, EventArgs e)
        {
            var lifestyle = new WebApiRequestLifestyle(true);
            _container = new Container();
            _assemblies = new[]
            {
                typeof(Global).Assembly,
                typeof(ContactDb).Assembly,
                typeof(Commander).Assembly,
                typeof(JsonRpcController).Assembly,
            };

            ConfigureServices(lifestyle);
            ConfigureMediator();
            ConfigureCommander();
            ConfigureWebApi(GlobalConfiguration.Configuration);
            _container.Verify();
        }
Exemplo n.º 5
0
		/// <summary>
		/// Registers application-level IoC settings.
		/// </summary>
		/// <param name="container"></param>
		public void RegisterServices(Container container)
		{
			// Support sharing the DbContext amongst objects participating in a single request;
			// but if there is no request, just make it transient.
			var webApiRequestLifestyle = new WebApiRequestLifestyle(true);
			var hybridLifestyle = Lifestyle.CreateHybrid(() => webApiRequestLifestyle.GetCurrentScope(container) == null, Lifestyle.Transient, webApiRequestLifestyle);
			container.Register<TodoListContext>(hybridLifestyle);
			container.RegisterLazy<TodoListContext>();

			// Required: Register global datamodel metadata (IContainerMetadata and IContainerMetadata<DbContext>)
			var mmRegistration = Lifestyle.Singleton.CreateRegistration<DbContextMetadata<TodoListContext>>(container);
			container.AddRegistration(typeof(IContainerMetadata), mmRegistration);
			container.AddRegistration(typeof(IContainerMetadata<TodoListContext>), mmRegistration);

			// Query validation settings could be specified here
			container.RegisterSingle(new ODataValidationSettings()
			{
				MaxExpansionDepth = 5,
				MaxTop = 200
			});
		}
        public static void Register(HttpConfiguration config)
        {
            var container = new Container();
            var webapiLifestyle = new WebApiRequestLifestyle();
            container.RegisterWebApiRequest<ITransactionEngine, TransactionEngine>();

            BuildManager.GetReferencedAssemblies();
            AppDomain currentDomain = AppDomain.CurrentDomain;

            container.RegisterManyForOpenGeneric(typeof(IQueryHandler<,>), webapiLifestyle, currentDomain.GetAssemblies());
            container.RegisterManyForOpenGeneric(typeof(ICommandHandler<>), webapiLifestyle, currentDomain.GetAssemblies());

            // Veroorzaakt een StackOverFlowException bij opvragen:
            container.RegisterOpenGeneric(typeof(ICommandHandler<>), typeof(UpdateFeaturesCommandHandler<,>), webapiLifestyle,
                c => !c.Handled);

            container.RegisterSingle<ITransactionalCommandHandlerFactory>(new TransactionalCommandHandlerFactory(container));

            container.RegisterWebApiControllers(config);
            container.Verify();

            GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
        }