Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed imports in README.md #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# mediatr_py

[![PyPI](https://img.shields.io/pypi/v/mediatr)](https://pypi.org/project/mediatr)
[![Python](https://img.shields.io/pypi/pyversions/mediatr)](https://pypi.org/project/mediatr)
[![Downloads](https://img.shields.io/pypi/dm/mediatr)](https://pypi.org/project/mediatr)
[![Python](https://img.shields.io/pypi/pyversions/mediatr)](https://pypi.org/project/mediatr)
[![Downloads](https://img.shields.io/pypi/dm/mediatr)](https://pypi.org/project/mediatr)

<a href="https://www.buymeacoffee.com/megafetis" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>

Expand All @@ -11,7 +11,8 @@ This is an async implementation of Mediator pattern with pipline behaviors.
It is a port of [Mediatr](https://github.com/jbogard/MediatR) from .Net C#

Requirements:
* Python >= 3.6

- Python >= 3.6

## Usage:

Expand All @@ -32,17 +33,17 @@ class GetArrayQuery():
### Define your handler class or function

```py
import Mediator from mediatr
from mediatr import Mediator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, was gonna open a PR about this myself as well.


@Mediator.handler
async def get_array_handler(request:GetArrayQuery):
items = list()
for i in range(0, request.items_count):
items.append(i)
return items

# or just Mediator.register_handler(get_array_handler)

```

or class:
Expand All @@ -55,14 +56,14 @@ class GetArrayQueryHandler():
for i in range(0, request.items_count):
items.append(i)
return items

# or just Mediator.register_handler(GetArrayQueryHandler)
```

### Run mediator

```py
import Mediator from mediatr
from mediatr import Mediator

mediator = Mediator()

Expand All @@ -80,11 +81,10 @@ print(result) // [0,1,2,3,4]
>
> In another case use `asyncio` module for manual manage of event loop in synchronous code


### Run mediator statically, without instance

```py
import Mediator from mediatr
from mediatr import Mediator

request = GetArrayQuery(5)

Expand All @@ -97,10 +97,10 @@ print(result) // [0,1,2,3,4]
```

Note that instantiation of `Mediator(handler_class_manager = my_manager_func)` is useful if you have custom handlers creation. For example using an injector.
By default class handlers are instantiated with simple init: `SomeRequestHandler()`. handlers or behaviors as functions are executed directly.

By default class handlers are instantiated with simple init: `SomeRequestHandler()`. handlers or behaviors as functions are executed directly.

## Using behaviors

You can define behavior class with method 'handle' or function:

```py
Expand Down Expand Up @@ -137,12 +137,11 @@ def default_handler_class_manager(HandlerCls:type,is_behavior:bool=False):

```


For example, if you want to instantiate them with dependency injector or custom, pass your own factory function to Mediator:

```py
def my_class_handler_manager(handler_class, is_behavior=False):

if is_behavior:
# custom logic
pass
Expand All @@ -152,12 +151,11 @@ def my_class_handler_manager(handler_class, is_behavior=False):
mediator = Mediator(handler_class_manager=my_class_handler_manager)

```
PS:

PS:

The `next` function in behavior is `async`, so if you want to take results or if your behavior is async, use `middle_results = await next()`


Handler may be async too, if you need.

## Using with generic typing support (version >= 1.2):
Expand Down