Last active
July 19, 2023 14:54
-
-
Save caiogranero/d42c6e10820b8af8249e0bf6e59f1883 to your computer and use it in GitHub Desktop.
ChangeTracking behaviour inside foreach with try/catch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
new MyClass().Main(); | |
} | |
} | |
class RoomDto | |
{ | |
public int Id { get; private set; } | |
public bool EmUso { get; private set; } | |
public Queue<PendingChange> PendingChanges { get; private set; } | |
public RoomDto(int id, bool emUso) | |
{ | |
Id = id; | |
EmUso = emUso; | |
PendingChanges = new Queue<PendingChange>(); | |
} | |
public void SetEmUso() | |
{ | |
Logger.LogInfo($"Alterando status da sala {Id} para EmUso"); | |
EmUso = true; | |
PendingChanges.Enqueue(new PendingChange($"Status da sala {Id} para EmUso.", Status.Modified)); | |
} | |
} | |
internal class PendingChange | |
{ | |
public string Action { get; set; } | |
public Status Status { get; set; } | |
public PendingChange(string action, Status status) | |
{ | |
Action = action; | |
Status = status; | |
} | |
} | |
public enum Status | |
{ | |
Modified, | |
Added, | |
Deleted | |
} | |
class MyClass | |
{ | |
private RoomDto[] Rooms { get; set; } | |
public MyClass() | |
{ | |
Rooms = new[] { new RoomDto(1, false), new RoomDto(2, false)}; | |
} | |
public void Main() | |
{ | |
var appointment = 1; | |
for (var currentIndex = 0; currentIndex < Rooms.Length; currentIndex++) | |
{ | |
var room = Rooms[currentIndex]; | |
try | |
{ | |
Logger.LogInfo($"Iniciando alocação da sala {room.Id} / Iteração {currentIndex}"); | |
room.SetEmUso(); | |
SaveChanges(currentIndex); | |
} | |
catch (Exception e) | |
{ | |
Logger.LogError(e.Message); | |
} | |
} | |
} | |
private void SaveChanges(int currentIteration) | |
{ | |
Logger.LogInfo($"Salvando alterações no banco de dados"); | |
var roomsWithPendingChanges = Rooms.Where(x => x.PendingChanges.Any()).ToList(); | |
Logger.LogInfo($"Quantidade de alterações pendentes: {roomsWithPendingChanges.Count()}"); | |
var isFirstIteration = currentIteration == 0; | |
if (isFirstIteration) | |
{ | |
throw new InvalidOperationException("Oh No! A deadlock just happened in our amazing database!"); | |
} | |
foreach (var room in roomsWithPendingChanges) | |
{ | |
while (room.PendingChanges.Count > 0) | |
{ | |
var pendingChange = room.PendingChanges.Dequeue(); | |
Logger.LogInfo($"Action: {pendingChange.Action} | Status: {pendingChange.Status}"); | |
} | |
} | |
} | |
} | |
static class Logger | |
{ | |
public static void LogInfo(string message) | |
{ | |
Console.WriteLine($"INFO: {message}"); | |
} | |
public static void LogError(string message) | |
{ | |
Console.WriteLine($"ERROR: {message}"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment