V
V
Vlad2021-10-11 15:28:24
.NET
Vlad, 2021-10-11 15:28:24

Unit Of Work - Repository pattern and exception handling?

I want to implement the repository pattern in my work, and I was preoccupied with the question, where is it preferable to handle exceptions?
Inside the repository methods or outside, or both? If exception handling is done internally, then won't the next entry be overloaded?

public async Task<(bool, TFileRef?)> GetAsync(Guid id, CancellationToken cancellationToken)
        {
            try
            {
                using (var dbContext = dbContextFactory.CreateDbContext())
                {
                    var item = await dbContext.TFileRefs.FindAsync(id, cancellationToken);
                    if (item == null)
                    {
                        logger.LogWarning($"Item {0} not found", id);
                    }
                    return (true, item);
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"Item {0} not found", id);
                return (false, null);
            }
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question