This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var products = new List<Product> | |
{ | |
new() | |
{ | |
Title = "MacBook Pro 14 inch", | |
Description = "It's now even more capable, thanks to the new M3 chip." | |
}, | |
new() | |
{ | |
Title = "MacBook Pro 16 inch", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CsvProductsExporterAdapter : IProductsExporter | |
{ | |
private CsvHelper CsvHelper { get; set; } | |
public CsvProductsExporterAdapter(CsvHelper csvHelper) | |
{ | |
CsvHelper = csvHelper; | |
} | |
public string Export(List<Product> data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CsvHelper | |
{ | |
private char Delimiter { get; set; } | |
public CsvHelper(char delimiter) | |
{ | |
Delimiter = delimiter; | |
} | |
public string GenerateCsv(List<object> rows) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JsonProductsExporter : IProductsExporter | |
{ | |
public string Export(List<Product> products) | |
{ | |
return JsonSerializer.Serialize(products); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class XmlProductsExporter : IProductsExporter | |
{ | |
public string Export(List<Product> products) | |
{ | |
var xmlSerializer = new XmlSerializer(products.GetType()); | |
using var stringWriter = new StringWriter(); | |
using var xmlWriter = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented }; | |
xmlSerializer.Serialize(xmlWriter, products); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IProductsExporter | |
{ | |
string Export(List<Product> products); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
modelBuilder | |
.Entity<Shipment>() | |
.Property(e => e.State) | |
.HasConversion( | |
s => s.ToString(), | |
s => s switch { | |
nameof(OrderPlacedState) => new OrderPlacedState(), | |
nameof(InTransitState) => new InTransitState(), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var shipment = new Shipment(Guid.NewGuid(), "RU", "Istanbul") | |
shipment.ToInTransit(); | |
shipment.ToInCustoms(); | |
shipment.ToInCustomsCleared(); | |
shipment.ToOutForDelivery(); | |
shipment.ToNotDelivered(); | |
shipment.ToDelivered(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Shipment | |
{ | |
public Guid OrderId { get; private set; } | |
public string FromCountry { get; private set; } | |
public string To { get; private set; } | |
public ShipmentState State { get; private set; } | |
public int DeliveryAttempts { get; internal set; } | |
public Shipment(Guid orderId, string fromCountry, string to) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NotDeliveredState : ShipmentState | |
{ | |
public override string ToString() => nameof(NotDeliveredState); | |
} |
NewerOlder