예제 #1
0
        public IToposProducer Create()
        {
            ToposConfigurerHelpers.RegisterCommonServices(_injectionist);

            _injectionist.PossiblyRegisterDefault <ITopicMapper>(c => new SimpleTopicMapper());

            _injectionist.Register <IToposProducer>(c =>
            {
                var messageSerializer      = c.Get <IMessageSerializer>();
                var topicMapper            = c.Get <ITopicMapper>();
                var producerImplementation = c.Get <IProducerImplementation>(errorMessage: "Røvhul");
                var loggerFactory          = c.Get <ILoggerFactory>();

                var defaultToposProducer = new DefaultToposProducer(
                    messageSerializer,
                    topicMapper,
                    producerImplementation,
                    loggerFactory
                    );

                defaultToposProducer.Disposing += () =>
                {
                    foreach (var instance in c.TrackedInstances.OfType <IDisposable>().Reverse())
                    {
                        instance.Dispose();
                    }
                };

                return(defaultToposProducer);
            });

            var resolutionResult = _injectionist.Get <IToposProducer>();

            return(resolutionResult.Instance);
        }
예제 #2
0
        public IToposConsumer Create()
        {
            ToposConfigurerHelpers.RegisterCommonServices(_injectionist);

            _injectionist.PossiblyRegisterDefault <IConsumerDispatcher>(c =>
            {
                var loggerFactory     = c.Get <ILoggerFactory>();
                var messageSerializer = c.Get <IMessageSerializer>();
                var handlers          = c.Get <Handlers>(errorMessage: @"Failing to get the handlers is a sign that the consumer has not had any handlers configured.

Please remember to configure at least one handler by invoking the .Handle(..) configurer like this:

    Configure.Consumer(...)
        .Handle(async (messages, cancellationToken) =>
        {
            // handle messages
        })
        .Start()
");
                var positionManager   = c.Get <IPositionManager>(errorMessage: @"The consumer dispatcher needs access to a positions manager, so it can store a 'high water mark' position for each topic/partition.

It can be configured by invoking the .Positions(..) configurer like this:

    Configure.Consumer(...)
        .Positions(p => p.StoreIn(...))
        .Start()

");

                return(new DefaultConsumerDispatcher(loggerFactory, messageSerializer, handlers, positionManager));
            });

            _injectionist.Register <IToposConsumer>(c =>
            {
                var toposConsumerImplementation = c.Get <IConsumerImplementation>();

                var defaultToposConsumer = new DefaultToposConsumer(toposConsumerImplementation);

                defaultToposConsumer.Disposing += () =>
                {
                    foreach (var instance in c.TrackedInstances.OfType <IDisposable>().Reverse())
                    {
                        instance.Dispose();
                    }
                };

                return(defaultToposConsumer);
            });

            var resolutionResult = _injectionist.Get <IToposConsumer>();

            foreach (var initializable in resolutionResult.TrackedInstances.OfType <IInitializable>())
            {
                initializable.Initialize();
            }

            return(resolutionResult.Instance);
        }