我开始在Dynamics365开发中使用InversifyJS。为了给您提供一些上下文,Dynamic允许您扩展平台,编写自定义业务逻辑(使用JS),并将其附加到定义的表单事件中。在本例中,我希望实例化我的bussiness逻辑类,并在form事件上执行我的自定义代码。代码应该如下所示:
namespace Example {
export function onLoad(context: ExternalContext) {
set bl = container.resolve<BusinessLogic>(BusinessLogic);
bl.doSomething();
}
}正如您可以想象的那样,onLoad函数将在事件发生时由Dynamic365调用。表单上下文(本例中的ExternalContext)将作为参数传递给函数。这个对象非常重要,因为它允许我们的代码与表单中存在的控件进行交互,而正是我想要注入到类的对象。
BusinessLogic类:
@injectable()
export class BusinessLogic {
protected readonly _context: ExternalContext;
protected readonly _otherDependency: OtherDependency;
constructor(
@inject(ExternalContext) formContext: ExternalContext,
@inject(OtherDependency) otherDependency: OtherDependency) {
this._formContext = formContext;
this._otherDependency = otherDependency;
}
doSomething() {
this._otherDependency.foo(this._context.value1);
}
}另一个例子是依赖关系:
@injectable()
export class OtherDependency {
foo(propertyValue: string) {
// do stuff...
}
}如何将平台传递给我的ExternalContext方法的onLoad对象注册/注入到我的业务类中?我考虑将它存储在容器上下文中,但我确信有更好的方法来实现它。
container.bind<ExternalContext>().toDynamicValue((context) => {
//context??
});发布于 2019-06-28 13:16:20
为了防止其他人面临这种情况,我已经将容器包装在一个带有私有变量的类中,在该类中我可以存储我的ExternalContext并使用toDynamicValue()绑定它(我找不到一种使用容器上下文的方法)。
this.container.bind<ExternalContext>(ExternalContext).toDynamicValue(() => {
return this._externalContext;
});对于这种方法,我不喜欢的一点是,在使用容器之前需要手动设置上下文,但考虑到替代方法,我想这并不坏:
namespace Example {
export function onLoad(context: ExternalContext) {
externalContainer.setContext(context); // <----- :(
set bl = externalContainer.container.resolve<BusinessLogic>(BusinessLogic);
bl.doSomething();
}
}也许有更好的方法来做这件事,所以如果你能弄清楚的话,请大声喊。
https://stackoverflow.com/questions/56563254
复制相似问题