Skip to content

Instantly share code, notes, and snippets.

@kleinron
Last active April 29, 2023 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kleinron/40a23906165ffa5cf2d2c0ad8fc6d173 to your computer and use it in GitHub Desktop.
Save kleinron/40a23906165ffa5cf2d2c0ad8fc6d173 to your computer and use it in GitHub Desktop.
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;
}
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("do-not-reply@canberra-shopping.com", 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..
}
}
}
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);
}
}
}
public class CustomersReminder
{
public void Remind(CustomersFetcher customersFetcher, EmailSender emailSender)
{
foreach (var customer in customersFetcher.GetSleepingCustomers())
{
emailSender.SendRemindMessage(customer.FirstName, customer.LastName, customer.EmailAddress);
}
}
}
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;
}
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("do-not-reply@canberra-shopping.com", 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..
}
}
}
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);
}
}
}
public class CustomersReminder
{
public void Remind(CustomersFetcher customersFetcher, EmailSender emailSender)
{
foreach (var customer in customersFetcher.GetSleepingCustomers())
{
emailSender.SendRemindMessage(customer.FirstName, customer.LastName, customer.EmailAddress);
}
}
}
var customersReminder = new CustomersReminder();
customersReminder.Remind(new CustomersFetcher(), new EmailSender());
public interface ICustomersFetcher
{
IEnumerable<CustomerDetails> GetSleepingCustomers();
}
public interface IEmailSender
{
void SendRemindMessage(string firstName, string lastName, string email);
}
public class CustomersFetcher : ICustomersFetcher
{ ... }
public void Remind(ICustomersFetcher customersFetcher, IEmailSender emailSender)
{ ... }
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);
}
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;
}
}
public static void DoSomething(IShape shape)
{
Console.WriteLine("the area is {0:00}", shape.GetArea());
}
public interface IShape
{
double GetArea();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment