Eventbus Event Bus Mode instance (publish/Subscribe event)

Source: Internet
Author: User
Tags eventbus

In our company often use the bus, the specific bus is what let me understand I do not know, but in the past few months, I already know how to use the bus, now plus an example to explain how the bus is used.

1. First of all we create a new class, this class is actually used for the bus transfer model

Using System;

Namespace PurchaseDevices.Model.CommonModel
{
<summary>
Event Pass Parameters
</summary>
<typeparam name= "T" ></typeparam>
public class Flowdataeventargs<t>: EventArgs
{
Public Flowdataeventargs (T data)
{
data = data;
}
Public T Data {get; set;}
}
}

2. Create a Class (Flowservicebus), inside to write the method of the bus.

public class Flowservicebus

{
Static Flowservicebus _flowservicebus;

public static Flowservicebus Instance
{
Get
{
if (_flowservicebus = = null) _flowservicebus = new Flowservicebus ();
return _flowservicebus;
}
}

}

4. Write a method that hangs the bus inside

public class Flowservicebus

{
Static Flowservicebus _flowservicebus;

public static Flowservicebus Instance
{
Get
{
if (_flowservicebus = = null) _flowservicebus = new Flowservicebus ();
return _flowservicebus;
}
}

public void Configmsgdispachrule () {

Hanging bus

}

}

5. Let's talk about our traditional method of invoking another class

Create a new object model class

public class Farmercardorratesellmodel
{
Public Farmercardorratesellmodel ()
{
Packagehead = 0xAA;
FixedLength = 11;
}

<summary>
Frame Header
Frame start tag, 0xAA
</summary>
Public byte packagehead {get; set;}
<summary>
Recipient, identifying who the data will be sent to
1-pound code host, 2-tier keypad, 3 cigarette swipe.
</summary>
Public byte Receiver {get; set;}
<summary>
sender, identifying who sent the data
1-pound code host, 2-tier keypad, 3 cigarette swipe.
</summary>
Public byte Sender {get; set;}
<summary>
ZigBee Communication Address
1~15
</summary>
Public byte zigbeeaddress {get; set;}
<summary>
Frame number
0~255
</summary>
Public byte framenumber {get; set;}
<summary>
Command code
</summary>
Public byte Command {get; set;}
<summary>
Number of data area bytes
///
</summary>
public int Datalength {get; set;}
<summary>
Data area, none (data area is 0)
</summary>
Public byte[] Data {get; set;}
<summary>
From receiver to the entire data and checksum
</summary>
Public byte CRC {get; set;}
<summary>
The length of the data area is fixed at 11
</summary>
public int FixedLength {get; set;}

}

What we're going to do now is to enter different methods according to the sender sender.

private void Dissendertodisway (Farmercardorratesellmodel farmercardorratesellmodel)
{
Devtypeenum devtypeenum = (devtypeenum) Farmercardorratesellmodel.sender;                                                                                                                                                           Here is an enumeration, which is not explained in detail here, which means Sender has different values.
Switch (devtypeenum)
{

Case Devtypeenum.carddev:
Carddev carddev=new Carddev ();

Carddev.carddevway (Farmercardorratesellmodel);

Break
Case Devtypeenum.leveldev:

Leveldev leveldev=new Leveldev ();

Leveldev.leveldevway (Farmercardorratesellmodel);

Break
Default
Logger.getinstance (). Warn ("unidentified sender");
Break
}
}

The method to be executed when sender is Carddev

public class carddev{

public void Carddevway (Farmercardorratesellmodel farmercardorratesellmodel) {

Method of execution

}

}

The method to be executed when sender is Leveldev

public class leveldev{

public void Leveldevway (Farmercardorratesellmodel farmercardorratesellmodel) {

Method of execution

}

}

}

In the traditional method, one class would refer to the method of another class, we must new an object and then reference the method. This writing is not wrong, I used to write this, for some of the non-complicated program I can write, but in some of the program with the device interaction, mostly the bus will connect the methods, we do not have to care about who is the sender, who is the recipient, each method only care I get this pass over the value, what to do, I don't have to worry about who I should send the results of this method to. So low coupling.

Now let's talk about the method of using the bus, how to write:

Or keep the Farmercardorratesellmodel class.

Here's how we use events to write:

Write event:

public event eventhandler<flowdataeventargs<farmercardorratesellmodel>> Onreceivecarddataeventhadler;

Public event Eventhandler<flowdataeventargs<farmercardorratesellmodel>> Onreceiveleveldataeventhadler ;

private void Dissendertodisway (Farmercardorratesellmodel farmercardorratesellmodel)
{
Devtypeenum devtypeenum = (devtypeenum) Farmercardorratesellmodel.sender;
Switch (devtypeenum)
{

Case Devtypeenum.carddev:

if (onreceivecarddataeventhadler!=null)
{
Onreceivecarddataeventhadler (This,new flowdataeventargs<farmercardorratesellmodel> ( Farmercardorratesellmodel));
}

Break
Case Devtypeenum.leveldev:

if (onreceiveleveldataeventhadler!=null)
{
Onreceiveleveldataeventhadler (This, new flowdataeventargs<farmercardorratesellmodel> ( Farmercardorratesellmodel));

}

Break
Default
Logger.getinstance (). Warn ("unidentified sender");
Break
}
}

We're going to write two methods, both of which are sender for Leveldev and Carddev, and they get the data they need to deal with.

The method to be executed when sender is Carddev

public class carddev{

public void Carddevway (object sender, Flowdataeventargs<farmercardorratesellmodel> e) {

var data=e.data; Data is the Farmercardorratesellmodel object to be transmitted.

}

}

The method to be executed when sender is Leveldev

public class leveldev{

public void Leveldevway (object sender, Flowdataeventargs<farmercardorratesellmodel> e) {

var data=e.data; Data is the Farmercardorratesellmodel object to be transmitted.

}

}

Write here we have the respective internal methods completed, and now we want to complete is how the method and method directly in series, how to let sender for Carddev when the corresponding Carddevway method to the south. So now it's time to hang up the bus.

Remember the way we write in the bus class:

Now we're going to add some code in there.

public class Flowservicebus

{
Static Flowservicebus _flowservicebus;

public static Flowservicebus Instance
{
Get
{
if (_flowservicebus = = null) _flowservicebus = new Flowservicebus ();
return _flowservicebus;
}
}

New Object

Private readonly Dissendertodisway _dissendertodisway=new Dissendertodisway ();

Private readonly Carddev _carddev=new Carddev ();

Private readonly Leveldev _leveldev=new Leveldev ();

public void Configmsgdispachrule () {

Eventbroker eventbroker =new eventbroker ();

EventBroker.SpecialCasesRegistrar.AddPublication ("Sender is Carddev method", _dissendertodisway,
"Onreceivecarddataeventhadler", Handlerrestriction.none);

Eventbroker.specialcasesregistrar.addsubscription<flowdataeventargs<farmercardorratesellmodel>> (" The sender is the Carddev method ",
_carddev, _carddev.carddevway, New Onpublisher ());

This is a set of corresponding can be understood: when executed to the Dissendertodisway class inside the Onreceivecarddataeventhadler event, the execution of Carddev inside the Carddevway method

According to the above, when we execute the Onreceiveleveldataeventhadler event inside the Dissendertodisway class, we should then execute the Leveldevway method in the Leveldev class south

EventBroker.SpecialCasesRegistrar.AddPublication ("Sender is Leveldev method", _dissendertodisway,
"Onreceiveleveldataeventhadler", Handlerrestriction.none);
Eventbroker.specialcasesregistrar.addsubscription<flowdataeventargs<farmercardorratesellmodel>> (" The sender is the Leveldev method ",
_leveldev, _leveldev.leveldevway, New Onpublisher ());

}

}

Bus this to slowly understand, just at the beginning I do not know how to hook up two methods, slowly understand on the line. We seldom use the bus in the usual code, but for some companies it may still be used frequently, and for those friends who have just come into contact with the bus, I hope it will be helpful for you to understand and use it briefly.

Eventbus Event Bus Mode instance (publish/Subscribe event)

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.