Masstransit Learning Record (Miscellaneous) Iconsumer generic implementation and multiple inheritance

Source: Internet
Author: User

As mentioned before, masstransit consumption three ways, consumer way is one of the,iconsumer<t> is the way of the agreed interface.

In general, each exchange corresponds to a consumer, which is the general code as follows

    public class someconsumer:iconsumer<classa>    {public        Task consume (consumecontext<classa> Context)        {            throw new notimplementedexception ();        }    }
When a consumer is used for the same class of messages (which have a common base class), then we typically implement the consumer through generics (inheritance)
    public class genericconsumer<t>: Iconsumer<t>        where T:class, icommand// Here ICommand is the base class for all the received exchange, which does not discuss broadcast or send when you can specify that Exchange is sent, actually other types of messages    {//All messages are based on the same base class, can be implemented by generics consumer Public        Task consume (consumecontext<t> context)        {            //do some thing            //make some operations based on ICommand            throw new NotImplementedException ();        }    }
At this point you can receive all Exchange-ICommand messages by using the following code
                Configure. Receiveendpoint (Host, "Allcommand", E + =                {                    E.consumer () = new genericconsumer<icommand> ()); /Of course this is the premise that the message sender has specified exchange as ICommand, such as rbbus.publish<icommand> (new {Value = text});                });
At this time, there is a situation, although not common, but there is a situation, it is that these messages have nothing to do with each other, but they have to do something common, this time the generics must be out of line (of course, you say the code to let all messages inherit the same base class can also, but this is not considered in this way), And the first kind of declaration of the way although can be achieved, but to write N consumer implementation, receive part also write N E.consumer delegate, this is certainly not a best implementation, then how to do? Well, that's a lot of inheritance, the code is as follows
public class Commonconsumer:iconsumer<classa>, Iconsumer<classb>, iconsumer<class1> {// ClassA and ClassB have nothing to do with them, they are just some of the same properties, assuming they all have ID and Value property//class1 and ClassA, ClassB even attribute names are not the same public async Task consume        (consumecontext<classb> context) {await this.} DoSomething (context.        Message); Public async Task Consume (consumecontext<classa> context) {await this. DoSomething (context.        Message); Public async Task Consume (consumecontext<class1> context) {Dynamic message = context. message;//assumes that CLASS1 does not have an ID and value, which is the CID and cvalue await this. DoSomething (New {Id = message. CId, Value = message.        Cvalue}); The private async Task dosomething (Dynamic message) {//do something//assumes that all have id,value properties, and then You can do some processing with the DLR for Message.id and Message.value}}
And the receiving code is simple as follows
                Configure. Receiveendpoint (Host, "Allcommand", E = =                {                    E.consumer () = new Commonconsumer ());                
Note that this is when the message sender pushes on the message push or on their own exchange, which is not the same as the second generic consumer


Masstransit Learning Record (Miscellaneous) Iconsumer generic implementation and multiple inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.