Մι∩z•thedev · Follow
Published in Coding · 6 min read · 1 day ago
__
👏65k 💬321 🔖 ⤴️
__
While investigating CQRS, I angled a couple of nice tips.
less typing, very convenient for dependency injection
public class QueryRoomAvailabilityService
{
private readonly IQueryable<RoomAvailability> _roomAvailabilities;
public QueryRoomAvailabilityService(IQueryable<RoomAvailability> roomAvailabilities)
{
_roomAvailabilities = roomAvailabilities;
}
}
becomes
public class QueryRoomAvailabilityService(IQueryable<RoomAvailability> roomAvailabilities)
{
}
but unhandy when the class inherits a base constructor.
public int? FindHotel(string hotelName, bool approx)
{
var exactMatch = back.Hotels
.FirstOrDefault(hotel => 0 == string.Compare(
hotel.HotelName,
hotelName,
CultureInfo.CurrentCulture,
CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols))
?.HotelId;
if (exactMatch != default)
{
return exactMatch;
}
if (!approx)
{
return default;
}
var approxMatch = back.Hotels
.FirstOrDefault(hotel => -1 != CultureInfo.InvariantCulture.CompareInfo.IndexOf(
hotel.HotelName,
hotelName,
CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols))
?.HotelId;
return approxMatch;
}
T[] array = Array.Empty<T>();
string a = string.Empty;
//assume array of nullable
Foo?[] nullableFooArray;
//filter null values and cast to non nullable, aka base type of Nullable<T>
Foo[] fooArray = nullableFooArray
.Where(nullableFoo => nullableFoo != null)
.Select(nullableFoo => nullableFoo!) // here ! means : believe me it is non null, indeed I took care to filter at previous statement
.ToArray();
//1.Environment.ExpandEnvironmentVariables to resolve environment variables
//2.Path.GetFullPath to normalize path separator characters
var rootDir =
Path.GetFullPath(Environment.ExpandEnvironmentVariables(Path.Combine(
"%ProgramData%", "Vendor", "Product", "subfolder"
)));