[Best Practice?] API (MediatR) --> calling class library (Mediatr)? #787
Replies: 2 comments
-
|
Ah good question - I try to have MediatR as the top-level request/response in a system because things like behaviors get quite tricky. You can't turn them easily off/on for specific requests. From the diagram above it looks like that whole chain is in one process? If that's the case I might create my own little request/response library, that doesn't have all the features of MediatR, for all the "internal" request/responses. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the awesome answer! I see you active on Reddit too occasionally, its very cool to read your posts! I really appreciate your advice on this, I try to keep best practices as much as possible, it reduces the pain later as the code is used in other implementations and scales! I wrote a somewhat long winded comment, sorry. I wanted to provide as much context as possible. I have 2 summary questions posted at the bottom to try and save your eyes. Thanks again!! My Reply
Perfect, this makes complete sense. I think the take-away here is: have MediatR as the top-level request/response This keeps it clean, and free of issues like you mentioned. I was afraid that the overlapping
Yea, in this case it is one process, an example process is below. I tried to make the process as clear as possible, sorry if its confusing:
Usually for results I use something like FluentResults, sometimes I modify it, but it seems to be pretty flexible for my usecases. I think I you might be referring to implementing a CommandPattern similar to something one of these: Is this what you mean? I really appreciate the advice!! I will do this and make these changes! Thank you! Below, I elaborate on a use-case where I like to use MediatR in my libraries so I do not need to inject 10+ services in some cases. It's a bit long, but you might find it interesting. Class Library usecase for MediatR (skip this if you want)For example, I have this Cross-Platform Filesystem library I am building which is a modified version of: Singulink.IO.FileSystem - and Singularlink.Filesystem is a modified version of AlphaFS which is basically a FileSystem library for Windows, it adds extra functionality into In my library, I have some methods which parse paths into The
The goal would be able to perform something like this: var myAbsoluteFile = _pathParserService.Parse(somepath);
// It would be nice to have all these actions encapuslated within the IAbsoluteFilePath.
// This makes the API of IAbsoluteFilePath cleaner and all in a single 'place'.
myAbsoluteFile.Copy(someDestinationPath, out Progress progressReporter);
myAbsoluteFile.CreateHardLink(someDestaintion, someEnum);Instead of injecting all the services, I find it really clean to use public class SomeClass
{
private readonly IPathParserService _pathParserService;
public SomeClass(IPathParserService pathParserService)
{
_pathParserServicve = pathParserService;
}
public IAbsoluteFilePath Parse(string path)
{
// Some validations...
return _pathParserService.Parse(path);
}
}The path parser behaves something like this: public class PathParserService
{
private readonly IMediator _mediator;
public PathParserService(IMediator mediator)
{
_mediator = mediator;
}
public IAbsoluteFilePath Parse(string path)
{
// Some OS Platform path validations and such...
return new AbsoluteFilePath(path, _mediator);
}
}
// This is where IMediator saves your brain-power and code organization:
// If I did not use IMediator, instead of injecting only _mediator, would have to inject all of these services:
* `ICopyFileService`
* `IMoveFileService`
* `IBackupFileService`
* `IFileAttrebuteReaderService`
* `IFileHardlinkService`
* etc...The code above is pseudo-code for a FileSytem library, I agree with you that using Final Questions
Big thanks again! I hope that this thread is helpful to others as well who would like to use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello!
I am wondering best practices and if this is 'bad' class library design' or not. I understand that nested director handlers are bad from this issue here: #434
Scenario
I build many class library nuget packages. I like to put
MediatRin the nuget packages so it is cleaner. For example, a nuget package class library which reads.csprojfiles. (This is not my library, but it is similar to this library: ByteDev.DotNet.) Imagine that this library usesMediatRinternally to execute commands and return results.Then separately, I have an API application. The API application also uses
MediatR. The API application will have ByteDev.DotNet installed as a package.The API application will call a
API MediatR Handler-->API Service--> then the API service will call the class Library handler -->Class Library MediatR handler.Question
Thanks all!!
Beta Was this translation helpful? Give feedback.
All reactions