我正在使用Luis和Luis Action Binding创建一个Bot应用程序,如下例所示 Luis action binding sample on GitHub
我想使用依赖注入将数据服务实例传递给动作绑定
我在存储库设计模式之后添加了一个数据层:
public class ProviderService : IProviderService
{
private readonly IProviderRepository ProviderRepository;
private readonly IUnitOfWork UnitOfWork;
public ProviderService(IProviderRepository providerService, IUnitOfWork unitOfWork)
{
this.ProviderRepository = providerService;
this.UnitOfWork = unitOfWork;
}
public void CreateProvider(Provider provider)
{
ProviderRepository.Add(provider);
}
并将使用动作绑定类中的fulfill方法,如
public class FindNumberAction : BaseLuisAction
{
IProviderService _provider;
[Required(ErrorMessage = "Please provide a ProviderName")]
public string Provider { get; set; }
public string ProviderType { get; set; }
public FindNumberAction() : this(new ProviderService(new ProviderRepository(new DbFactory()), new UnitOfWork(new DbFactory())))
{
}
public FindNumberAction(IProviderService provider)
{
_provider = provider;
}
public override Task<object> FulfillAsync()
{
var providerData = _provider.GetByProviderName(Provider);
if (!string.IsNullOrEmpty(providerData.ProviderAddress))
{
var result = new ProviderInfo
{
ProviderAddress = providerData.ProviderAddress,
ProviderName = providerData.Provider.ProviderName,
ProviderPhone = providerData.ProviderPhone,
ProviderType = providerData.Provider.ProviderType.ProviderTypeName
};
return Task.FromResult((object)result.GetNumber());
}
return Task.FromResult((object)new ProviderInfo() { ProviderName = Provider, ProviderType = ProviderType }.NoProviderNumberFound());
}
}
答案 0 :(得分:0)
https://github.com/Microsoft/BotBuilder/tree/master/CSharp/Samples/AlarmBot
以下是如何在bot框架中实现AutoFac的一个很好的例子。您将看到您不仅需要实现容器,还需要更改MessagesController处理收到消息的方式。