✅ 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
using System.Collections;
using System.Runtime.CompilerServices;
using Chocco.Services.Availability.Core.Exceptions;
using Chocco.Services.Availability.Core.ValueObjects;
namespace Chocco.Services.Availability.Core.Entities;
public class Resource : AggregateRoot
{
private ISet<string> _tags = new HashSet<string>();
private ISet<Reservation> _reservations = new HashSet<Reservation>();
public IEnumerable<string> Tags
{
get => _tags;
private set => _tags = new HashSet<string>(value);
}
public IEnumerable<Reservation> Reservations
{
get => _reservations;
private set => _reservations = new HashSet<Reservation>(value);
}
public Resource(AggregateId id, IEnumerable<string> tags, IEnumerable<Reservation>? reservations = null, int version = 0)
{
var enumerable = tags.ToList();
ValidateTags(enumerable);
Id = id;
Tags = enumerable;
Reservations = reservations ?? Enumerable.Empty<Reservation>();
Version = version;
}
private static void ValidateTags(IEnumerable<string> tags)
{
var enumerable = tags.ToList();
if (tags is null || !enumerable.Any())
{
throw new MissingResourceTagsException();
}
if (enumerable.Any(string.IsNullOrWhiteSpace))
{
throw new InvalidResourceTagsException();
}
}
}using System.Collections;
using System.Runtime.CompilerServices;
using Chocco.Services.Availability.Core.Exceptions;
using Chocco.Services.Availability.Core.ValueObjects;
namespace Chocco.Services.Availability.Core.Entities;
public class Resource : AggregateRoot
{
private ISet<string> _tags = new HashSet<string>();
private ISet<Reservation> _reservations = new HashSet<Reservation>();
public IEnumerable<string> Tags
{
get => _tags;
private set => _tags = new HashSet<string>(value);
}
public IEnumerable<Reservation> Reservations
{
get => _reservations;
private set => _reservations = new HashSet<Reservation>(value);
}
public Resource(AggregateId id, IEnumerable<string> tags, IEnumerable<Reservation>? reservations = null, int version = 0)
{
var enumerable = tags.ToList();
ValidateTags(enumerable);
Id = id;
Tags = enumerable;
Reservations = reservations ?? Enumerable.Empty<Reservation>();
Version = version;
}
private static void ValidateTags(IEnumerable<string> tags)
{
var enumerable = tags.ToList();
if (tags is null || !enumerable.Any())
{
throw new MissingResourceTagsException();
}
if (enumerable.Any(string.IsNullOrWhiteSpace))
{
throw new InvalidResourceTagsException();
}
}
}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