Last active
August 29, 2015 14:06
-
-
Save tolpp/b89aa394a10959ccd366 to your computer and use it in GitHub Desktop.
Delegates-2
This file contains hidden or 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
namespace Tolpp.Delegates2.Context | |
{ | |
/// <summary> | |
/// Kullanicilar uzerinde yapilan islemleri handle eden temel delegate | |
/// </summary> | |
/// <param name="user">Uzerinde islem yapilan kullanici</param> | |
public delegate void UserEventHandler(User user); | |
/// <summary> | |
/// Kullanicilar uzerinde islemlerin yapildigi siniftir | |
/// </summary> | |
public class UserContext | |
{ | |
/// <summary> | |
/// SaveUserToDatabase isimli static metodu UserCreated isimli UserEventHandler'a atiyoruz. | |
/// Dogrudan metod ismi ile verdigimiz icin Implicit delegate creation yapmis olduk. | |
/// </summary> | |
private UserEventHandler UserCreated = SaveUserToDatabase; | |
public UserContext() | |
{ | |
// SendRegisterationSmsToUser metodu UserCreated delegete'i icine atiliyor. | |
UserCreated += SendRegisterationSmsToUser; | |
// NotifyManager metodu UserCreated delegete'i icine atiliyor. | |
// Delegate new UserEventHandler() seklinde olusturuldugundan Explicit delegate creation yapmis olduk. | |
UserCreated += new UserEventHandler(NotifyManager); | |
} | |
public void CreateUser(string username, string mobilPhone) | |
{ | |
//.. | |
User user = new User() | |
{ | |
Username = username, | |
MobilePhone = mobilPhone | |
}; | |
//.. | |
//Kullanici yaratildi event'i tetikleniyor. Kullanici yaratildiktan sonra yapilmasi gereken | |
//islemler otomatik olarak yapiliyor. | |
UserCreated(user); | |
} | |
/// <summary> | |
/// Kullaniciyi database uzerine kaydeden metod | |
/// </summary> | |
/// <param name="user">Yaratilan kullanici</param> | |
private static void SaveUserToDatabase(User user) | |
{ | |
//.. Database islemleri | |
Console.WriteLine(user + @" Database'e kaydedildi"); | |
} | |
/// <summary> | |
/// Kullaniciya kayit sonrasi sms gonderen metod | |
/// </summary> | |
/// <param name="user">Yaratilan kullanici</param> | |
private void SendRegisterationSmsToUser(User user) | |
{ | |
//..Sms gonderim islemi | |
Console.WriteLine(user.MobilePhone + @" numarasina sms gonderildi."); | |
} | |
/// <summary> | |
/// Kullanıcının kaydolduguna dair yoneticiyi bilgilendiren metod | |
/// </summary> | |
/// <param name="user">Yaratilan kullanici</param> | |
private void NotifyManager(User user) | |
{ | |
//.. Yoneticiyi haberdar etme islemi | |
Console.WriteLine(user.Username +@" Manager'a haberdar edildi."); | |
} | |
} | |
/// <summary> | |
/// Kullanici'ya ait bilgilerin tutuldugu siniftir. | |
/// </summary> | |
public class User | |
{ | |
public string Username { get; set; } | |
public string MobilePhone { get; set; } | |
public override string ToString() | |
{ | |
return Username; | |
} | |
} | |
static class Program | |
{ | |
static void Main() | |
{ | |
UserContext context = new UserContext(); | |
context.CreateUser("tolpp","+905320001122"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment