NService Bus' Publish()'基本界面不发送混凝土子类属性
我使用界面使用nservicebus将消息发送给处理程序。以下是接口:
interface IDeliveryTarget
{
public DeliveryType DeliveryType { get; }
public string DocumentId { get; set; }
}
子类电子邮件和传真:
class EmailTarget : IDeliveryTarget
{
public DeliveryType DeliveryType => DeliveryType.Email;
public int DocumentId {get;set;}
public string MessageBody { get; set; }
public string[] Recepients { get; set; }
public string Subject { get; set; }
}
class FaxTarget : IDeliveryTarget
{
public DeliveryType DeliveryType => DeliveryType.Email;
public int DocumentId {get;set;}
public string FaxNumber { get; set; }
public string FaxUserName { get; set; }
public string Organization { get; set; }
public string Subject { get; set; }
}
我发送列表< ideliveryTarget>
从处理程序如下:
context.Publish(new UploadSuccess
{
DeliveryTargets = new List<IDeliveryTarget>()
}
此列表有许多电子邮件和传真输入。
如下所示,在另一个传奇中处理:
public async Task Handle(UploadSuccess message, IMessageHandlerContext context)
但是,在此处,上载消息仅具有在ideliverytarget接口中定义的属性,但是电子邮件和传真特定的属性却缺少。
除了电子邮件和传真属性以外,我要出席进一步处理。 请指导。
I am using an interface to send message to a handler using NServicebus. Below is the interface:
interface IDeliveryTarget
{
public DeliveryType DeliveryType { get; }
public string DocumentId { get; set; }
}
Child Classes Email and Fax:
class EmailTarget : IDeliveryTarget
{
public DeliveryType DeliveryType => DeliveryType.Email;
public int DocumentId {get;set;}
public string MessageBody { get; set; }
public string[] Recepients { get; set; }
public string Subject { get; set; }
}
class FaxTarget : IDeliveryTarget
{
public DeliveryType DeliveryType => DeliveryType.Email;
public int DocumentId {get;set;}
public string FaxNumber { get; set; }
public string FaxUserName { get; set; }
public string Organization { get; set; }
public string Subject { get; set; }
}
I send List<IDeliveryTarget>
from handler as below:
context.Publish(new UploadSuccess
{
DeliveryTargets = new List<IDeliveryTarget>()
}
This list has many Email and Fax inputs.
Handled in another Saga as below:
public async Task Handle(UploadSuccess message, IMessageHandlerContext context)
But here the UploadSuccess message has only properties defined in IDeliveryTarget interface but Email and Fax specific properties are missing.
I except Email and Fax properties to be present for further processing.
Please guide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@MindSwipe 提到的另一种解决方案是为每个具体类型的事件提供专用属性,即:
这将允许您在处理程序中访问具体类型的属性,而无需进行强制转换。
An alternative solution to what @MindSwipe mentioned is to have dedicated properties on the event for each of the concrete types ie.:
This will allow you to access concrete types' properties in a handler without the need for casting.
您需要将
DeliveryTarget
中的项目转换为接口的具体实现,这些属性仍然存在,它们只是对智能感知隐藏,因为您有一个IDeliveryTarget
类型的变量> 它没有声明您想要的那些属性。做这样的事情:
这对于很多实现来说变得很麻烦,我建议您向
IDeliveryTarget
接口添加像Handle
这样的方法,并使用多态性将其缩短为:You need to cast the items in
DeliveryTarget
to a concrete implementation of your interface, those properties are still there, they're just hidden to intellisense because you have a variable of typeIDeliveryTarget
which doesn't declare those properties you want.Do something like this:
This becomes cumbersome with lots of implementations, I'd suggest you add a method like
Handle
to theIDeliveryTarget
interface and use polymorphism to shorten that to: