Skip to content

Instantly share code, notes, and snippets.

View NDiiong's full-sized avatar
😀
Hi there!

duong.nb NDiiong

😀
Hi there!
View GitHub Profile
@NDiiong
NDiiong / ObjectValidator.cs
Created June 24, 2020 16:04 — forked from mainettis/ObjectValidator.cs
Simple Generic Object Validation as based on solution found here: https://codereview.stackexchange.com/questions/178106/simple-object-validator
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq.Expressions;
// I loved this when I found here: https://codereview.stackexchange.com/questions/178106/simple-object-validator
namespace Tools.Validate.Objects // could be something else
{
public class Validation
{
class Program
{
static void Main(string[] args)
{
var model = new Test() { };
var validator = new Validator<Test>();
var result = new Validator<Test>()
.AddRule(t => t.Name, new ValidatorNameRule())
.Validate(model);
public interface IValidator<in T> {
}
public class CompositeValidator<T> : IValidator<T> {
public CompositeValidator(params IValidator<T>[] validators) {
}
}
@NDiiong
NDiiong / IValidator.cs
Created June 24, 2020 16:41 — forked from ardacetinkaya/IValidator.cs
An easy Validation pattern implementation for any kind of application...
//Generic interface for Validators
public interface IValidator<T>
{
IEnumerable<ValidationResult> Validate(T businessEntity);
}
@NDiiong
NDiiong / Validation.cs
Created June 24, 2020 16:44 — forked from saberone/Validation.cs
A Quick hack for a simple validation using yield. The idea is to create a very clean api/interface. Some Unit tests are included, but they are far from thorough.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Configuration;
using System.Web.Security;
namespace Validation
{
public interface IValidator <T>
{
public class ValidatorHandler<TRequest, TResponse>
: IRequestHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly IRequestHandler<TRequest, TResponse> _inner;
private readonly IValidator<TRequest> _validator;
public ValidatorHandler(IRequestHandler<TRequest, TResponse> inner,
public class RegsiterCommandValidator : IValidator<RegisterCommand>
{
private readonly IValidationResult _validationResult;
private readonly IDbContext _context;
public RegsiterCommandValidator(IValidationResult validationResult, IDbContext context)
{
_validationResult = validationResult;
_context = context;
}
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Text;
using System.Threading.Tasks;
namespace WebApp
{
public class ShowAllServicesMiddleware
@NDiiong
NDiiong / ValidateXmlWithXsd.cs
Created July 29, 2020 12:01 — forked from automatonic/ValidateXmlWithXsd.cs
Programmatically validate an XML document with an XSD (XML Schema)
//License: MIT
//using System.Xml;
//using System.Xml.Schema;
public static List<ValidationEventArgs> ValidateXmlWithXsd(string xmlPath, string xsdPath)
{
XmlSchemaSet s = new XmlSchemaSet();
var args = new List<ValidationEventArgs>();
using (XmlReader reader = XmlReader.Create(xsdPath))
{
//These validations will be from reading the xsd itself
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
namespace XSD_fun
{
class Program
{