Last active
July 17, 2024 10:30
-
-
Save Muhammad-1990/9dd62a9b791f7bcd157aedef43576980 to your computer and use it in GitHub Desktop.
Combine Predicates and Actions for a clean and efficient way to manage in memory lists.
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.Globalization; | |
| var _cache = new Dictionary<string, CacheItem<Transfer>>() { | |
| { "1", new CacheItem<Transfer> { Item = new Transfer { Id = 100, Status = TransferStatus.Completed, LastModified = DateTime.ParseExact("2024-07-16 11:00", "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture) } } }, | |
| { "2", new CacheItem<Transfer> { Item = new Transfer { Id = 200, Status = TransferStatus.Incomplete, LastModified = DateTime.ParseExact("2024-07-16 11:00", "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture) } } }, | |
| { "3", new CacheItem<Transfer> { Item = new Transfer { Id = 300, Status = TransferStatus.Canceled, LastModified = DateTime.ParseExact("2024-07-16 11:00", "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture) } } }, | |
| }; | |
| // Global method to access and clean up cache. | |
| void CleanUpCache<T>(KeyValuePair<string, CacheItem<T>> entry) => | |
| _cache.Remove(entry.Key); | |
| // Filter for specific type and condition. | |
| Predicate<KeyValuePair<string, CacheItem<Transfer>>> staleTransfers = transfer => | |
| transfer.Value.Item.Status is (TransferStatus.Incomplete or TransferStatus.Canceled) | |
| && | |
| transfer.Value.Item.LastModified.AddHours(1) <= DateTime.Now; | |
| // Execution on each item. | |
| Action<KeyValuePair<string, CacheItem<Transfer>>> removeStaleTransfers = transfer => | |
| CleanUpCache(transfer); | |
| // Do work. | |
| _cache.ToList().FindAll(staleTransfers).ForEach(removeStaleTransfers); | |
| public class CacheItem<T> | |
| { | |
| public T Item { get; set; } | |
| } | |
| public partial class Transfer | |
| { | |
| public int Id { get; set; } | |
| public TransferStatus Status { get; set; } | |
| public DateTime LastModified { get; set; } | |
| } | |
| public enum TransferStatus : int | |
| { | |
| Completed = 1, | |
| Incomplete = 2, | |
| Canceled = 3, | |
| AwaitingFunds = 4 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Predicates, Funcs and Actions can make your life so much easier.
Simply put they offer a way to pass around a method as a parameter and combing them can be an effective strategy.
This example showcases how to take care of managing a cache dictionary, cleaning it up by removing drop-offs older than 1 hour. This effectively leads to better memory usage and faster response times as the cache is kept to a minimum, all while enforcing clean, extendable, and testable code.
This implementation can also be extended to support abandoned order baskets, placing back orders for specific products or even sending out a reengagement notification.
In summary, Func is for returning values, Action is for performing actions, and Predicate is a specialized form of Func<T, bool> for boolean evaluations.
You can find additional resources on @MicrosoftLearning or Microsoft Learn Online