Skip to content

Commit

Permalink
Add materials for extended lecture - design patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
martindybal committed Apr 16, 2018
1 parent 7d5ea5f commit c9c0768
Show file tree
Hide file tree
Showing 27 changed files with 671 additions and 0 deletions.
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
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);
}
}
}
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);
}
}
}
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;
}
}
}
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);
}
}
}
}
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}");
}
}
}
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>
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
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; }
}
}
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>();
}
}
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; }
}
}
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;
}

}
}
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;
}
}
}
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);
}
}
}
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;
}
}
}
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;
}
}
}
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}");
}
}
}
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);
}
}
Loading

0 comments on commit c9c0768

Please sign in to comment.