-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add materials for extended lecture - design patterns
- Loading branch information
1 parent
7d5ea5f
commit c9c0768
Showing
27 changed files
with
671 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Lectures/Extended_02/UserManagement-after/UserManagement/UserManagement.sln
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.27428.2015 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserManagement", "UserManagement\UserManagement.csproj", "{CC18FE27-A64C-4C4E-909A-FDE4B52BE7EC}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{CC18FE27-A64C-4C4E-909A-FDE4B52BE7EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{CC18FE27-A64C-4C4E-909A-FDE4B52BE7EC}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{CC18FE27-A64C-4C4E-909A-FDE4B52BE7EC}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{CC18FE27-A64C-4C4E-909A-FDE4B52BE7EC}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {A78F91F4-F6FA-4CBA-8F85-6091145EE4F7} | ||
EndGlobalSection | ||
EndGlobal |
15 changes: 15 additions & 0 deletions
15
...tended_02/UserManagement-after/UserManagement/UserManagement/Factory/NewsletterFactory.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using UserManagement.Models; | ||
|
||
namespace UserManagement.Factory | ||
{ | ||
public class NewsletterFactory | ||
{ | ||
public Email CreateNewsletter() | ||
{ | ||
string subject = "Máme pro vás jedinečnou nabídku!"; | ||
string body = "Začněte používat návrhové vzory právě teď!"; | ||
|
||
return new Email(subject, body); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Lectures/Extended_02/UserManagement-after/UserManagement/UserManagement/Models/Contact.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using UserManagement.Services; | ||
|
||
namespace UserManagement.Models | ||
{ | ||
public class Contact | ||
{ | ||
public string Firstname { get; set; } | ||
public string Lastname { get; set; } | ||
public string Name => $"{Firstname} {Lastname}"; | ||
public string EmailAddress { get; set; } | ||
|
||
public void SendMail(Email email) | ||
{ | ||
var emailService = new EmailService(); | ||
emailService.SendEmail(EmailAddress, email); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Lectures/Extended_02/UserManagement-after/UserManagement/UserManagement/Models/Email.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace UserManagement.Models | ||
{ | ||
public struct Email | ||
{ | ||
public string Subject { get; } | ||
public string Body { get; } | ||
|
||
public Email(string subject, string body) | ||
{ | ||
Subject = subject; | ||
Body = body; | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Lectures/Extended_02/UserManagement-after/UserManagement/UserManagement/Program.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UserManagement.Factory; | ||
using UserManagement.Models; | ||
|
||
namespace UserManagement | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var contacts = new List<Contact>() | ||
{ | ||
new Contact { Firstname = "Roman", EmailAddress = "[email protected]" }, | ||
new Contact { Firstname = "Tibor", EmailAddress = "[email protected]" }, | ||
new Contact { Firstname = "Martin", EmailAddress = "[email protected]" }, | ||
new Contact { Firstname = "Adam", EmailAddress = "[email protected]" }, | ||
new Contact { Firstname = "Joe", EmailAddress = "[email protected]" }, | ||
new Contact { Firstname = "Jake", EmailAddress = "[email protected]" }, | ||
new Contact { Firstname = "Emily", EmailAddress = "[email protected]" }, | ||
new Contact { Firstname = "Sophia", EmailAddress = "[email protected]" }, | ||
new Contact { Firstname = "Brian", EmailAddress = "[email protected]" }, | ||
new Contact { Firstname = "Bob", EmailAddress = "[email protected]" }, | ||
}; | ||
|
||
var newsletterFactory = new NewsletterFactory(); | ||
var newsletter = newsletterFactory.CreateNewsletter(); | ||
|
||
foreach (var contact in contacts) | ||
{ | ||
contact.SendMail(newsletter); | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...s/Extended_02/UserManagement-after/UserManagement/UserManagement/Services/EmailService.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using UserManagement.Models; | ||
|
||
namespace UserManagement.Services | ||
{ | ||
public class EmailService | ||
{ | ||
public void SendEmail(string to, Email email) | ||
{ | ||
Console.WriteLine($"Send email to: {to}"); | ||
Console.WriteLine($"\t{email.Subject} - {email.Body}"); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ures/Extended_02/UserManagement-after/UserManagement/UserManagement/UserManagement.csproj
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
25 changes: 25 additions & 0 deletions
25
Lectures/Extended_02/UserManagement-before/UserManagement/UserManagement.sln
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.27428.2015 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserManagement", "UserManagement\UserManagement.csproj", "{CC18FE27-A64C-4C4E-909A-FDE4B52BE7EC}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{CC18FE27-A64C-4C4E-909A-FDE4B52BE7EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{CC18FE27-A64C-4C4E-909A-FDE4B52BE7EC}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{CC18FE27-A64C-4C4E-909A-FDE4B52BE7EC}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{CC18FE27-A64C-4C4E-909A-FDE4B52BE7EC}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {A78F91F4-F6FA-4CBA-8F85-6091145EE4F7} | ||
EndGlobalSection | ||
EndGlobal |
16 changes: 16 additions & 0 deletions
16
...nded_02/UserManagement-before/UserManagement/UserManagement/Composite/ContactComponent.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using UserManagement.Models; | ||
using UserManagement.Services; | ||
|
||
namespace UserManagement.Composite | ||
{ | ||
public class ContactComponent : IContactComponent | ||
{ | ||
private readonly IEmailService emailService; | ||
|
||
public string Firstname { get; set; } | ||
public string Lastname { get; set; } | ||
|
||
public string Name => $"{Firstname} {Lastname}"; | ||
public string EmailAddress { get; set; } | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...02/UserManagement-before/UserManagement/UserManagement/Composite/ContactGroupComposite.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UserManagement.Models; | ||
|
||
namespace UserManagement.Composite | ||
{ | ||
public class ContactGroupComposite : IContactComponent | ||
{ | ||
public string Name { get; set; } | ||
public IList<IContactComponent> Members { get; } = new List<IContactComponent>(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ded_02/UserManagement-before/UserManagement/UserManagement/Composite/IContactComponent.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using UserManagement.Models; | ||
|
||
namespace UserManagement.Composite | ||
{ | ||
public interface IContactComponent | ||
{ | ||
string Name { get; } | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ed_02/UserManagement-before/UserManagement/UserManagement/Decorator/LoggedEmailService.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using UserManagement.Models; | ||
using UserManagement.Services; | ||
|
||
namespace UserManagement.Decorator | ||
{ | ||
public class LoggedEmailService : IEmailService | ||
{ | ||
private readonly IEmailService emailServiceImplementation; | ||
|
||
public LoggedEmailService(IEmailService emailServiceImplementation) | ||
{ | ||
this.emailServiceImplementation = emailServiceImplementation; | ||
} | ||
|
||
public void SendEmail(string to, Email email) | ||
{ | ||
var methodName = "SendEmail"; | ||
ConsoleLogMessage(ConsoleColor.Yellow, $"Method: {methodName} start"); | ||
try | ||
{ | ||
emailServiceImplementation.SendEmail(to, email); | ||
ConsoleLogMessage(ConsoleColor.Yellow, $"Method: {methodName} was succeed"); | ||
} | ||
catch (Exception e) | ||
{ | ||
ConsoleLogMessage(ConsoleColor.Red, $"Exception {e} was raised in Method: {methodName}"); | ||
throw; | ||
} | ||
ConsoleLogMessage(ConsoleColor.Yellow, $"Method: {methodName} exit"); | ||
} | ||
|
||
private void ConsoleLogMessage(ConsoleColor color, string message, params object[] args) | ||
{ | ||
var originalColor = Console.ForegroundColor; | ||
Console.ForegroundColor = color; | ||
Console.WriteLine(message, args); | ||
Console.ForegroundColor = originalColor; | ||
} | ||
|
||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ed_02/UserManagement-before/UserManagement/UserManagement/Decorator/LoggingInterceptor.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using Castle.DynamicProxy; | ||
|
||
namespace UserManagement.Decorator | ||
{ | ||
public class LoggingInterceptor : IInterceptor | ||
{ | ||
public void Intercept(IInvocation invocation) | ||
{ | ||
var methodName = invocation.Method.Name; | ||
ConsoleLogMessage(ConsoleColor.Yellow, $"Method: {methodName} start"); | ||
try | ||
{ | ||
invocation.Proceed(); | ||
ConsoleLogMessage(ConsoleColor.Yellow, $"Method: {methodName} was succeed"); | ||
} | ||
catch (Exception e) | ||
{ | ||
ConsoleLogMessage(ConsoleColor.Red, $"Exception {e} was raised in Method: {methodName}"); | ||
throw; | ||
} | ||
ConsoleLogMessage(ConsoleColor.Yellow, $"Method: {methodName} exit"); | ||
} | ||
|
||
private void ConsoleLogMessage(ConsoleColor color, string message, params object[] args) | ||
{ | ||
var originalColor = Console.ForegroundColor; | ||
Console.ForegroundColor = color; | ||
Console.WriteLine(message, args); | ||
Console.ForegroundColor = originalColor; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ended_02/UserManagement-before/UserManagement/UserManagement/Factory/NewsletterFactory.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using UserManagement.Models; | ||
|
||
namespace UserManagement.Factory | ||
{ | ||
public class NewsletterFactory | ||
{ | ||
public Email CreateNewsletter() | ||
{ | ||
string subject = "Máme pro vás jedinečnou nabídku!"; | ||
string body = "Začněte používat návrhové vzory právě teď!"; | ||
|
||
return new Email(subject, body); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Lectures/Extended_02/UserManagement-before/UserManagement/UserManagement/Models/Email.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace UserManagement.Models | ||
{ | ||
public struct Email | ||
{ | ||
public string Subject { get; } | ||
public string Body { get; } | ||
|
||
public Email(string subject, string body) | ||
{ | ||
Subject = subject; | ||
Body = body; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Lectures/Extended_02/UserManagement-before/UserManagement/UserManagement/Program.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Castle.MicroKernel.Registration; | ||
using Castle.Windsor; | ||
using UserManagement.Decorator; | ||
using UserManagement.Visitor.Advanced; | ||
using UserManagement.Factory; | ||
using UserManagement.Models; | ||
using UserManagement.Services; | ||
using UserManagement.Singleton; | ||
|
||
namespace UserManagement | ||
{ | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
//var program = new UserManagementApp(new DisplayUserVisitor(), new NewsletterFactory(), new SendMailUserVisitor(new LoggedEmailService(new MailerService()))); | ||
//program.Start(); | ||
|
||
var container = CreateContainer(); | ||
var app = container.Resolve<UserManagementApp>(); | ||
app.Start(); | ||
} | ||
|
||
private static WindsorContainer CreateContainer() | ||
{ | ||
var container = new WindsorContainer(); | ||
container.Register(Component.For<NewsletterFactory>().LifestyleTransient()); | ||
container.Register(Component.For<UserManagementApp>().LifestyleTransient()); | ||
container.Register(Component.For<DisplayUserVisitor>().LifestyleTransient()); | ||
container.Register(Component.For<SendMailUserVisitor>().LifestyleTransient()); | ||
container.Register(Component.For<IEmployeeStorage>().Instance(EmployeeStorage.Instance));//.ImplementedBy<EmployeeStorage>().LifestyleSingleton()); | ||
container.Register(Component.For<IEmailService>().ImplementedBy<EmailService>().LifestyleTransient().Interceptors<LoggingInterceptor>()); | ||
container.Register(Component.For<LoggingInterceptor>().LifestyleTransient()); | ||
|
||
return container; | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../Extended_02/UserManagement-before/UserManagement/UserManagement/Services/EmailService.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using UserManagement.Models; | ||
|
||
namespace UserManagement.Services | ||
{ | ||
public class EmailService : IEmailService | ||
{ | ||
public void SendEmail(string to, Email email) | ||
{ | ||
Console.WriteLine($"Send email to: {to}"); | ||
Console.WriteLine($"\t{email.Subject} - {email.Body}"); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...Extended_02/UserManagement-before/UserManagement/UserManagement/Services/IEmailService.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using UserManagement.Models; | ||
|
||
namespace UserManagement.Services | ||
{ | ||
public interface IEmailService | ||
{ | ||
void SendEmail(string to, Email email); | ||
} | ||
} |
Oops, something went wrong.