- Parameter Object
- Application Service
- Domain Service Pure
- Domain Service with ApplicationLayer Service
- seperation of concerns
- testability
- Dependency Injection not always necessary:
- Service is pure
- Service can be made static
Created
February 26, 2020 18:29
-
-
Save rommsen/4c6d9153255264b68eb53880b3292fbc to your computer and use it in GitHub Desktop.
Practical DDD Step 4
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
namespace ddd_patterns.Step6 | |
{ | |
public interface IPaymentService | |
{ | |
public bool Pay(decimal amount, decimal vat); | |
} | |
} |
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
using System; | |
namespace ddd_patterns.Step6 | |
{ | |
public interface ITicketRepository | |
{ | |
public int HowManyAreAvailable(); | |
public Guid Book(Guid attendee); | |
public void Cancel(Guid tId); | |
void Payed(Guid ticket); | |
DateTime BookingDateFor(Guid ticket); | |
} | |
} |
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
namespace ddd_patterns.Step6 | |
{ | |
public interface IVoucherService | |
{ | |
public int DiscountFor(string voucher); | |
} | |
} |
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
using System; | |
namespace ddd_patterns.Step6 | |
{ | |
public class OnlineTickets | |
{ | |
private readonly ITicketRepository _tickets; | |
private readonly IVoucherService _vouchers; | |
private readonly IPaymentService _paymentService; | |
private readonly DateTime _earlyBirdFrom; | |
private readonly DateTime _earlyBirdUntil; | |
private readonly DateTime _blindBirdFrom; | |
private readonly DateTime _blindBirdUntil; | |
private readonly decimal _blindBirdPrice; | |
private readonly decimal _earlyBirdPrice; | |
private readonly decimal _normalBirdPrice; | |
public OnlineTickets( | |
ITicketRepository tickets, | |
IVoucherService vouchers, | |
IPaymentService paymentService, | |
DateTime earlyBirdFrom, | |
DateTime earlyBirdUntil, | |
DateTime blindBirdFrom, | |
DateTime blindBirdUntil, | |
decimal blindBirdPrice, | |
decimal earlyBirdPrice, | |
decimal normalBirdPrice) | |
{ | |
_tickets = tickets; | |
_vouchers = vouchers; | |
_paymentService = paymentService; | |
_earlyBirdFrom = earlyBirdFrom; | |
_earlyBirdUntil = earlyBirdUntil; | |
_blindBirdFrom = blindBirdFrom; | |
_blindBirdUntil = blindBirdUntil; | |
_blindBirdPrice = blindBirdPrice; | |
_earlyBirdPrice = earlyBirdPrice; | |
_normalBirdPrice = normalBirdPrice; | |
} | |
public Guid BookFor(Guid attendee) | |
{ | |
var availableTickets = _tickets.HowManyAreAvailable(); | |
if (availableTickets <= 0) | |
{ | |
throw new Exception("No Ticket available"); | |
} | |
return _tickets.Book(attendee); | |
} | |
public void Cancel(Guid ticket) | |
{ | |
_tickets.Cancel(ticket); | |
} | |
public bool PayFor(Guid ticket, string voucher) | |
{ | |
var bookingDate = _tickets.BookingDateFor(ticket); | |
var amount = _normalBirdPrice; | |
if (bookingDate >= _blindBirdFrom && bookingDate <= _blindBirdUntil) | |
{ | |
amount = _blindBirdPrice; | |
} | |
else if (bookingDate >= _earlyBirdFrom && bookingDate <= _earlyBirdUntil) | |
{ | |
amount = _earlyBirdPrice; | |
} | |
amount -= _vouchers.DiscountFor(voucher); | |
var vat = amount * 0.19m; | |
if (!_paymentService.Pay(amount, vat)) | |
{ | |
return false; | |
} | |
_tickets.Payed(ticket); | |
return true; | |
} | |
} | |
} |
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
using System; | |
using System.Data.SqlClient; | |
namespace ddd_patterns.Step6 | |
{ | |
public class SqlTicketRepository : ITicketRepository | |
{ | |
private readonly string _connectionString; | |
public SqlTicketRepository(string connectionString) | |
{ | |
_connectionString = connectionString; | |
} | |
public int HowManyAreAvailable() | |
{ | |
var availableTickets = 0; | |
const string sql1 = @"SELECT get number of available tickets from db"; | |
using var connection = new SqlConnection(_connectionString); | |
var command = new SqlCommand(sql1, connection); | |
connection.Open(); | |
var reader = command.ExecuteReader(); | |
while (reader.Read()) | |
{ | |
availableTickets = reader.GetInt32(0); | |
} | |
reader.Close(); | |
return availableTickets; | |
} | |
public Guid Book(Guid attendee) | |
{ | |
using var connection = new SqlConnection(_connectionString); | |
var tId = Guid.NewGuid(); | |
const string sql2 = @"Insert into tickets... tId = @tId, aId = @aId"; | |
var command2 = new SqlCommand(sql2, connection); | |
command2.Parameters.AddWithValue("@tId", tId); | |
command2.Parameters.AddWithValue("@tad", attendee); | |
command2.ExecuteNonQuery(); | |
return tId; | |
} | |
public void Cancel(Guid tId) | |
{ | |
using var connection = new SqlConnection(_connectionString); | |
const string sql = @"Delete FROM tickets WHERE tId = @tId"; | |
var command = new SqlCommand(sql, connection); | |
command.Parameters.AddWithValue("@tId", tId); | |
command.ExecuteNonQuery(); | |
} | |
public void Payed(Guid ticket) | |
{ | |
throw new NotImplementedException(); | |
} | |
public DateTime BookingDateFor(Guid ticket) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment