Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
vkhorikov / Promote.cs
Created September 6, 2016 00:37
Promote method with a possibility to fail while saving data to the DB
[HttpPost]
[Route("customers/{id}/promotion")]
public HttpResponseMessage Promote(long id)
{
return _customerRepository.GetById(id)
.ToResult("Customer with such Id is not found: " + id)
.Ensure(customer => customer.CanBePromoted(), "The customer has the highest status possible")
.OnSuccess(customer => customer.Promote())
.OnSuccess(customer => _unitOfWork.Flush().Map(() => customer))
.OnSuccess(customer => _emailGateway.SendPromotionNotification(customer.PrimaryEmail, customer.Status)
[Fact]
public void SnackMachineRepository_NewSnackMachine_IsSaved()
{
SessionFactory.Init(@"Server=VLADIMIR-PC\SQL2014;Database=DddInPractice;Trusted_Connection=true");
var repository = new SnackMachineRepository();
var snackMachine = new SnackMachine();
snackMachine.LoadSnacks(1, new SnackPile(Chocolate, 1, 1m));
snackMachine.LoadSnacks(2, new SnackPile(Chocolate, 1, 1m));
snackMachine.LoadSnacks(3, new SnackPile(Chocolate, 1, 1m));
@vkhorikov
vkhorikov / 1_Before.cs
Last active April 20, 2020 17:08
Representing data as code
public class Industry : Entity
{
public const string CarsIndustry = "Cars";
public const string PharmacyIndustry = "Pharmacy";
public const string MediaIndustry = "Media";
public string Name { get; private set; }
}
public class Customer : Entity
public class UserBuilder
{
private readonly string _firstName;
private readonly string _lastName;
public UserBuilder()
: this(Guid.NewGuid().ToString(), Guid.NewGuid().ToString())
{
}
@vkhorikov
vkhorikov / UserTests.cs
Created January 21, 2016 02:35
Arrange Section reuse example
public class UserTests
{
[Fact]
public void Should_fire_event_after_provisioning()
{
// Arrange
Organization organization = CreateOrganization();
User user = CreateUser(organization);
Subscription subscription = CreateSubscription(Package.Streamer, organization);
[Fact]
public void Parser_parses_xml_in_correct_order()
{
// Arrange : input values
string xml = "<outer><inner /></outer>";
var parser = new Parser();
// Arrange : record expectations
var mocks = new MockRepository();
IHandler handler = mocks.CreateMock<IHandler>();
[Fact]
public void Amount_shows_order_amount()
{
// Arrange
var order = new Order();
order.AddLine(10, new Product());
order.AddLine(20, new Product());
// Act
decimal amount = order.Amount;
public class Order
{
private readonly List<OrderLine> _lines;
public Order()
{
_lines = new List<OrderLine>();
}
public decimal Amount
public abstract class Error
{
public abstract ErrorType Type { get; }
}
public class SimpleError : Error
{
public override ErrorType Type { get { return ErrorType.Simple; } }
public string Message { get; private set; }
public void Create(string name, string email, string billingInfo)
{
Result<CustomerName> nameResult = CustomerName.Create(name);
Result<Email> emailResult = Email.Create(email);
Result<BillingInfo> billingInfoResult = BillingInfo.Create(billingInfo);
return Result.Combine(nameResult, emailResult, billingInfoResult)
.OnSuccess(() => _paymentGateway.ChargeCommission(billingInfoResult.Value));
/* Other OnSuccess, OnFailure, OnBoth methods */
}