Skip to content

Instantly share code, notes, and snippets.

View thiagomajesk's full-sized avatar
💜
Happily doing some Elixir wizardry

Thiago Majesk Goulart thiagomajesk

💜
Happily doing some Elixir wizardry
  • Brazil
View GitHub Profile
@thiagomajesk
thiagomajesk / Controller.cs
Created February 11, 2017 22:22
View Model Factories in ASP.NET Core with Mediator + IoC Extension
public class FooController : Controller
{
private readonly IViewFactory viewFactory;
public FooController(IViewFactory viewFactory) { this.viewFactory = viewFactory; }
public IActionResult Index()
{
var viewModel = viewFactory.Create<CreateFooViewModel>();
@thiagomajesk
thiagomajesk / CharacterController.cs
Last active May 26, 2017 15:39
ViewModelFactory (Mediator Pattern)
public class CharacterController : Controller
{
/*[...]*/
public IActionResult Profile(int id)
{
/*[...]*/
var viewModel = viewFactory.Build<int, CharacterProfileViewModelBuilder>(id);
return View(viewModel);
}
}
public static class IServiceCollectionExtensions
{
public static IServiceCollection ConnectImplementations<TService>(this IServiceCollection services, Assembly assembly)where TService : class
{
return services.ConnectImplementations(typeof (TService), assembly);
}
public static IServiceCollection ConnectImplementations(this IServiceCollection services, Type serviceType, Assembly assembly)
{
if (!serviceType.IsInterface) throw new ArgumentException($"{nameof(serviceType)} must be an interface");
@thiagomajesk
thiagomajesk / brunch-config.js
Last active August 8, 2017 17:30
ASP.NET Core Brunch Config (Babel + Bootstrap 4 + LESS + Font Awesome)
module.exports = {
paths: {
public: "wwwroot",
watched: ["wwwroot/javascripts", "wwwroot/stylesheets"]
},
npm: {
styles: {
"bootstrap": ["dist/css/bootstrap.css"],
"font-awesome": ["css/font-awesome.css"],
"animate.css": ["animate.css"]
@thiagomajesk
thiagomajesk / ModelStateHelper.cs
Created September 30, 2017 15:07
ASP.NET Core PRG TempData Helpers
public class ModelStateTransferValue
{
public string Key { get; set; }
public string AttemptedValue { get; set; }
public object RawValue { get; set; }
public ICollection<string> ErrorMessages { get; set; } = new List<string>();
}
public static class ModelStateHelper
{
@thiagomajesk
thiagomajesk / AutoMapperModule.cs
Created January 25, 2018 13:05
AutoMapper configuration for AutoFac
public class AutoMapperModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new MapperConfiguration(config => { config.AddProfiles(GetType().Assembly); })).AsSelf().SingleInstance();
builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope();
}
}
@thiagomajesk
thiagomajesk / AutoFacModule.cs
Last active August 30, 2023 16:47
Generic Handlers & Commands for MediatR (+ AutoFac config)
public class GenericHandlersModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<CreateCommandHandler<Foo, CreateFooCommand>>().As<IRequestHandler<CreateFooCommand, bool>>();
builder.RegisterType<DeleteCommandHandler<Foo, DeleteFooCommand>>().As<IRequestHandler<DeleteFooCommand, bool>>();
builder.RegisterType<ListQueryHandler<Foo, ListFooQuery>>().As<IRequestHandler<ListFooQuery, IEnumerable<Foo>>>();
builder.RegisterType<UpdateCommandHandler<Foo, UpdateFooCommand>>().As<IRequestHandler<UpdateFooCommand, bool>>();
}
}
@thiagomajesk
thiagomajesk / application.ts
Last active July 3, 2019 20:24
Knockout & Typescript & Webpack bootstrap code
// Component definition (holds information about template and view model)
class ClickCounterComponent extends Component {
constructor(name: string, viewModel: ViewModelClass) {
super(name, viewModel, template)
}
}
// Actual view model (This separation allows us to reuse view models for components)
class ClickCounterViewModel extends ViewModel {
public count: ko.Observable<number> = ko.observable<number>(0);
@thiagomajesk
thiagomajesk / benchmark.exs
Last active July 7, 2020 18:50
XML parsing benchmark
# Dependencies: {:saxy, "~> 1.2"}, {:sax_map, "~> 0.2"}, {:benchee, "~> 1.0"}
#
# Samples
#
simple = """
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<catalog>
@thiagomajesk
thiagomajesk / counter_cache.ex
Created February 17, 2022 23:59
Elixir's Ecto Counter Cache Manager
defmodule CounterCache do
@moduledoc """
This module provides a way of generating embedded schemas that contain the definition of cached fields.
A module is automatically created for each and contains the definition values passed in the list.
By accessing the generated module, those fields can be queried from the schema that contains them.
# Usage
- Add `use CounterCache` to your module.
- Invoke `counter_cache_field` inside your schema definition: