Can you please help me split this method to reuse the repeating code?
Any advice/comments on the code are welcome.
//This method will return Dictionary object having key/value pair of keywords and URLs.
//Keywords are single word or bunch of words from db.
//Value in dictionary is URLs against each key (keyword).
//Code starts from here.
private static Dictionary<string, string> GetKeywordsAndEntityWithURL(int eventTypeId, string entityName)
{
// Get initial url from configuration file.
string basewebUrl = CommonMethods.GetAppSettingsValue("Baseweb");
// Get all keywords from db, from KeyWords table.
KeywordsCollection keyWords = KeywordsEntity.GetKeywords(null, eventTypeId);
// This text will be concatenated with entityname.
// Purpose of this is to avoid replacement of EntityName again in find/replace.
string dummyText = "TemporaryText";
// This object will be used to create key/value pair of keys and URLs.
Dictionary<string, string> keyWordsWithURLs = new Dictionary<string, string>();
// There are two types of keywords in db, Singular and Plural. Get each and create key/value using them.
foreach (KeywordsEntity keyWord in keyWords)
{
string singularKeyword = keyWord.Keyword;
// Keywords are like 'stag do' in db we need to replace white space
// with hyphen to use this in URLs.
singularKeyword = singularKeyword.Replace(' ', '-');
// This key is like 'stag do London'.
string singularKey = string.Empty;
singularKey = string.Format("{0} {1}", keyWord.Keyword.ToString(), entityName);
// Concat entityName (London) with dummytext.
entityName = entityName + dummyText;
// Text of URL e.g. stag do London
string urlText = keyWord.Keyword + " " + entityName;
// This value is like initial url (whatever it is in config file)/stag-do/london
string singularValue = string.Empty;
singularValue = string.Format("<a style='text-decoration:underline;' href='" + basewebUrl + "/{0}/{1}'>{2}</a>", singularKeyword, entityName, urlText);
// Add key/value in dictionaly.
keyWordsWithURLs.Add(singularKey, singularValue);
// Plural keywords are like 'stag dos' in db we need to replace space with hyphen for URLs.
string pluralKeyword = keyWord.PluralKeyword;
pluralKeyword = pluralKeyword.Replace(' ', '-');
// This key would be 'stag dos london'
string pluralKey = string.Empty;
pluralKey = string.Format("{0} {1}", keyWord.PluralKeyword.ToString(), entityName);
// URL would be, initial url(whatever in config file)/ stag-dos/london
string pluralValue = string.Empty;
pluralValue = string.Format("<a style='text-decoration:underline;' href='" + basewebUrl + "/{0}/{1}'>{2}</a>", pluralKeyword, entityName, urlText);
// Add key/value in dictionary.
keyWordsWithURLs.Add(pluralKey, pluralValue);
// Singular Key = London stag do
singularKey = string.Format("{0} {1}", entityName, keyWord.Keyword.ToString());
// Text of anchor e.g. London stag do.
urlText = entityName + dummyText + " " + keyWord.Keyword;
// Singular Value = /stag-do/london
singularValue = string.Format("<a style='text-decoration:underline;' href='" + basewebUrl + "/{0}/{1}'>{2}</a>", singularKeyword, entityName, urlText);
keyWordsWithURLs.Add(singularKey, singularValue);
// Plural Key = London stag dos
pluralKey = string.Format("{0} {1}", entityName, keyWord.PluralKeyword.ToString());
// Plural Key = /stag-dos/lonon
pluralValue = string.Format("<a style='text-decoration:underline;' href='" + basewebUrl + "/{0}/{1}'>{2}</a>", pluralKeyword, entityName, urlText);
// Add key/value pair in dictionary.
keyWordsWithURLs.Add(pluralKey, pluralValue);
urlText = keyWord.Keyword + " in " + entityName + dummyText;
singularKey = string.Format("{0} {1} {2}", keyWord.Keyword.ToString(), "in", entityName);
singularValue = string.Format("<a style='text-decoration:underline;' href='" + basewebUrl + "/{0}/{1}'>{2}</a>", singularKeyword, entityName, urlText);
// Add key/value in dictionaly.
keyWordsWithURLs.Add(singularKey, singularValue);
pluralKey = string.Format("{0} {1} {2}", keyWord.PluralKeyword.ToString(), "in", entityName);
pluralValue = string.Format("<a style='text-decoration:underline;' href='" + basewebUrl + "/{0}/{1}'>{2}</a>", pluralKeyword, entityName, urlText);
// Add key/value in dictionary.
keyWordsWithURLs.Add(pluralKey, pluralValue);
if (keyWord.IsDefault)
{
pluralKeyword = keyWord.PluralKeyword.Replace(' ', '-');
pluralKey = string.Format("{0}", entityName);
pluralValue = string.Format("<a style='text-decoration:underline;' href='" + basewebUrl + "/{0}/{1}'>{2}</a>", pluralKeyword, entityName, entityName);
keyWordsWithURLs.Add(pluralKey, pluralValue);
}
}
return keyWordsWithURLs;
}
If you need the full class, please write a comment. I will upload it.