Ejemplo n.º 1
0
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            builder.RegisterModule(new FiberModule <DialogTask>());

            // singleton components

            builder
            .Register(c => new ResourceManager("Microsoft.Bot.Builder.Resource.Resources", typeof(Resource.Resources).Assembly))
            .As <ResourceManager>()
            .SingleInstance();

            // every lifetime scope is driven by a message

            builder
            .Register((c, p) => p.TypedAs <IMessageActivity>())
            .AsSelf()
            .AsImplementedInterfaces()
            .InstancePerMatchingLifetimeScope(LifetimeScopeTag);

            // make the address and cookie available for the lifetime scope

            builder
            .Register(c => Address.FromActivity(c.Resolve <IActivity>()))
            .AsImplementedInterfaces()
            .InstancePerMatchingLifetimeScope(LifetimeScopeTag);

            builder
            .RegisterType <ResumptionCookie>()
            .AsSelf()
            .InstancePerMatchingLifetimeScope(LifetimeScopeTag);

            // components not marked as [Serializable]
            builder
            .RegisterType <MicrosoftAppCredentials>()
            .AsSelf()
            .SingleInstance();

            builder
            // not resolving IEqualityComparer<IAddress> from container because it's a very local policy
            // and yet too broad of an interface.  could explore using tags for registration overrides.
            .Register(c => new LocalMutualExclusion <IAddress>(new ConversationAddressComparer()))
            .As <IScope <IAddress> >()
            .SingleInstance();

            builder
            .Register(c => new ConnectorClientFactory(c.Resolve <IAddress>(), c.Resolve <MicrosoftAppCredentials>()))
            .As <IConnectorClientFactory>()
            .InstancePerLifetimeScope();

            builder
            .Register(c => c.Resolve <IConnectorClientFactory>().MakeConnectorClient())
            .As <IConnectorClient>()
            .InstancePerLifetimeScope();

            builder
            .Register(c => c.Resolve <IConnectorClientFactory>().MakeStateClient())
            .As <IStateClient>()
            .InstancePerLifetimeScope();

            builder
            .Register(c => new DetectChannelCapability(c.Resolve <IAddress>()))
            .As <IDetectChannelCapability>()
            .InstancePerLifetimeScope();

            builder
            .Register(c => c.Resolve <IDetectChannelCapability>().Detect())
            .As <IChannelCapability>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ConnectorStore>()
            .AsSelf()
            .InstancePerLifetimeScope();

            // If bot wants to use InMemoryDataStore instead of
            // ConnectorStore, the below registration should be used
            // as the inner IBotDataStore for CachingBotDataStore

            /*builder.RegisterType<InMemoryDataStore>()
             *  .AsSelf()
             *  .SingleInstance(); */

            builder.Register(c => new CachingBotDataStore(c.Resolve <ConnectorStore>(),
                                                          CachingBotDataStoreConsistencyPolicy.ETagBasedConsistency))
            .As <IBotDataStore <BotData> >()
            .AsSelf()
            .InstancePerLifetimeScope();

            builder
            .RegisterType <JObjectBotData>()
            .AsSelf()
            .InstancePerLifetimeScope();

            builder
            .Register(c => new DialogTaskManagerBotDataLoader(c.Resolve <JObjectBotData>(),
                                                              c.Resolve <IDialogTaskManager>()))
            .As <IBotData>()
            .InstancePerLifetimeScope();

            builder
            .Register((c, p) => new BotDataBagStream(p.TypedAs <IBotDataBag>(), p.TypedAs <string>()))
            .As <Stream>()
            .InstancePerDependency();

            builder
            .Register(c => new DialogTaskManager(DialogModule.BlobKey,
                                                 c.Resolve <JObjectBotData>(),
                                                 c.Resolve <IStackStoreFactory <DialogTask> >(),
                                                 c.Resolve <Func <IDialogStack, CancellationToken, IDialogContext> >()))
            .AsSelf()
            .As <IDialogTaskManager>()
            .InstancePerLifetimeScope();

            builder
            .RegisterType <DialogContext>()
            .As <IDialogContext>()
            .InstancePerDependency();


            builder
            .Register(c =>
            {
                var cc = c.Resolve <IComponentContext>();

                Func <string, IBotDataBag, IStore <IFiberLoop <DialogTask> > > make = (taskId, botDataBag) =>
                {
                    var stream = cc.Resolve <Stream>(TypedParameter.From(botDataBag), TypedParameter.From(taskId));
                    return(cc.Resolve <IStore <IFiberLoop <DialogTask> > >(TypedParameter.From(stream)));
                };

                return(make);
            })
            .As <Func <string, IBotDataBag, IStore <IFiberLoop <DialogTask> > > >()
            .InstancePerDependency();


            builder.Register(c => c.Resolve <IDialogTaskManager>().DialogTasks[0])
            .As <IDialogStack>()
            .As <IDialogTask>()
            .InstancePerLifetimeScope();


            // Scorable implementing "/deleteprofile"
            builder
            .Register(c => new Regex("^(\\s)*/deleteprofile", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace))
            .Keyed <Regex>(Key_DeleteProfile_Regex)
            .SingleInstance();

            builder
            .Register(c => new DeleteProfileScorable(c.Resolve <IDialogStack>(), c.Resolve <IBotData>(), c.Resolve <IBotToUser>(), c.ResolveKeyed <Regex>(Key_DeleteProfile_Regex)))
            .As <IScorable <IActivity, double> >()
            .InstancePerLifetimeScope();

            builder
            .Register(c =>
            {
                var cc = c.Resolve <IComponentContext>();
                Func <IActivity, IResolver> make = activity =>
                {
                    var resolver = NoneResolver.Instance;
                    resolver     = new EnumResolver(resolver);
                    resolver     = new AutofacResolver(cc, resolver);
                    resolver     = new ArrayResolver(resolver,
                                                     activity,
                                                     cc.Resolve <IDialogStack>(),
                                                     cc.Resolve <IBotToUser>(),
                                                     cc.Resolve <IBotData>(),
                                                     cc.Resolve <IDialogTaskManager>());
                    resolver = new ActivityResolver(resolver);
                    return(resolver);
                };
                return(make);
            })
            .AsSelf()
            .InstancePerLifetimeScope();

            builder
            .RegisterType <DialogRouter>()
            .Keyed <IScorable <IActivity, double> >(Key_Dialog_Router)
            .InstancePerLifetimeScope();

            builder
            .Register(c =>
            {
                var cc = c.Resolve <IComponentContext>();
                Func <IPostToBot> makeInner = () =>
                {
                    IPostToBot post = new ReactiveDialogTask(cc.Resolve <IDialogTask>(), cc.Resolve <Func <IDialog <object> > >());
                    post            = new ScoringDialogTask <double>(post, cc.ResolveKeyed <IScorable <IActivity, double> >(Key_Dialog_Router));
                    return(post);
                };

                IPostToBot outer = new PersistentDialogTask(makeInner, cc.Resolve <IBotData>());
                outer            = new SerializingDialogTask(outer, cc.Resolve <IAddress>(), c.Resolve <IScope <IAddress> >());
                outer            = new ExceptionTranslationDialogTask(outer);
                outer            = new LocalizedDialogTask(outer);
                outer            = new PostUnhandledExceptionToUserTask(outer, cc.Resolve <IBotToUser>(), cc.Resolve <ResourceManager>(), cc.Resolve <TraceListener>());
                outer            = new LogPostToBot(outer, cc.Resolve <IActivityLogger>());
                return(outer);
            })
            .As <IPostToBot>()
            .InstancePerLifetimeScope();

            builder
            .RegisterType <NullActivityLogger>()
            .AsImplementedInterfaces()
            .SingleInstance();

            builder
            .RegisterType <AlwaysSendDirect_BotToUser>()
            .AsSelf()
            .InstancePerLifetimeScope();

            builder
            .Register(c => new LogBotToUser(
                          new MapToChannelData_BotToUser(c.Resolve <AlwaysSendDirect_BotToUser>(), new [] { new KeyboardCardMapper() }),
                          c.Resolve <IActivityLogger>()))
            .As <IBotToUser>()
            .InstancePerLifetimeScope();
        }
Ejemplo n.º 2
0
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            builder.RegisterModule(new FiberModule <DialogTask>());

            // singleton components

            builder
            .Register(c => new ResourceManager("Microsoft.Bot.Builder.Resource.Resources", typeof(Resource.Resources).Assembly))
            .As <ResourceManager>()
            .SingleInstance();

            // every lifetime scope is driven by a message

            builder
            .Register((c, p) => p.TypedAs <IMessageActivity>())
            .AsSelf()
            .AsImplementedInterfaces()
            .InstancePerMatchingLifetimeScope(LifetimeScopeTag);

            // make the address and cookie available for the lifetime scope

            builder
            .Register(c => Address.FromActivity(c.Resolve <IActivity>()))
            .AsImplementedInterfaces()
            .InstancePerMatchingLifetimeScope(LifetimeScopeTag);

            builder
            .RegisterType <ResumptionCookie>()
            .AsSelf()
            .InstancePerMatchingLifetimeScope(LifetimeScopeTag);

            // components not marked as [Serializable]
            builder
            .RegisterType <MicrosoftAppCredentials>()
            .AsSelf()
            .SingleInstance();

            builder
            // not resolving IEqualityComparer<IAddress> from container because it's a very local policy
            // and yet too broad of an interface.  could explore using tags for registration overrides.
            .Register(c => new LocalMutualExclusion <IAddress>(new ConversationAddressComparer()))
            .As <IScope <IAddress> >()
            .SingleInstance();

            builder
            .Register(c => new ConnectorClientFactory(c.Resolve <IAddress>(), c.Resolve <MicrosoftAppCredentials>()))
            .As <IConnectorClientFactory>()
            .InstancePerLifetimeScope();

            builder
            .Register(c => c.Resolve <IConnectorClientFactory>().MakeConnectorClient())
            .As <IConnectorClient>()
            .InstancePerLifetimeScope();

            builder
            .Register(c => c.Resolve <IConnectorClientFactory>().MakeStateClient())
            .As <IStateClient>()
            .InstancePerLifetimeScope();

            builder
            .Register(c => new DetectChannelCapability(c.Resolve <IAddress>()))
            .As <IDetectChannelCapability>()
            .InstancePerLifetimeScope();

            builder
            .Register(c => c.Resolve <IDetectChannelCapability>().Detect())
            .As <IChannelCapability>()
            .InstancePerLifetimeScope();

            builder.RegisterType <ConnectorStore>()
            .AsSelf()
            .InstancePerLifetimeScope();

            // If bot wants to use InMemoryDataStore instead of
            // ConnectorStore, the below registration should be used
            // as the inner IBotDataStore for CachingBotDataStore

            /*builder.RegisterType<InMemoryDataStore>()
             *  .AsSelf()
             *  .SingleInstance(); */

            builder.Register(c => new CachingBotDataStore(c.Resolve <ConnectorStore>(),
                                                          CachingBotDataStoreConsistencyPolicy.ETagBasedConsistency))
            .As <IBotDataStore <BotData> >()
            .AsSelf()
            .InstancePerLifetimeScope();

            builder
            .RegisterType <JObjectBotData>()
            .As <IBotData>()
            .InstancePerLifetimeScope();

            builder
            .Register(c => new BotDataBagStream(c.Resolve <IBotData>().PrivateConversationData, BlobKey))
            .As <Stream>()
            .InstancePerLifetimeScope();

            builder
            .RegisterType <DialogTask>()
            .AsSelf()
            .As <IDialogStack>()
            .InstancePerLifetimeScope();

            // Scorable implementing "/deleteprofile"
            builder
            .Register(c => new Regex("^(\\s)*/deleteprofile", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace))
            .Keyed <Regex>(Key_DeleteProfile_Regex)
            .SingleInstance();

            builder
            .Register(c => new DeleteProfileScorable(c.Resolve <IDialogStack>(), c.Resolve <IBotData>(), c.Resolve <IBotToUser>(), c.ResolveKeyed <Regex>(Key_DeleteProfile_Regex)))
            .As <IScorable <IActivity, double> >()
            .InstancePerLifetimeScope();

            builder
            .Register(c =>
            {
                var stack      = c.Resolve <IDialogStack>();
                var fromStack  = stack.Frames.Select(f => f.Target).OfType <IScorable <IActivity, double> >();
                var fromGlobal = c.Resolve <IScorable <IActivity, double>[]>();
                // since the stack of scorables changes over time, this should be lazy
                var lazyScorables = fromStack.Concat(fromGlobal);
                var scorable      = new TraitsScorable <IActivity, double>(c.Resolve <ITraits <double> >(), c.Resolve <IComparer <double> >(), lazyScorables);
                return(scorable);
            })
            .InstancePerLifetimeScope()
            .AsSelf();

            builder
            .RegisterType <NullActivityLogger>()
            .AsImplementedInterfaces()
            .SingleInstance();

            builder
            .Register(c =>
            {
                var cc = c.Resolve <IComponentContext>();

                Func <IPostToBot> makeInner = () =>
                {
                    var task           = cc.Resolve <DialogTask>();
                    IDialogStack stack = task;
                    IPostToBot post    = task;
                    post = new ReactiveDialogTask(post, stack, cc.Resolve <IStore <IFiberLoop <DialogTask> > >(), cc.Resolve <Func <IDialog <object> > >());
                    post = new ExceptionTranslationDialogTask(post);
                    post = new LocalizedDialogTask(post);
                    post = new ScoringDialogTask <double>(post, stack, cc.Resolve <TraitsScorable <IActivity, double> >());
                    return(post);
                };

                IPostToBot outer = new PersistentDialogTask(makeInner, cc.Resolve <IBotData>());
                outer            = new SerializingDialogTask(outer, cc.Resolve <IAddress>(), c.Resolve <IScope <IAddress> >());
                outer            = new PostUnhandledExceptionToUserTask(outer, cc.Resolve <IBotToUser>(), cc.Resolve <ResourceManager>(), cc.Resolve <TraceListener>());
                outer            = new LogPostToBot(outer, cc.Resolve <IActivityLogger>());
                return(outer);
            })
            .As <IPostToBot>()
            .InstancePerLifetimeScope();

            builder
            .RegisterType <AlwaysSendDirect_BotToUser>()
            .AsSelf()
            .InstancePerLifetimeScope();

            builder
            .Register(c => new LogBotToUser(new MapToChannelData_BotToUser(
                                                c.Resolve <AlwaysSendDirect_BotToUser>(),
                                                new List <IMessageActivityMapper> {
                new KeyboardCardMapper()
            }), c.Resolve <IActivityLogger>()))
            .As <IBotToUser>()
            .InstancePerLifetimeScope();

            builder
            .RegisterType <DialogContext>()
            .As <IDialogContext>()
            .InstancePerLifetimeScope();
        }