Last active
April 29, 2023 09:41
-
-
Save kleinron/40a23906165ffa5cf2d2c0ad8fc6d173 to your computer and use it in GitHub Desktop.
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 CustomersFetcher | |
{ | |
public IEnumerable<CustomerDetails> GetSleepingCustomers() | |
{ | |
List<CustomerDetails> result = new List<CustomerDetails>(); | |
var connectionString = "..."; | |
using (var connection = new OleDbConnection(connectionString)) | |
{ | |
connection.Open(); | |
var command = new OleDbCommand("select * from Customers where LastPurchasedAt < ?", connection); | |
var todayMinus30 = DateTime.Today.AddDays(-30); | |
var oleDbParameter = new OleDbParameter { OleDbType = OleDbType.Date, Value = todayMinus30 }; | |
command.Parameters.Add(oleDbParameter); | |
using (command) | |
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection)) | |
{ | |
while (reader.Read()) | |
{ | |
var customerDetails = new CustomerDetails | |
{ | |
FirstName = (string) reader["FirstName"], | |
LastName = (string) reader["LastName"], | |
EmailAddress = (string) reader["EmailAddress"] | |
}; | |
result.Add(customerDetails); | |
} | |
} | |
} | |
return result; | |
} | |
} | |
public class CustomerDetails | |
{ | |
public string FirstName; | |
public string LastName; | |
public string EmailAddress; | |
} |
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 CustomersReminder | |
{ | |
public void Remind() | |
{ | |
var connectionString = "..."; | |
using (var connection = new OleDbConnection(connectionString)) | |
{ | |
connection.Open(); | |
var command = new OleDbCommand("select * from Customers where LastPurchasedAt < ?", connection); | |
var todayMinus30 = DateTime.Today.AddDays(-30); | |
var oleDbParameter = new OleDbParameter {OleDbType = OleDbType.Date, Value = todayMinus30}; | |
command.Parameters.Add(oleDbParameter); | |
using (command) | |
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection)) | |
{ | |
while (reader.Read()) | |
{ | |
var firstName = (string) reader["FirstName"]; | |
var lastName = (string) reader["LastName"]; | |
var email = (string)reader["EmailAddress"]; | |
SendMailTo(firstName, lastName, email); | |
} | |
} | |
} | |
} | |
private void SendMailTo(string firstName, string lastName, string email) | |
{ | |
var mailMessage = new MailMessage("[email protected]", email); | |
mailMessage.Subject = "It's been a while..."; | |
mailMessage.Body = string.Format("Hi there, {0} {1}, please try our new sales!", firstName, lastName); | |
var smtpClient = new SmtpClient(); | |
try | |
{ | |
smtpClient.Send(mailMessage); | |
} | |
catch (Exception ex) | |
{ | |
// do something here, like logging or alerting.. | |
} | |
} | |
} |
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 CustomersReminder | |
{ | |
public void Remind() | |
{ | |
var customersFetcher = new CustomersFetcher(); | |
var emailSender = new EmailSender(); | |
foreach (var customer in customersFetcher.GetSleepingCustomers()) | |
{ | |
emailSender.SendRemindMessage(customer.FirstName, customer.LastName, customer.EmailAddress); | |
} | |
} | |
} |
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 CustomersReminder | |
{ | |
public void Remind(CustomersFetcher customersFetcher, EmailSender emailSender) | |
{ | |
foreach (var customer in customersFetcher.GetSleepingCustomers()) | |
{ | |
emailSender.SendRemindMessage(customer.FirstName, customer.LastName, customer.EmailAddress); | |
} | |
} | |
} |
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 CustomersFetcher | |
{ | |
public IEnumerable<CustomerDetails> GetSleepingCustomers() | |
{ | |
List<CustomerDetails> result = new List<CustomerDetails>(); | |
var connectionString = "..."; | |
using (var connection = new OleDbConnection(connectionString)) | |
{ | |
connection.Open(); | |
var command = new OleDbCommand("select * from Customers where LastPurchasedAt < ?", connection); | |
var todayMinus30 = DateTime.Today.AddDays(-30); | |
var oleDbParameter = new OleDbParameter { OleDbType = OleDbType.Date, Value = todayMinus30 }; | |
command.Parameters.Add(oleDbParameter); | |
using (command) | |
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection)) | |
{ | |
while (reader.Read()) | |
{ | |
var customerDetails = new CustomerDetails | |
{ | |
FirstName = (string) reader["FirstName"], | |
LastName = (string) reader["LastName"], | |
EmailAddress = (string) reader["EmailAddress"] | |
}; | |
result.Add(customerDetails); | |
} | |
} | |
} | |
return result; | |
} | |
} | |
public class CustomerDetails | |
{ | |
public string FirstName; | |
public string LastName; | |
public string EmailAddress; | |
} |
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 CustomersReminder | |
{ | |
public void Remind() | |
{ | |
var connectionString = "..."; | |
using (var connection = new OleDbConnection(connectionString)) | |
{ | |
connection.Open(); | |
var command = new OleDbCommand("select * from Customers where LastPurchasedAt < ?", connection); | |
var todayMinus30 = DateTime.Today.AddDays(-30); | |
var oleDbParameter = new OleDbParameter {OleDbType = OleDbType.Date, Value = todayMinus30}; | |
command.Parameters.Add(oleDbParameter); | |
using (command) | |
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection)) | |
{ | |
while (reader.Read()) | |
{ | |
var firstName = (string) reader["FirstName"]; | |
var lastName = (string) reader["LastName"]; | |
var email = (string)reader["EmailAddress"]; | |
SendMailTo(firstName, lastName, email); | |
} | |
} | |
} | |
} | |
private void SendMailTo(string firstName, string lastName, string email) | |
{ | |
var mailMessage = new MailMessage("[email protected]", email); | |
mailMessage.Subject = "It's been a while..."; | |
mailMessage.Body = string.Format("Hi there, {0} {1}, please try our new sales!", firstName, lastName); | |
var smtpClient = new SmtpClient(); | |
try | |
{ | |
smtpClient.Send(mailMessage); | |
} | |
catch (Exception ex) | |
{ | |
// do something here, like logging or alerting.. | |
} | |
} | |
} |
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 CustomersReminder | |
{ | |
public void Remind() | |
{ | |
var customersFetcher = new CustomersFetcher(); | |
var emailSender = new EmailSender(); | |
foreach (var customer in customersFetcher.GetSleepingCustomers()) | |
{ | |
emailSender.SendRemindMessage(customer.FirstName, customer.LastName, customer.EmailAddress); | |
} | |
} | |
} |
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 CustomersReminder | |
{ | |
public void Remind(CustomersFetcher customersFetcher, EmailSender emailSender) | |
{ | |
foreach (var customer in customersFetcher.GetSleepingCustomers()) | |
{ | |
emailSender.SendRemindMessage(customer.FirstName, customer.LastName, customer.EmailAddress); | |
} | |
} | |
} |
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 customersReminder = new CustomersReminder(); | |
customersReminder.Remind(new CustomersFetcher(), new EmailSender()); |
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 ICustomersFetcher | |
{ | |
IEnumerable<CustomerDetails> GetSleepingCustomers(); | |
} | |
public interface IEmailSender | |
{ | |
void SendRemindMessage(string firstName, string lastName, string email); | |
} |
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 CustomersFetcher : ICustomersFetcher | |
{ ... } |
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 void Remind(ICustomersFetcher customersFetcher, IEmailSender emailSender) | |
{ ... } |
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 ImageProcessing | |
{ | |
public void ProcessImage(Uri imageUrl, IDownloader downloader, IImageResizer imageResizer, IFileSystem fileSystem) | |
{ | |
byte[] imageBytes = downloader.Get(imageUrl); | |
Image bitmap = Image.FromStream(new MemoryStream(imageBytes)); | |
Image afterResize = imageResizer.ResizeToMax(bitmap, 100); | |
fileSystem.StoreImage(afterResize); | |
} | |
} | |
public interface IFileSystem | |
{ | |
void StoreImage(Image image); | |
} | |
public interface IImageResizer | |
{ | |
Image ResizeToMax(Image bitmap, int maxWidthOrHeight); | |
} | |
public interface IDownloader | |
{ | |
byte[] Get(Uri imageUrl); | |
} |
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 Rectangle : IShape | |
{ | |
private readonly double a; | |
private readonly double b; | |
public Rectangle(double a, double b) | |
{ | |
this.a = a; | |
this.b = b; | |
} | |
public double GetArea() | |
{ | |
return a*b; | |
} | |
} | |
public class Circle : IShape | |
{ | |
private readonly double radius; | |
public Circle(double radius) | |
{ | |
this.radius = radius; | |
} | |
public double GetArea() | |
{ | |
const double pi = Math.PI; // ? | |
return pi * radius * radius; | |
} | |
} |
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 static void DoSomething(IShape shape) | |
{ | |
Console.WriteLine("the area is {0:00}", shape.GetArea()); | |
} |
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 IShape | |
{ | |
double GetArea(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment