我正在使用XMLSOCCER.COM API。我对如何最好地设计应用程序感到有点困惑。我面临的问题是,球队没有联赛ID,所以我不能把一支球队和一个联赛联系起来。
为了把他们联系起来,我的联盟有一个List<Team>,而球队有List<Player>。
我试图减少对API的调用,但是我找不到解决这个问题的方法。
class Leagues
{
public List<League> _Leagues { get; set; }
public Leagues()
{
XmlDocument xdoc = Program.MakeRequest("http://www.xmlsoccer.com/FootballDataDemo.asmx/GetAllLeagues", Program.API_KEY);
StringBuilder output = new StringBuilder();
string json = JsonConvert.SerializeXmlNode(xdoc);
JObject o = JObject.Parse(json);
IList<JToken> results = o["XMLSOCCER.COM"]["League"].Children().ToList();
IList<League> leagues = new List<League>();
_Leagues = new List<League>();
foreach (JToken result in results)
{
League league = JsonConvert.DeserializeObject<League>(result.ToString());
_Leagues.Add(league);
}
}
}
class League
{
public int id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Historical_Data { get; set; }
public string Fixtures { get; set; }
public string Livescore { get; set; }
public int NumberOfMatches { get; set; }
public DateTime LatestMatch { get; set; }
public List<Team> Teams { get; set; }
public League()
{
}
public void GetLeagueTeamsByID(int league, string seasondateString){
var url = String.Format("http://www.xmlsoccer.com/FootballDataDemo.asmx/GetAllTeamsByLeagueAndSeason?ApiKey={0}&league={1}&seasonDateString={2}", Program.API_KEY, id, seasondateString);
try
{
XmlDocument xdoc = Program.MakeRequest(url);
StringBuilder output = new StringBuilder();
string json = JsonConvert.SerializeXmlNode(xdoc);
JObject o = JObject.Parse(json);
IList<JToken> results = o["XMLSOCCER.COM"]["Team"].Children().ToList();
IList<Team> teams = new List<Team>();
Teams = new List<Team>();
foreach (JToken result in results)
{
Team team = JsonConvert.DeserializeObject<Team>(result.ToString());
Teams.Add(team);
}
}
catch (Exception ex)
{
Console.WriteLine("------------ERROR----------------");
Console.WriteLine(ex.ToString());
}
}
}
class Team
{
public int Team_Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Stadium { get; set; }
public string HomePageURL { get; set; }
public string WIKILink { get; set; }
public List<Player> Players { get; set; }
public Team()
{
GetPlayersByTeam(Team_Id);
}
public void GetPlayersByTeam(int team)
{
var url = String.Format("http://www.xmlsoccer.com/FootballDataDemo.asmx/GetPlayersByTeam?ApiKey={0}&team_id={1}", Program.API_KEY, team);
try
{
XmlDocument xdoc = Program.MakeRequest(url);
StringBuilder output = new StringBuilder();
string json = JsonConvert.SerializeXmlNode(xdoc);
JObject o = JObject.Parse(json);
IList<JToken> results = o["XMLSOCCER.COM"]["Player"].Children().ToList();
IList<Player> players = new List<Player>();
Players = new List<Player>();
foreach (JToken result in results)
{
Player player = JsonConvert.DeserializeObject<Player>(result.ToString());
Players.Add(player);
}
}
catch (Exception ex)
{
Console.WriteLine("------------ERROR----------------");
Console.WriteLine(ex.ToString());
}
}
}
class Player{
public int Id { get; set; }
public string Name { get; set; }
public double Height { get; set; }
public double Weight { get; set; }
public string Nationality { get; set; }
public string Position { get; set; }
public int Team_Id { get; set; }
public int PlayerNumber { get; set; }
public DateTime DateOfBirth { get; set; }
public DateTime DateOfSigning { get; set; }
public string Signing { get; set; }
} static void Main(string[] args)
{
Leagues leagues = new Leagues();
foreach (var league in leagues._Leagues)
{
//Just get Scottish Premier League
if (league.id == 3)
{
Console.WriteLine(league.id + " " + league.Name);
league.GetLeagueTeamsByID(league.id, "1415");
foreach (var team in league.Teams)
{
Console.WriteLine(team.Team_Id + " " + team.Name);
foreach (var player in team.Players)
{
Console.WriteLine("\t\t Player Name:" + player.Name);
}
}
}
}
Console.ReadKey();
}
public static XmlDocument MakeRequest(string requestUrl, string API_KEY)
{
try
{
requestUrl = requestUrl + "?ApiKey=" + API_KEY;
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(response.GetResponseStream());
return (xmlDoc);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
return null;
}
}
public static XmlDocument MakeRequest(string requestUrl)
{
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(response.GetResponseStream());
return (xmlDoc);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
return null;
}
}<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>45</Team_Id>
<Name>Aberdeen</Name>
<Country>Scotland</Country>
<Stadium>Pittodrie Stadium</Stadium>
<HomePageURL>http://www.afc.co.uk</HomePageURL>
<WIKILink>http://en.wikipedia.org/wiki/Aberdeen_F.C.</WIKILink>
</Team>
<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>46</Team_Id>
<Name>St Johnstone</Name>
<Country>Scotland</Country>
<Stadium>McDiarmid Park</Stadium>
<HomePageURL>http://www.perthstjohnstonefc.co.uk</HomePageURL>
<WIKILink>http://en.wikipedia.org/wiki/St._Johnstone_F.C.</WIKILink>
</Team>
<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>47</Team_Id>
<Name>Motherwell</Name>
<Country>Scotland</Country>
<Stadium>Fir Park Stadium</Stadium>
<HomePageURL>http://www.motherwellfc.co.uk</HomePageURL>
<WIKILink>http://en.wikipedia.org/wiki/Motherwell_F.C.</WIKILink>
</Team>
<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>48</Team_Id>
<Name>Inverness C</Name>
<Country>Scotland</Country>
<Stadium>Caledonian Stadium</Stadium>
<HomePageURL>http://ictfc.com</HomePageURL>
<WIKILink>
http://en.wikipedia.org/wiki/Inverness_Caledonian_Thistle_F.C.
</WIKILink>
</Team>
<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>50</Team_Id>
<Name>Hearts</Name>
<Country>Scotland</Country>
<Stadium>Tynecastle Stadium</Stadium>
<HomePageURL>http://www.heartsfc.co.uk/page/Home</HomePageURL>
<WIKILink>
http://en.wikipedia.org/wiki/Heart_of_Midlothian_F.C.
</WIKILink>
</Team>
<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>51</Team_Id>
<Name>Dundee United</Name>
<Country>Scotland</Country>
<Stadium>Tannadice Park</Stadium>
<HomePageURL>http://www.dundeeunitedfc.co.uk</HomePageURL>
<WIKILink>http://en.wikipedia.org/wiki/Dundee_United_F.C.</WIKILink>
</Team>
<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>52</Team_Id>
<Name>Kilmarnock</Name>
<Country>Scotland</Country>
<Stadium>Rugby Park</Stadium>
<HomePageURL>http://www.kilmarnockfc.co.uk/page/Home</HomePageURL>
<WIKILink>http://en.wikipedia.org/wiki/Kilmarnock_F.C.</WIKILink>
</Team>
<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>53</Team_Id>
<Name>Hibernian</Name>
<Country>Scotland</Country>
<Stadium>Easter Road</Stadium>
<HomePageURL>http://www.hibernianfc.co.uk/page/Home</HomePageURL>
<WIKILink>http://en.wikipedia.org/wiki/Hibernian_F.C.</WIKILink>
</Team>
<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>54</Team_Id>
<Name>Celtic</Name>
<Country>Scotland</Country>
<Stadium>Celtic Park</Stadium>
<HomePageURL>http://www.celticfc.net</HomePageURL>
<WIKILink>http://en.wikipedia.org/wiki/Celtic_F.C.</WIKILink>
</Team>
<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>56</Team_Id>
<Name>St Mirren</Name>
<Country>Scotland</Country>
<Stadium>St. Mirren Park</Stadium>
<HomePageURL>http://www.saintmirren.net/pages/</HomePageURL>
<WIKILink>http://en.wikipedia.org/wiki/St._Mirren_F.C.</WIKILink>
</Team>
<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>360</Team_Id>
<Name>Ross County</Name>
<Country>Scotland</Country>
<Stadium>Victoria Park</Stadium>
<HomePageURL>http://www.rosscountyfootballclub.co.uk/</HomePageURL>
<WIKILink>http://en.wikipedia.org/wiki/Ross_County_F.C.</WIKILink>
</Team>
<Team xmlns="http://xmlsoccer.com/Team">
<Team_Id>561</Team_Id>
<Name>Partick</Name>
<Country>Scotland</Country>
<Stadium>Firhill Stadium</Stadium>
<HomePageURL>http://www.ptfc.co.uk/</HomePageURL>
<WIKILink>http://en.wikipedia.org/wiki/Partick_Thistle_F.C.</WIKILink>
</Team>发布于 2015-09-27 18:26:19
再加上..。我想要明确的是,你的联盟/球队/球员集合等-你的领域/业务对象,应该只在他们之间工作。达尔会创造这些。那么,像FindByTeamName()这样的方法是微不足道的,因为我们已经通过了所有这些XML内容。
public class League {
protected List<Team> Teams { get; set; }
///<summary>
/// Case Sensitive search by Name. Returns null if not found
///<summary>
public Team FindByName(string teamName) {
return Teams.Find(x -> x.Name == teamName);
}
}我试图减少对API的调用,但是我找不到解决这个问题的方法。
我认为主要的问题是,一切都是public。因此,客户端代码可以在任何时间在任何地方执行任何操作--所有代码调用都是免费的。
我面临的问题是,球队没有联赛ID,所以我不能把一支球队和一个联赛联系起来。为了把他们联系起来,我的联盟有一份名单,而球队有一份名单。
你的意思是,如果你有那个ID,那么一个League就不会有一个List<Team>?但它当然应该!这里没有构建规范化的关系数据库。
@RubberDuck说:“我真的不喜欢你的建设者有太多的逻辑。”
我想强调的是,不应该将原始XML转储到构造函数中并将其解析出来--例如,我的意思是将单个团队的XML转换到Team构造函数中。相反,您的"DAL/Factory“应该解析它,并且:
Player对象List<Player>Team并在构造函数中传递该List<Player>List<Team>League并在构造函数中传递List<Team>。当然,您可以让Team.Add(Player aPlayer)、League.Add(Team aTeam)等来一次构建一个Team播放器--就像DAL/工厂构建它们一样。
..。我不得不叫GetTeamsByLeagueAndSeason在联赛的建设,反之亦然的球队->球员。
不是的。不,你没有。你会有办法的。在一切都被构造后调用。
public class Leagues {
protected List<League> Leagues { get; set; }
public ??? GetLeagueBySeason (string LeagueName, string season) {
League thisLeague = GetByName (sring LeagueName);
if(thisLeague != null)
thisLeague.GetSeason(season);
}
public League GetByName(string leagueName) {
return Leagues.Find(x -> x.Name == leagueName);
}
}
public class League {
protected List<Team> Teams { get; set; }
public ????? GetSeason (string thisSeason){
// I have no idea what a season is. I expect you need
// a Season class. Perhaps it has a complete list of games,
// which is yet another class. A Game: date, opponents, score.
// then a season: year, List<Game>
}
}发布于 2015-05-02 17:42:05
我认为你寻求一些建议是对的。
{get; set;}属性组成。为了将这些数据隐藏起来,您需要引入一个数据访问层( DAL )。您的DAL将负责查询来自网站的数据,并创建您的模型的新实例。这是我前面提到的那家一流工厂。不幸的是,我现在没有时间起草一个例子。我希望这还能帮上忙。
https://codereview.stackexchange.com/questions/88637
复制相似问题