예제 #1
0
        public static INForumApplicationBuilder WithNinject(this INForumApplicationBuilder app)
        {
            IKernelConfiguration config = new KernelConfiguration();

            // Register application services
            config.Bind(app.ApplicationBuilder.GetControllerTypes()).ToSelf().InScope(RequestScope);

            config.Bind <IUserService>().To <AspNetUserService>().InScope(RequestScope);
            //config.Bind<CustomMiddleware>().ToSelf();

            // Cross-wire required framework services
            config.BindToMethod(app.GetRequestService <IViewBufferScope>);
            //config.Bind<ILoggerFactory>().ToConstant(loggerFactory);

            config.Bind <ICommandDispatcher>().To <CommandDispatcher>();
            config.Bind <IQueryDispatcher>().To <QueryDispatcher>();

            config.Bind <IHttpContextAccessor>().ToMethod((context) => {
                return(app.ApplicationServices.GetRequiredService <IHttpContextAccessor>());
            }).InScope(RequestScope);

            config.Bind <IPrincipal>().ToMethod((context) => {
                return(context.Kernel.Get <IHttpContextAccessor>().HttpContext.User);
            }).InScope(RequestScope);

            config
            .Bind(typeof(IGenericValidationCommandHandlerDecorator <>))
            .To(typeof(GenericValidationCommandHandlerDecorator <>));
            config
            .Bind(typeof(IGenericPermissionCheckCommandHandlerDecorator <>))
            .To(typeof(GenericPermissionCheckCommandHandlerDecorator <>));

            config
            .Bind(typeof(IGenericValidationQueryHandlerDecorator <,>))
            .To(typeof(GenericValidationQueryHandlerDecorator <,>));
            config
            .Bind(typeof(IGenericPermissionCheckQueryHandlerDecorator <,>))
            .To(typeof(GenericPermissionCheckQueryHandlerDecorator <,>));

            Assembly coreAssembly = typeof(NForum.CQS.CommandWithStatus).GetTypeInfo().Assembly;

            config.BindQueryHandlers(coreAssembly);

            config.BindCommandHandlers(coreAssembly);

            config.BindPermissionChecks(coreAssembly);

            config.BindValidators(coreAssembly);

            kernel = config.BuildReadonlyKernel();

            return(builder);
        }
예제 #2
0
파일: Startup.cs 프로젝트: danieldc/NForum
        private IReadOnlyKernel RegisterApplicationComponents(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            IKernelConfiguration config = new KernelConfiguration();

            // Register application services
            config.Bind(app.GetControllerTypes()).ToSelf().InScope(RequestScope);

            config.Bind <IUserService>().To <AspNetUserService>().InScope(RequestScope);
            //config.Bind<CustomMiddleware>().ToSelf();

            // Cross-wire required framework services
            config.BindToMethod(app.GetRequestService <IViewBufferScope>);
            config.Bind <ILoggerFactory>().ToConstant(loggerFactory);



            // TODO: Handle this in the Ninject NForum builder!!!
            config.Bind <ICommandDispatcher>().To <CommandDispatcher>();
            config.Bind <IQueryDispatcher>().To <QueryDispatcher>();

            config.Bind <IHttpContextAccessor>().ToMethod((context) => {
                return(app.ApplicationServices.GetRequiredService <IHttpContextAccessor>());
            }).InScope(RequestScope);

            config.Bind <IPrincipal>().ToMethod((context) => {
                return(context.Kernel.Get <IHttpContextAccessor>().HttpContext.User);
            }).InScope(RequestScope);

            config
            .Bind(typeof(IGenericValidationCommandHandlerDecorator <>))
            .To(typeof(GenericValidationCommandHandlerDecorator <>));
            config
            .Bind(typeof(IGenericPermissionCheckCommandHandlerDecorator <>))
            .To(typeof(GenericPermissionCheckCommandHandlerDecorator <>));

            config
            .Bind(typeof(IGenericValidationQueryHandlerDecorator <,>))
            .To(typeof(GenericValidationQueryHandlerDecorator <,>));
            config
            .Bind(typeof(IGenericPermissionCheckQueryHandlerDecorator <,>))
            .To(typeof(GenericPermissionCheckQueryHandlerDecorator <,>));

            Assembly coreAssembly = typeof(NForum.CQS.CommandWithStatus).GetTypeInfo().Assembly;

            config.BindQueryHandlers(coreAssembly);

            config.BindCommandHandlers(coreAssembly);

            config.BindPermissionChecks(coreAssembly);

            config.BindValidators(coreAssembly);

            // TODO: Dapper wire up!
            config
            .Bind <Database>()
            .ToSelf()
            .InScope(RequestScope)
            .WithConstructorArgument("connectionString", "Server=.\\SQLEXPRESS2008R2; Database=nforum-dapper;User id=sa;Password=ivypqn63;");
            // Let's replace the command dispatcher, so we can have uow/transactions in the dapper provider!!
            config.Unbind <ICommandDispatcher>();
            config
            .Bind <ICommandDispatcher>()
            .To <UnitOfWorkCommandDispatcher>();
            //config
            //	.Bind(typeof(IRepository<>))
            //	.To(typeof(GenericRepository<>));
            // Let's replace the query dispatcher, so we can have uow/transactions in the dapper provider!!
            config.Unbind <IQueryDispatcher>();
            config
            .Bind <IQueryDispatcher>()
            .To <UnitOfWorkQueryDispatcher>();

            config
            .Bind <ICategoryDatastore>()
            .To <NForum.Datastores.Dapper.CategoryDatastore>();
            // TODO: End Dapper

            // TODO: MongoDB wire up!
            //config
            //	.Bind<ICategoryDatastore>()
            //	.To<NForum.Datastores.MongoDB.CategoryDatastore>();

            //MongoUrl url = new MongoUrl("mongodb://127.0.0.1/nforum");
            //db = new MongoClient(url).GetDatabase(url.DatabaseName);

            //config
            //	.Bind<IMongoCollection<NForum.Datastores.MongoDB.Dbos.Category>>()
            //	.ToMethod((context) => {
            //		return db.GetCollection<NForum.Datastores.MongoDB.Dbos.Category>("categories");
            //	});
            //config
            //	.Bind<IMongoCollection<NForum.Datastores.MongoDB.Dbos.Forum>>()
            //	.ToMethod((context) => {
            //		return db.GetCollection<NForum.Datastores.MongoDB.Dbos.Forum>("forums");
            //	});
            //config
            //	.Bind<IMongoCollection<NForum.Datastores.MongoDB.Dbos.Topic>>()
            //	.ToMethod((context) => {
            //		return db.GetCollection<NForum.Datastores.MongoDB.Dbos.Topic>("topics");
            //	});
            //config
            //	.Bind<IMongoCollection<NForum.Datastores.MongoDB.Dbos.Reply>>()
            //	.ToMethod((context) => {
            //		return db.GetCollection<NForum.Datastores.MongoDB.Dbos.Reply>("replies");
            //	});
            //config
            //	.Bind<NForum.Datastores.MongoDB.CommonDatastore>()
            //	.ToSelf();
            // TODO: END MONGO



            config
            .Bind <IBoardConfiguration>()
            .To <BuilderBoardConfiguration>();

            return(config.BuildReadonlyKernel());
        }