Replies: 4 comments 14 replies
-
|
I may be misunderstanding something, but you are not calling logging twice, but rather calling it once for each branch, as the message is different. So there's nothing bad in it. Could you elaborate more? |
Beta Was this translation helpful? Give feedback.
-
|
I don't see any better solutions using the same code you've provided, it's already correct in terms of logic. I would rather change more than moving one mapping method outside of match branches. Since you are using v4, it has some "limitations" in the context of using different monads in the same context. For example, your calling method is using static class Errors
{
public enum Code
{
FailedToGetThreadError
}
public static readonly Error FailedToGetThreadError = Error.New((int)Code.FailedToGetThreadError, "FailedToGetThreadError");
}
class Repository(ILogger logger, DbCtx ctx, IMapper mapper)
{
public Aff<CreatedConversationThreadDto> CreateConversationThread(Cmd cmd, Guid sellerUserId, CancellationToken ct) =>
(from thread in GetThread(cmd, sellerUserId, ct)
from created in MapConversationThreadToCreatedConversationThreadDto(thread)
from _ in Eff(() =>
{
logger.LogInformation("Retrieving existing conversation thread with ID: {ThreadId}", thread.Id);
return unit;
})
select default(CreatedConversationThreadDto))
| @catch((int)Errors.Code.FailedToGetThreadError, _ => CreateNewThread(cmd, sellerUserId, ct));
Aff<ConversationThread> GetThread(Cmd cmd, Guid sellerUserId, CancellationToken ct) =>
OptionalAsync(
ctx.Threads
.AsNoTracking()
.FirstOrDefaultAsync(q =>
q.ItemId == cmd.ConversationThread.ItemId &&
q.BuyerUserId == cmd.ConversationThread.BuyerUserId,
ct)).ToAff();
Aff<CreatedConversationThreadDto> CreateNewThread(Cmd cmd, Guid sellerUserId, CancellationToken ct) =>
from thread in ConversationThread.Create(cmd.ConversationThread.ItemId, sellerUserId, cmd.ConversationThread.BuyerUserId)
from created in MapConversationThreadToCreatedConversationThreadDto(thread)
from _ in Eff(() =>
{
logger.LogInformation("Creating new conversation thread for item with ID: {ItemId} seller with ID: {SellerUserId} and buyer with ID: {BuyerUserId}",
cmd.ConversationThread.ItemId,
sellerUserId,
cmd.ConversationThread.BuyerUserId);
return unit;
})
select default(CreatedConversationThreadDto);
Eff<CreatedConversationThreadDto> MapConversationThreadToCreatedConversationThreadDto(ConversationThread thread) =>
Eff(() => mapper.MapConversationThreadToCreatedConversationThreadDto(thread));
} |
Beta Was this translation helpful? Give feedback.
-
|
After update to 5.0.0-beta-54 Option and Either does not have MatchAsync so how can I replace it? @aloslider |
Beta Was this translation helpful? Give feedback.
-
|
What do you guys think about this? Is there anything that I can improve? And Why return await liftAsync(async () => await marketplaceApi.GetSellerUserIdAsync(
cmd.ConversationThread.ItemId,
ct
))
.Bind(sellerUserId => liftAsync(async () => Optional(await ctx.Threads
.AsNoTracking()
.FirstOrDefaultAsync(q =>
q.ItemId == cmd.ConversationThread.ItemId &&
q.BuyerUserId == cmd.ConversationThread.BuyerUserId,
ct
)
))
.Bind(q => q.Match(
Some: thread =>
{
logger.LogInformation(
"Retrieving existing conversation thread with ID: {ThreadId}",
thread.Id
);
return pure(thread);
},
None: () =>
{
logger.LogInformation(
"Creating new conversation thread for item with ID: {ItemId} seller with ID: {SellerUserId} and buyer with ID: {BuyerUserId}",
cmd.ConversationThread.ItemId,
sellerUserId,
cmd.ConversationThread.BuyerUserId
);
return ConversationThread
.Create(
cmd.ConversationThread.ItemId,
sellerUserId,
cmd.ConversationThread.BuyerUserId
)
.Match(
Succ: thread => liftAsync(async () =>
{
ctx.Add(thread);
await ctx.SaveChangesAsync(ct);
return thread;
}),
Fail: err =>
{
logger.LogWarning(
"Failed to create conversation thread: {Error}",
err.Message
);
return fail<ConversationThread>(err);
}
);
}
)))
.Map(mapper.MapConversationThreadToCreatedConversationThreadDto)
.RunSafeAsync(); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
Currently, in the logic for retrieving or creating a ConversationThread, I call the mapper twice to map the entity to CreatedConversationThreadDto:
Can I somehow use some method and map it at the end? Any other suggestions to my code would be great because it's my first time using this lib
Beta Was this translation helpful? Give feedback.
All reactions