NService Bus' Publish()'基本界面不发送混凝土子类属性

发布于 2025-01-21 03:12:33 字数 1247 浏览 0 评论 0原文

我使用界面使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南城追梦 2025-01-28 03:12:33

@MindSwipe 提到的另一种解决方案是为每个具体类型的事件提供专用属性,即:

context.Publish(new UploadSuccess
{
   EmailTargets = new List<EmailTarget>(),
   FaxTargest = new List<FaxTarget>();
} 

这将允许您在处理程序中访问具体类型的属性,而无需进行强制转换。

An alternative solution to what @MindSwipe mentioned is to have dedicated properties on the event for each of the concrete types ie.:

context.Publish(new UploadSuccess
{
   EmailTargets = new List<EmailTarget>(),
   FaxTargest = new List<FaxTarget>();
} 

This will allow you to access concrete types' properties in a handler without the need for casting.

别再吹冷风 2025-01-28 03:12:33

您需要将 DeliveryTarget 中的项目转换为接口的具体实现,这些属性仍然存在,它们只是对智能感知隐藏,因为您有一个 IDeliveryTarget 类型的变量> 它没有声明您想要的那些属性。

做这样的事情:

public async Task Handle(UploadSuccess message, IMessageHandlerContext ctx)
{
    foreach (IDeliveryTarget target in message.DeliveryTargets)
    {
        if (target is EmailTarget emailTarget)
        {
            Console.WriteLine($"Got email target with subject {emailTarget.Subject}");
        }
        else if ( ... )
    }
}

这对于很多实现来说变得很麻烦,我建议您向 IDeliveryTarget 接口添加像 Handle 这样的方法,并使用多态性将其缩短为:

public async Task Handle(UploadSuccess message, IMessageHandlerContext ctx)
{
    foreach (IDeliveryTarget target in message.DeliveryTargets)
        await target.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 type IDeliveryTarget which doesn't declare those properties you want.

Do something like this:

public async Task Handle(UploadSuccess message, IMessageHandlerContext ctx)
{
    foreach (IDeliveryTarget target in message.DeliveryTargets)
    {
        if (target is EmailTarget emailTarget)
        {
            Console.WriteLine(
quot;Got email target with subject {emailTarget.Subject}");
        }
        else if ( ... )
    }
}

This becomes cumbersome with lots of implementations, I'd suggest you add a method like Handle to the IDeliveryTarget interface and use polymorphism to shorten that to:

public async Task Handle(UploadSuccess message, IMessageHandlerContext ctx)
{
    foreach (IDeliveryTarget target in message.DeliveryTargets)
        await target.Handle();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文