Skip to content

Instantly share code, notes, and snippets.

@btodts
btodts / Foo.csproj
Last active August 1, 2018 15:00
config.json as an Embedded Resource
<ItemGroup>
<EmbeddedResource Include="Configuration\config.json" />
</ItemGroup>
@btodts
btodts / Configuration.cs
Last active September 17, 2018 08:26
Configuration.cs
namespace Foo.Configuration
{
public interface IConfiguration
{
string ApiBaseAddress { get; set; }
}
public class Configuration : IConfiguration
{
[JsonConstructor]
@btodts
btodts / ConfigurationModule.cs
Last active August 1, 2018 19:02
The autofac module that contains configuration
using System.IO;
using System.Reflection;
using Autofac;
using Newtonsoft.Json;
using Foo.Configuration;
using Module = Autofac.Module;
namespace Foo.Modules
{
public class ConfigurationModule : Module
Animated.loop(
Animated.sequence([
Animated.delay(3000),
Animated.timing(this.state.width, {
toValue: 400,
duration: 2000
})
]),
{
iterations: 10
animateWidth() {
Animated.sequence([
Animated.delay(3000),
Animated.timing(this.state.width, {
toValue: 400,
duration: 2000
})
]).start(() => {
// Logic whenever an iteration finishes...
this.animateWidth()
[HttpPost]
public async Task<IActionResult> Post(Guid id, string name)
{
Guard.GuidNotEmpty(id, nameof(id));
Guard.StringNotNullOrEmpty(name, nameof(name));
// handle the request
}
public static class Guard
{
public static void GuidNotEmpty(Guid argument, string argumentName)
{
if (argument == Guid.Empty)
throw new ArgumentException("Guid should not be empty.", argumentName);
}
public static void StringNotNullOrEmpty(string argument, string argumentName)
{
public sealed class GuardException : Exception
{
public string ArgumentName { get; }
public GuardException(string argumentName, string message)
: base(message)
{
ArgumentName = argumentName;
}
}
public static class Guard
{
public static void GuidNotEmpty(Guid argument, string argumentName)
{
if (argument == Guid.Empty)
throw new GuardException(argumentName, "Guid should not be empty.");
}
public static void StringNotNullOrEmpty(string argument, string argumentName)
{
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
var exceptionHandlingFeature = context.Features.Get<IExceptionHandlerPathFeature>();
if (exceptionHandlingFeature?.Error is GuardException ex)