private ContainerRegistry()
        {
            Scan(scan =>
            {
                scan.TheCallingAssembly();
                scan.WithDefaultConventions();

                scan.AssemblyContainingType<Entity>();
                scan.AssemblyContainingType<User>();

                scan.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<,>));
                scan.ConnectImplementationsToTypesClosing(typeof(IQueryHandler<,>));
                scan.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>));
                scan.ConnectImplementationsToTypesClosing(typeof(IValidator<>));
                scan.ConnectImplementationsToTypesClosing(typeof(EntityDeleter<>));
            });

            For<IResponseMapper>().Use(new InvariantResponseMapper());

            var structureMapInteractionObjectResolver = new StructureMapInteractionObjectResolver();
            For<IInteractionHandlerResolver>().Use(structureMapInteractionObjectResolver);
            For<IEntityDeleterResolver>().Use(structureMapInteractionObjectResolver);

            // Register query handlers for standard queries. WARNING: Intentionally Bad Code!
            StandardInteractionHandlerGenericTypeDefinitions standardInteractionHandlerGenericTypeDefinitions;
            if (UnitOfWorkFactory.DefaultUnitOfWorkType == typeof(Raven.UnitOfWork))
            {
                standardInteractionHandlerGenericTypeDefinitions = new StandardInteractionHandlerGenericTypeDefinitions
                    (
                    typeof(GetByIdQueryHandler<>),
                    typeof(GetOneQueryHandler<>),
                    typeof(GetAllQueryHandler<>),
                    typeof(GetTotalCountQueryHandler<>)
                    );
            }
            else
            {
                standardInteractionHandlerGenericTypeDefinitions = new StandardInteractionHandlerGenericTypeDefinitions
                (
                    typeof(NHibernate.Interaction.StandardQueries.GetByIdQueryHandler<>),
                    typeof(NHibernate.Interaction.StandardQueries.GetOneQueryHandler<>),
                    typeof(NHibernate.Interaction.StandardQueries.GetAllQueryHandler<>),
                    typeof(NHibernate.Interaction.StandardQueries.GetTotalCountQueryHandler<>)
                );
            }

            StandardInteractionHandlerRegistration.RegisterStandardInteractionHandlersForEntities(this, standardInteractionHandlerGenericTypeDefinitions, typeof(User).Assembly);

            For<UnitOfWork>()
                .LifecycleIs(new InteractionScopeLifecycle())
                .Use(() => UnitOfWorkFactory.CreateUnitOfWork());

            IncludeRegistry<RavenContainerRegistry>();
            IncludeRegistry<NHibernateContainerRegistry>();
        }
Example #2
0
        private ContainerRegistry()
        {
            Scan(scan =>
            {
                scan.TheCallingAssembly();
                scan.WithDefaultConventions();

                scan.AssemblyContainingType<Entity>();
                scan.AssemblyContainingType<User>();

                scan.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<,>));
                scan.ConnectImplementationsToTypesClosing(typeof(IQueryHandler<,>));
                scan.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>));
                scan.ConnectImplementationsToTypesClosing(typeof(IValidator<>));
                scan.ConnectImplementationsToTypesClosing(typeof(EntityDeleter<>));
            });

            For<IResponseMapper>().Use(new InvariantResponseMapper());

            var structureMapInteractionObjectResolver = new StructureMapInteractionObjectResolver();
            For<IInteractionHandlerResolver>().Use(structureMapInteractionObjectResolver);
            For<IEntityDeleterResolver>().Use(structureMapInteractionObjectResolver);
            For<IQueryResultCacheResolver>().Use(structureMapInteractionObjectResolver);

            // Register query handlers for standard queries. WARNING: Intentionally Bad Code!
            StandardInteractionHandlerGenericTypeDefinitions standardInteractionHandlerGenericTypeDefinitions;
            if (UnitOfWorkFactory.DefaultUnitOfWorkType == typeof(Raven.UnitOfWork))
            {
                For<IQueryHandler<GetByIdQuery, Option<object>>>().Singleton().Use<RavenStandardQueries.GetByIdQueryHandler>();
                standardInteractionHandlerGenericTypeDefinitions = new StandardInteractionHandlerGenericTypeDefinitions
                    (
                    typeof(RavenStandardQueries.GetByIdQueryHandler<>),
                    typeof(RavenStandardQueries.GetOneQueryHandler<>),
                    typeof(RavenStandardQueries.GetAllQueryHandler<>),
                    typeof(RavenStandardQueries.GetTotalCountQueryHandler<>)
                    );

                IncludeRegistry<RavenContainerRegistry>();

                StandardInteractionHandlerRegistration.RegisterStandardInteractionHandlersForEntities(this, standardInteractionHandlerGenericTypeDefinitions);
                StandardInteractionHandlerRegistration.RegisterStandardNonGenericGetByIdQueryHandler(this, typeof(RavenStandardQueries.GetByIdQueryHandler));

                For<UnitOfWork>()
                    .LifecycleIs(new InteractionScopeLifecycle())
                    .Use(() => UnitOfWorkFactory.CreateUnitOfWork());
            }
            else if (UnitOfWorkFactory.DefaultUnitOfWorkType == typeof(NHibernate.UnitOfWork))
            {
                For<IQueryHandler<GetByIdQuery, Option<object>>>().Singleton().Use<NHibernateStandardQueries.GetByIdQueryHandler>();
                standardInteractionHandlerGenericTypeDefinitions = new StandardInteractionHandlerGenericTypeDefinitions
                (
                    typeof(NHibernateStandardQueries.GetByIdQueryHandler<>),
                    typeof(NHibernateStandardQueries.GetOneQueryHandler<>),
                    typeof(NHibernateStandardQueries.GetAllQueryHandler<>),
                    typeof(NHibernateStandardQueries.GetTotalCountQueryHandler<>)
                );

                IncludeRegistry<NHibernateContainerRegistry>();

                StandardInteractionHandlerRegistration.RegisterStandardInteractionHandlersForEntities(this, standardInteractionHandlerGenericTypeDefinitions);
                StandardInteractionHandlerRegistration.RegisterStandardNonGenericGetByIdQueryHandler(this, typeof(NHibernateStandardQueries.GetByIdQueryHandler));

                For<UnitOfWork>()
                    .LifecycleIs(new InteractionScopeLifecycle())
                    .Use(() => UnitOfWorkFactory.CreateUnitOfWork());
            }
            else if (UnitOfWorkFactory.DefaultUnitOfWorkType == typeof (Pragmatic.EntityFramework.UnitOfWork))
            {
                IncludeRegistry<EntityFramework.ContainerRegistry>();
            }
            else throw new InvalidOperationException(string.Format("The type '{0}' is not supported as the default unit of work.", UnitOfWorkFactory.DefaultUnitOfWorkType));

            For<IQueryResultCache<GetUsersQuery, User[]>>()
                .Use(new InMemoryEquatableQueryResultCache<GetUsersQuery, User[]>());
        }