Skip to content

Instantly share code, notes, and snippets.

public class EntityController : ApiController
{
public SomeDto Get()
{
//return some DTO
}
}
public class CustomerValidator: AbstractValidator
{
public CustomerValidator()
{
RuleFor(customer => customer.Surname).NotEmpty();
RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name");
RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount);
RuleFor(customer => customer.Address).Length(20, 250);
RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
}
public IQueryable<CustomerPurchases> GetPurchases(int customerID)
{
return _purchaseService.GetPurchases(customerID);
}
@karbyninc
karbyninc / TypeConverters.cs
Last active February 11, 2016 15:21
Using TypeConverters in Web API
public class Color
{
public double Red { get; set; }
public double Green { get; set; }
public double Blue { get; set; }
public Color()
{ }
}
@karbyninc
karbyninc / MessageHandlers.cs
Last active February 11, 2016 15:31
Message Handlers in Web API
public class APIKeyHandler : DelegatingHandler
{
private const string REQUEST_HEADER = "X-KARBYN-APIKEY";
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
bool isValidAPIKey = false;
//Validate that the api key exists, and if so, validate
if (request.Headers.Contains(REQUEST_HEADER))
@karbyninc
karbyninc / ExceptionHandlers.cs
Last active July 4, 2018 10:38
Handling Errors in Web API Using Exception Filters and Exception Handlers
public override void OnException(HttpActionExecutedContext actionExecutedContext)
@karbyninc
karbyninc / StrategyPattern.cs
Last active July 4, 2018 10:34
The Strategy Pattern
namespace StrategyPattern.Contracts
{
public interface IDiscountStrategy
{
decimal ApplyDiscount(decimal price);
}
}
@karbyninc
karbyninc / UpdatingDatasources.cs
Last active February 11, 2016 17:08
Sitecore Tip: Updating Datasources After Upgrading to Sitecore 7
///
/// Updates the datasource for a rendering from an item path to using the
/// Sitecore ID for the item. The benefit is that this will allow datasources
/// to be able to be freely moved from one area of the content tree to another
/// while enabling the rendering to still function as expected.
///
///The Sitecore.Data.Items.Item to update the datasources for.
public void UpdateDataSourcePathToId(Sitecore.Data.Items.Item itemToProcess)
{
if (null != itemToProcess)
@karbyninc
karbyninc / CustomConditions.cs
Last active February 11, 2016 18:10
Sitecore Development Insight - Custom Conditions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;
using Sitecore.Data.Items;
using Sitecore.Analytics;
using Karbyn.SC.Core.Extensions;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Protocols;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Notifications;
using Microsoft.Owin.Security.OpenIdConnect;
using Owin;
using Sitecore.Owin.Authentication.Configuration;
using Sitecore.Owin.Authentication.Pipelines.IdentityProviders;