Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
danielplawgo / CsvFormatter1.cs
Created December 5, 2018 07:07
Jak zastąpić rozbudowanego switch w aplikacji
public class CsvFormatter : ICsvFormatter
{
public void Export(Report report)
{
Console.WriteLine($"CsvFormatter.Export: {report.Name}");
}
}
public interface ICsvFormatter
{
@danielplawgo
danielplawgo / BaseTest.cs
Created December 4, 2018 05:27
FluentAssertions.Mvc - assercie dla ASP.NET MVC
public class BaseTest
{
protected Mock<IProductLogic> Logic { get; set; }
protected Mock<IMapper> Mapper { get; set; }
protected virtual ProductsController Create()
{
Logic = new Mock<IProductLogic>();
@danielplawgo
danielplawgo / BaseTests.cs
Created November 27, 2018 05:53
Effort - testy Entity Framework
public class BaseTests : RepositoryTests
{
protected ProductRepository Create(string path = null)
{
var context = CreateContext(path);
return new ProductRepository(new Lazy<DataContext>(() => context));
}
}
@danielplawgo
danielplawgo / GetByIdTests1.cs
Created November 21, 2018 04:54
Fluent Assertions - przyjemne asserty w testach
public class GetByIdTests : BaseTest
{
protected Mock<IProductRepository> Repository { get; private set; }
protected ProductLogic Create()
{
Repository = new Mock<IProductRepository>();
return new ProductLogic(new Lazy<IProductRepository>(() => Repository.Object));
}
@danielplawgo
danielplawgo / BaseModel.cs
Created November 12, 2018 13:26
EF Plus - aktualizacja wielu obiektów jednym zapytaniem
public class BaseModel
{
public BaseModel()
{
IsActive = true;
}
public int Id { get; set; }
public bool IsActive { get; set; }
@danielplawgo
danielplawgo / Address.cshtml
Created November 5, 2018 05:49
Dlaczego Editor Template jest lepsze niż Partial View do tworzenia formularzy?
@model PartialForms.ViewModels.Shared.AddressViewModel
<div class="form-group">
@Html.LabelFor(model => model.Street, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Street, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Street, "", new { @class = "text-danger" })
</div>
</div>
public static class ControllerExtensions
{
public static string GetUserClaim(this ApiController controller, string claimName)
{
var principal = controller.User as ClaimsPrincipal;
if (principal == null)
{
return null;
}
@danielplawgo
danielplawgo / Product.cs
Last active October 23, 2018 03:49
Swagger - dokumentowanie REST API
/// <summary>
/// The product.
/// </summary>
public class Product
{
/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>
/// The identifier.
@danielplawgo
danielplawgo / AddCategories.sql
Created October 12, 2018 04:05
Migracja schematu bazy danych z DbUp
CREATE TABLE [dbo].[Categories] (
[Id] INT NOT NULL IDENTITY(1,1),
[Name] NVARCHAR(255) NOT NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY ([Id])
)
INSERT INTO dbo.Categories SELECT DISTINCT Category FROM dbo.Products;
ALTER TABLE [dbo].[Products] ADD [CategoryId] INT
[Migration(201810030717)]
public class AddCategory : Migration
{
public override void Up()
{
Create.Table("Categories")
.WithColumn("Id").AsInt32().NotNullable().PrimaryKey().Identity()
.WithColumn("Name").AsString();
Execute.Sql("INSERT INTO dbo.Categories SELECT DISTINCT Category FROM dbo.Products;");