-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLR_Good.cs
56 lines (55 loc) · 1.31 KB
/
SLR_Good.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.ComponentModel.DataAnnotations;
using System.Net.Mail;
namespace SOLID_Principles.SLR
{
/// <summary>
/// mockup class for DbContext class
/// lazy to import and using declaration
/// </summary>
public class DbContext
{
public void Save(User user) { }
}
}
namespace SOLID_Principles.SLR.Good
{
public class UserService
{
EmailService _emailService;
DbContext _dbContext;
public UserService(EmailService aEmailService, DbContext aDbContext)
{
_emailService = aEmailService;
_dbContext = aDbContext;
}
public void Register(string email, string password)
{
if (!_emailService.ValidateEmail(email))
throw new ValidationException("Email is not an email");
var user = new User(email, password);
_dbContext.Save(user);
_emailService.SendEmail(new MailMessage("myname@mydomain.com", email) { Subject = "Hi. How are you!" });
}
}
public class EmailService
{
SmtpClient _smtpClient;
public EmailService(SmtpClient aSmtpClient)
{
_smtpClient = aSmtpClient;
}
/// <summary>
/// virtual in C# = overridable in VB.NET
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
public virtual bool ValidateEmail(string email)
{
return email.Contains("@");
}
public void SendEmail(MailMessage message)
{
_smtpClient.Send(message);
}
}
}