Skip to content

Instantly share code, notes, and snippets.

@DavidRogersDev
DavidRogersDev / CreateNamespace.js
Created August 24, 2022 02:18
CreateNamespace is a Function which can be used to Namespace your code if you are working with an old codebase.
// function from the old days before Modules. Was very handy if you wanted to namespace your code.
if (!window.createNamespace) {
window.createNamespace = function (name, separator, container) {
var o = container || window;
name.split(separator || '.').forEach(function (x) {
o = o[x] = o[x] || {};
});
return o;
};
}
@DavidRogersDev
DavidRogersDev / news.com.au-remove-video.js
Last active May 5, 2022 02:27
A very small Greasemonkey script to remove the Video from pages on news.com.au It also removes the ad banner at the top.
// ==UserScript==
// @name news.com.au - Video Be Gone
// @description removes video and top ad from news.com.au stories
// @version 1
// @grant none
// @match https://www.news.com.au/
// @match https://www.news.com.au/*
// @noframes
// ==/UserScript==
@DavidRogersDev
DavidRogersDev / ColorsController.cs
Created April 18, 2020 08:21
Gist for Medium Article - ColorsController
public class ColorsController : KesselRunApiController
{
public ColorsController(
ICurrentUser currentUser,
ILogger logger,
IMediator mediator)
: base(currentUser, logger, mediator){}
public async Task<IActionResult> GetColors()
{
@DavidRogersDev
DavidRogersDev / GetColorsQueryHandler.cs
Last active April 18, 2020 23:22
Gist for Medium Article - GetColorsQueryHandler
public class GetColorsQueryHandler : IRequestHandler<GetColorsQuery, Either<IEnumerable<ColorPayloadDto>, ValidationResult>>
{
private readonly IColorsService _colorsService;
public GetColorsQueryHandler(IColorsService colorsService)
{
_colorsService = colorsService;
}
public async Task<Either<IEnumerable<ColorPayloadDto>, ValidationResult>> Handle(GetColorsQuery request, CancellationToken cancellationToken)
@DavidRogersDev
DavidRogersDev / Either.cs
Created April 18, 2020 06:11
Gist for Medium Article - Either
/*
* This class was taken from this GitHub repo https://github.com/mikhailshilkov/mikhailio-samples
* under an MIT licence.
*/
/// <summary>
/// Functional data data to represent a discriminated
/// union of two possible types.
/// </summary>
/// <typeparam name="TL">Type of "Left" item.</typeparam>
@DavidRogersDev
DavidRogersDev / GetColorsQuery.cs
Last active April 18, 2020 23:21
Gist for Medium Article - GetColorsQuery
public class GetColorsQuery : IRequest<Either<IEnumerable<ColorPayloadDto>, ValidationResult>>
{
// no properties necessary, as the query will return all Colors.
}
@DavidRogersDev
DavidRogersDev / ColorsService.cs
Last active April 18, 2020 23:19
Gist for Medium Article - ColorsService
public class ColorsService : ApplicationService, IColorsService
{
private readonly IValidator<IEnumerable<ColorPayloadDto>> _colorValidator;
public ColorsService(IValidator<IEnumerable<ColorPayloadDto>> colorValidator, IMapper mapper)
: base(mapper)
{
_colorValidator = colorValidator;
}
@DavidRogersDev
DavidRogersDev / ColorCollectionValidator.cs
Last active April 18, 2020 05:55
Gist for Medium Article - ColorCollectionValidator
public class ColorCollectionValidator : AbstractValidator<IEnumerable<ColorPayloadDto>>
{
public ColorCollectionValidator()
{
// This is a bit of a silly validator.
// Normally a collection would be a property on a DTO.
// But it interesting to see that you can do this with FluentValidation at all.
RuleFor(c => c)
.Must(c => c.Any())
.WithMessage("The color collection must contain at least one colour.");
@DavidRogersDev
DavidRogersDev / RegisterNewUserValidator.cs
Last active April 6, 2020 05:43
Gits for Medium Article - RegisterNewUserValidator
public class RegisterNewUserValidator : AbstractValidator<RegisterNewUserCommand>
{
public RegisterNewUserValidator()
{
RuleFor(u => u.Dto.UserName)
.MustAsync(NotContainUserAlready)
.WithMessage((u,userName) => $"{userName} {Constants.Validation.PartMessages.UserAlreadyExists}");
}
async Task<bool> NotContainUserAlready(RegisterNewUserCommand dto, string userName, CancellationToken cancellation = new CancellationToken())
@DavidRogersDev
DavidRogersDev / BusinessValidationPipeline.cs
Last active June 27, 2022 22:28
Gists for Medium Article - BusinessValidationPipeline
public class BusinessValidationPipeline<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TResponse : class
where TRequest : IValidateable
{
private readonly IValidator<TRequest> _compositeValidator;
private readonly ILogger<TRequest> _logger;
private readonly ICurrentUser _currentUser;
private readonly IActionContextAccessor _actionContextAccessor;
public BusinessValidationPipeline(