Created
October 7, 2011 14:34
-
-
Save keithbloom/1270396 to your computer and use it in GitHub Desktop.
WebFromsAndSRP
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
using System; | |
using WebFormsAndSRP.CookieManagement; | |
namespace WebFormsAndSRP | |
{ | |
public partial class Default : System.Web.UI.Page | |
{ | |
private IMarketingTracker _marketingTracker; | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
_marketingTracker = new MarketingTrackerBuilder(Request.Cookies).Build(); | |
} | |
protected void OnSaveButtonClick(object sender, EventArgs e) | |
{ | |
Db.SaveEnquiry(_marketingTracker); | |
} | |
} | |
public static class Db | |
{ | |
public static void SaveEnquiry(IMarketingTracker marketingTracker) | |
{ | |
// Do some Db stuff | |
} | |
} | |
} |
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 WebFormsAndSRP.CookieManagement | |
{ | |
public class MarketingTrackerBuilder | |
{ | |
private readonly HttpCookie _cookie; | |
private const string CookieName = "campaigns"; | |
private readonly Dictionary<string,string> _values; | |
public MarketingTrackerBuilder(HttpCookieCollection cookieCollection) | |
{ | |
_cookie = cookieCollection[CookieName]; | |
_values = new Dictionary<string, string>(); | |
} | |
private void SplitCookie() | |
{ | |
var values = _cookie.Value.Split(','); | |
foreach (var item in values.Select(value => value.Split('='))) | |
{ | |
_values.Add(item[0], item[1]); | |
} | |
} | |
private string Value(string key) | |
{ | |
string output; | |
return _values.TryGetValue(key, out output) ? output : string.Empty; | |
} | |
public IMarketingTracker Build() | |
{ | |
if (_cookie == null) return new NullMarketingTracker(); | |
SplitCookie(); | |
return new MarketingTracker(Value("ch"),Value("ca"),Value("ta"),Value("de")); | |
} | |
} | |
} |
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 WebFormsAndSRP.CookieManagement | |
{ | |
public interface IMarketingTracker | |
{ | |
string Channel { get; } | |
string Campaign { get; } | |
string Target { get; } | |
string Detail { get; } | |
} | |
public class MarketingTracker : IMarketingTracker | |
{ | |
public string Channel { get; private set; } | |
public string Campaign { get; private set; } | |
public string Target { get; private set; } | |
public string Detail { get; private set; } | |
public MarketingTracker(string channel, string campaign, string target, string detaill) | |
{ | |
Channel = channel; | |
Campaign = campaign; | |
Target = target; | |
Detail = detaill; | |
} | |
} | |
} |
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 WebFormsAndSRP.CookieManagement | |
{ | |
public class NullMarketingTracker : IMarketingTracker | |
{ | |
public string Channel | |
{ | |
get { return string.Empty; } | |
} | |
public string Campaign | |
{ | |
get { return string.Empty; } | |
} | |
public string Target | |
{ | |
get { return string.Empty; } | |
} | |
public string Detail | |
{ | |
get { return string.Empty; } | |
} | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.UI; | |
namespace WebFormsAndSRP | |
{ | |
public partial class ViolatesSRP : Page | |
{ | |
private Dictionary<string, string> _values = new Dictionary<string, string>(); | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
var cookie = Request.Cookies["campaign"]; | |
if (cookie == null || String.IsNullOrEmpty(cookie.Value)) return; | |
var items = cookie.Value.Split(','); | |
_values = items.Select(value => value.Split('=')) | |
.ToDictionary(item => item[0], item => item[1]); | |
} | |
protected void OnSaveButtonClick(object sender, EventArgs e) | |
{ | |
Db.SaveEnquiry(Value("ch"), Value("ca"), Value("ta"), Value("de")); | |
} | |
private string Value(string key) | |
{ | |
string output; | |
return _values.TryGetValue(key, out output) ? output : string.Empty; | |
} | |
} | |
public static class Db | |
{ | |
public static void SaveEnquiry(string channel, string campaign, string target, string detail) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment