SINGLE RESPONSIBILITY PRINCIPLE
- CricketRatingSource
- CricketDeserializer
- Logger
OCP is still violated , which we will resolve it in next chapters.
// See https://aka.ms/new-console-template for more information
using Newtonsoft.Json;
using System.Xml.Serialization;
public class CricketDeserializer
{
public List cricketRatingDeserializeJson(string sourceJson)
{
return JsonConvert.DeserializeObject>(sourceJson);
}
public List cricketRatingDeserializeXml(string sourceXml)
{
return DeserializeToObject>(sourceXml);
}
public T DeserializeToObject(string filepath) where T : class
{
XmlSerializer ser = new XmlSerializer(typeof(T));
using (StreamReader sr = new StreamReader(filepath))
{
return (T)ser.Deserialize(sr);
}
}
public T SerializeXml(string filepath, T result) where T : class
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
using (FileStream stream = File.Create(filepath))
{
xmlSerializer.Serialize(stream, result);
}
return result;
}
}
CricketRatingSource
namespace SRP
{
public class CricketRatingSource
{
const string jsonpath = "C:\\Users\\611184113\\source\\repos\\SRP\\SRP\\cricket.json";
string xmlpath = Path.Combine(Environment.CurrentDirectory, "cricket.xml");
public string CricketRatingFromJson()
{
return File.ReadAllText(jsonpath);
}
public string CricketRatingFromXML()
{
return xmlpath;
}
}
}
CricketSpecialistInAFormat
// See https://aka.ms/new-console-template for more information
using SRP;
internal class CricketSpecialistInAFormat
{
public Logger Logger { get; set; } = new Logger();
public CricketRatingSource sourceObj = new ();
public CricketDeserializer cricketDeserializerObj = new();
public int OverAllRating { get; set; }
internal void OverAllRate(Source source)
{
List ratingObject = new();
if(Source.JSON == source)
{
var ratingJsonStr = sourceObj.CricketRatingFromJson();
ratingObject = cricketDeserializerObj.cricketRatingDeserializeJson(ratingJsonStr); ;
}
else if (Source.XML == source)
{
var ratingXMLStr = sourceObj.CricketRatingFromXML();
ratingObject = cricketDeserializerObj.cricketRatingDeserializeXml(ratingXMLStr);
}
foreach (var item in ratingObject)
{
Logger.Log("-----------------------------");
Enum.TryParse(item.Format, out CricketFormat format);
switch (format)
{
case CricketFormat.T20:
Logger.Log("T20 Format");
if (item.StrikeRate > 150 && item.Average > 40)
{
Logger.Log($"star player:: {item.Name}");
Logger.Log("Applicable for special bonus");
}
else
{
Logger.Log($"Average player:: {item.Name}");
Logger.Log("player will be given next 10 chances to prove");
Logger.Log("Not applicable for bonus");
}
break;
case CricketFormat.ODI:
Logger.Log("ODI Format");
if (item.StrikeRate > 95)
{
Logger.Log($"star player:: {item.Name}");
Logger.Log("Applicable for special bonus");
}
else
{
Logger.Log($"Average player:: {item.Name}");
Logger.Log("Not applicable for bonus");
}
break;
default:
Logger.Log("unknown format!!");
break;
}
}
}
}