Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.
/ EventToolkit Public archive

Succinct, dependency-free, ioc-friendly utilities for publishing / subscribing to events as messages within the application domain.

License

Notifications You must be signed in to change notification settings

gettyimages/EventToolkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EventToolkit

Succinct, dependency-free, ioc-friendly utilities for publishing / subscribing to events within the application domain.

Events

Events aid decoupling and separation of concerns.

public class Fee {
    public RecordPayment(amountPaid) {
      if (amountPaid >= amountOwed)
        EventBus.Publish(new FeePaidOff { Fee = this });
    }
}

[Test]
public void Should_notify_when_the_balance_is_paid_off() {
    Fee paidOffFee = null;
    EventBus.Subscribe<FeePaidOff>(e => paidOffFee = e.Fee);

    var customer = new Customer();
    var fee = customer.ChargeFee(100m);
    fee.RecordPayment(100m);

    paidOffFee.ShouldEqual(fee);
}

Moving non-essential code such as logging into event handlers can isolate your core domain from infrastructure concerns.

public class CustomerMakesPurchaseHandler : Handles<OrderPaid> {
    readonly ILogger logger;

    public CustomerMakesPurchaseHandler(ILogger logger) {
        this.logger = logger;
    }

    protected override void Handle(OrderPaid message) {
        logger.Log(message);
    }
}

Transactions

Events raised within a TransactionScope will be raised once the transaction is completed, or lost if the transaction is cancelled.

    EventBus.Subscribe<FeePaidOff>(e => Console.Log("Fee paid off!"));

    using (var trx = new TransactionScope()) {
        EventBus.Publish<FeePaidOff>(new FeePaidOff { Fee = this });
        Console.Log("Committing transaction...");
        trx.Complete();
    }

    // prints:
    // Comitting transaction...
    // Fee paid off!

Thanks

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Succinct, dependency-free, ioc-friendly utilities for publishing / subscribing to events as messages within the application domain.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published