Skip to content

Instantly share code, notes, and snippets.

View enisn's full-sized avatar

Enis Necipoglu enisn

View GitHub Profile
@enisn
enisn / CustomTodoController.cs
Created January 15, 2022 13:09
Mastering at Source Generators - Extending TodoController via inheriting
using Awesome.Api.Data;
namespace Awesome.Api.Controllers;
public abstract partial class TodoController
{
}
public class CustomTodoController : TodoController
{
@enisn
enisn / TodoController.cs
Created January 15, 2022 13:00
Mastering at Source Generators - Extending TodoController via partial class
using Microsoft.AspNetCore.Mvc;
namespace Awesome.Api.Controllers;
public partial class TodoController
{
[HttpPut("mark-all-as-completed")]
public async Task MarkAllAsCompletedAsync()
{
var todos = await _repository.GetListAsync();
@enisn
enisn / MongoDbRepository.cs
Created January 15, 2022 12:17
Mastering at Source Generators - MongoDbRepository anatomy
using Humanizer;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
namespace Awesome.Api.Data;
public class MongoDbRepository<TModel> : IRepository<TModel>
where TModel : class, IIdentifiable
{
@enisn
enisn / IRepository.cs
Last active January 15, 2022 12:14
Mastering at Source Generatos - IRepository anatomy
namespace Awesome.Api.Data;
public interface IRepository<TModel>
where TModel : class, IIdentifiable
{
Task<List<TModel>> GetListAsync();
Task<TModel> GetSingleAsync(Guid id);
Task InsertAsync(TModel model);
@enisn
enisn / ServiceGenerator.cs
Last active January 15, 2022 11:32
Mastering at Source Generators - ServiceGenerator Final State
using Awesome.Generators.Templates;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using System.Text;
namespace Awesome.Generators;
[Generator]
public class ServiceGenerator : ISourceGenerator
@enisn
enisn / AttributeSyntaxReceiver.cs
Created January 15, 2022 10:47
Mastering at Source Generators - AttributeSyntaxReceiver
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Awesome.Generators;
public class AttributeSyntaxReceiver<TAttribute> : ISyntaxReceiver
where TAttribute : Attribute
{
public IList<ClassDeclarationSyntax> Classes { get; } = new List<ClassDeclarationSyntax>();
@enisn
enisn / AttributeSyntaxReceiver.cs
Created January 14, 2022 19:07
A syntax receiver for CodeAnalysis applications that handles attributes
using DotNurse.CodeAnalysis.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
namespace DotNurse.CodeAnalysis;
public class AttributeSyntaxReceiver<TAttribute> : ISyntaxReceiver
@enisn
enisn / MyAwesomeApp.csproj
Created December 8, 2021 14:43
Making MyAwesomeApp a dotnet tool.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
<PackAsTool>true</PackAsTool>
<ToolCommandName>myawesomeapp</ToolCommandName>
</PropertyGroup>
</Project>
@enisn
enisn / WeatherForecast.cs
Last active April 10, 2021 10:42
AutoFilterer.Generators | Step 2 | WeatherForecast.cs
[HttpGet]
public IEnumerable<WeatherForecast> Get([FromQuery]WeatherForecastFilter filter)
{
var rng = new Random();
// Change range to 100 from 5 to get more reasonable results.
return Enumerable.Range(1, 100).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
@enisn
enisn / WeatherForecast.cs
Created April 9, 2021 21:20
AutoFilterer.Generators | Step 1 | WeatherForecast.cs
using System;
namespace MyAwesomeApp
{
[GenerateAutoFilter] // <-- Add this attribute
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }