LazyGuard
✅ Which API design is better
I have orders that can be put in an issue status and then resolve that issue
There is two options:
1. The first one is having two different endpoints
PATCH /orders/{orderId}/create-issue
with the following body
and
PATCH /orders/{orderId}/resolve
with the following body
2. The second one is having a single endpoint
PATCH /orders/{orderId}/issue
and we either pass an issueDate + reason
OR resolveDate + solution
plus a validation if we pass inconsistent data (for example passing a body that contains a reason + solution
will result in a validation error since we can not create an issue and resolve it at the same time
Question: Which option is better and why?2 replies
✅ Functional programming instead of exceptions
I've watched this video where Derek implements an Either from scratch to make method signatures more explicit rather than throwing an exception. He also adds a Map method to force the client code to deal with both cases
However, the problem is that not every method has one successful path and one failure path. For example, the
PostAsync
. has many possible ways to fail (see exceptions here https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.postasync?view=net-8.0 ). So how to deal with this kind of situations ? especially if we want to keep the granularity of all the different ways to fail and we want to handle different failures in different ways in the client code35 replies
✅ How to modernize this code ?
So, I have found this .Net Core 3.1 code and want to make it more modern.
PArticulary, what I don't like is that converting tags into List. IF I remove it the compiler warns me about multiple possible enumeration (what the heck is this !)
Another thing is there a way to keep exposing an IEnumerable but having Sets as implementation?
Any other suggestions are also welcome as well
60 replies
How to cope with this downside of MediatR?
This is a quote from Derek Comartin that describes well the issue. "One of the biggest downside to in-process processing (which MediatR does), is it does not isolate the actual handling of a request. This isn’t a problem with MediatR at all, just the nature of it executing in-process. Everything is done in-process. Meaning that wherever process is calling mediator.Send() is also the same process that is executing the relevant Handler for that request.
A notification can have zero or multiple handlers. However the execution of all the notification handlers are done all in the same process. If you publish a notification to MediatR, and there are 3 handlers for that notification, it will execute all 3. But what happens if one of them fails and throws an exception? That exception will bubble up back to where you published the message via MediatR. How do you want to handle this? Can you re-publish that message again and have the handlers that succeeded run again? What if one of them was sending an email ?"
Now my question is, other than moving to out-of-process messaging, how to cope with this disadvantage ?
5 replies
How to test this code?
I am trying to refactor some code in a background service that looks like this:
Complex work should usually be kept somewhere else outside of the loop of the backgroundService. However, things get hairy enough that some tests will help.
5 replies
❔ Refactoring strategy pattern to a simpler code
I have the following code and I want te refactor it to a less-boiler-plate code.
Wha'ts suposed to do is that the
3. send to those systems
Handle
method should take a product as input and
1.decides to which output system it needs to send information (depending on some configuration and on the product itself). There could be more thant 1 output systems to send the product to.
2. convert the Product to the appropriate format for chosen output systems.3. send to those systems
14 replies
❔ Prevent accepting numeric values as enum
Hello, I have an API that exposes an enum field Vehicle (Car, Bike, Truck) in the POST. The problem is that when I POST a request with a body
{"vehicle" :"1" }
, it gets accepted and gets automatically converted to Vehicle.Bike.. But something like {"vehicle" :"bar" }
results in a bad request, which is what I need !
Any idea on how to stop parsing integer like enum values?31 replies
✅ How to parse this weird API ?
I have a very akward thing to do and I d'ont know if there is a clean way to do it.
In a REST API, there is two string fields
In my Domain I have a class with all possible properties
what I need to do is, depending on the AttributeName and AttributeValue given in the API, I want to create an instance of Product which has all everything equal to null except for the property for which we were given the name and value in AttributeName and AttributeValue. (there is always a single one) For exemple
attributeName
and attributeValue
(for example { "attributeName" : "isAllowed" , " attributeValue" : "true"}
and { "attributeName" : "productType" , " attributeValue" : "A50"}
, in the API this is modelled by the following class
N.B: the "attribute" here have nothing to do with any .NET vocabulary, it's just a name, think of it like "foo" or "bar"
In my Domain I have a class with all possible properties
what I need to do is, depending on the AttributeName and AttributeValue given in the API, I want to create an instance of Product which has all everything equal to null except for the property for which we were given the name and value in AttributeName and AttributeValue. (there is always a single one) For exemple
{ "attributeName" : "isAllowed" , " attributeValue" : "true"}
would result in a product
Instance with product.IsAllowed = true
and product.ProductType = null
(and other fields will be null as well)
Another example, { "attributeName" : "productType" , " attributeValue" : "A50"}
would result in product
Instance with product.IsAllowed = null
and product.ProductType = A50
(and other fields will be null as well)122 replies
❔ Where to put this logic ?
Hi folks,
I have a very simple project that have 3 layers : Api, Domain and Infrastructure
I have a mongo collection which contain users.
I want to implement a very simple feature. Delete a user by its Id.
I have two options :
1. In the domain I first try to get a user by the given Id. If it's there I delete it, if not I throw a NotFoundException. This exception is then catched in the API layer to return a Not Found Status code. The disadvantage of this approach is that I am doing 2 access to the database.
2. I simply try to delete the user in the infrastructure, then I use the WriteResult of mongo. And there are here two sub-options:
i. Either In the infrastructure, I check that the nbRemoved is not 0. If it is the case, the infrastructure throw a NotFoundExceptionthat will be catched in the API and mapped to Not Found status code. In this case, I think the disadvantage is that the domain does not contain much, it's a logic that should be in the domain but that's put in the infrastructure.
ii Or the infrastructure will just return nbRemoved (and not the whole WriteResult) and thus the domain checks if it's 0 and throw the exception
I am trying to choose between 1. and 2.i and 2.ii. Any help ?
36 replies
❔ How to design this code ?
Hi folks, I am having some troubles coming up with a design for the following problem.
I have a Product object that have a several properties (e.g. category (a string), dangerous (a boolean), type (enum), length(int), height(int), weight(int) etc.)
There is a set of rule that allows to set the value of some properties based on conditions on other properties. For example : Set the
Type
property to "Big" when Length
>2 and Weight
>15. Another example : "Set the Dangerous
to true
when Category
== fireworks
.
Those rule must not be harcoded in the code, because our users want to have the possibility to add such rules on their own.
Does anyone have an idea on how to do this ? Some of my pain point are :
- How to model a rule in a nosql database ?
- How to apply it seamlessly on the Product objects etc.38 replies
❔ How to deserialize correctly
Hi, I have the following records :
this is a little bit weird (the name Product is used twice), because unfortunately the records are used to deserialize objects returned from an external API that returns a list either empty or have the following format:
To deserialize, I am using
The problem is that I don't know how to do this properly in order not to have the warnings. Any idea ?
11 replies
❔ Is there a way to improve this code Performance ?
have a collection of items that have
_id
and serialNumbers
(an array) fields (and other fields as well but irrelevant for the question).
I want to do the following:
0. Check that the newSerialNumber
is not used in any document.
1. Filter to find out the item with the given id
, lets name it ItemX
2. Get all the items that have the same serialNumbers as ItemX
. Let's name them ItemsWithSameSerialNumbersAsItemX
3. For each one of the ItemsWithSameSerialNumbersAsItemX
, push the newSerialNumber
I am doing it as following:
Here I am making 4 calls to the database (3 finds and 1 update). Is there some way to write this more efficiently ?5 replies