【发布时间】:2016-01-14 17:47:18
【问题描述】:
我在将 DAL 级类的结果“继承”到具有相同定义的 BLL 类时遇到问题。
错误:“无法将'DeserializedGame'类型的对象转换为'DeserializedGameBLL'类型。”
//DAL Classes
public class GameCollection{
public List<DeserializedGame> Games { get; set; }
public int RefreshInterval { get; set; }
public string CurrentDate { get; set; }
public string NextDate { get; set; }
public string PrevDate { get; set; }
}
public class DeserializedGame
{
public string Atcommon { get; set; }
public string Canationalbroadcasts { get; set; }
public string Ata { get; set; }
public bool Rl { get; set; }
public int Atsog { get; set; }
public string Bs { get; set; }
public string Htcommon { get; set; }
public int Id { get; set; }
public string Atn { get; set; }
public int Hts { get; set; }
public string Atc { get; set; }
public string Htn { get; set; }
public string Usnationalbroadcasts { get; set; }
public bool Gcl { get; set; }
public string Hta { get; set; }
public int? Ats { get; set; }
public string Htc { get; set; }
public int Htsog { get; set; }
public string Bsc { get; set; }
public int Gs { get; set; }
public bool Gcll { get; set; }
}
//BLL Classes
public class GameCollectionBLL : IEnumerable
{
public List<DeserializedGame> Games { get; set; }
public int RefreshInterval { get; set; }
public string CurrentDate { get; set; }
public string NextDate { get; set; }
public string PrevDate { get; set; }
public GameCollectionBLL(List<DeserializedGame> gameList, int refreshInterval, string currentDate,
string nextDate, string previousDate)
{
this.Games = gameList;
this.RefreshInterval = refreshInterval;
this.CurrentDate = currentDate;
this.NextDate = nextDate;
this.PrevDate = previousDate;
}
public IEnumerator GetEnumerator()
{
return (Games as IEnumerable).GetEnumerator();
}
}
public class DeserializedGameBLL : BaseSeasonSchedule
{
public string Atcommon { get; set; }
public string Canationalbroadcasts { get; set; }
public string Ata { get; set; }
public bool Rl { get; set; }
public int Atsog { get; set; }
public string Bs { get; set; }
public string Htcommon { get; set; }
public int Id { get; set; }
public string Atn { get; set; }
public int Hts { get; set; }
public string Atc { get; set; }
public string Htn { get; set; }
public string Usnationalbroadcasts { get; set; }
public bool Gcl { get; set; }
public string Hta { get; set; }
public int? Ats { get; set; }
public string Htc { get; set; }
public int Htsog { get; set; }
public string Bsc { get; set; }
public int Gs { get; set; }
public bool Gcll { get; set; }
}
public abstract class BaseSeasonSchedule
{
public int GameID { get; set; }
public DateTime GameDate { get; set; }
}
//UI usage
public partial class Form1 : Form
{
private SeasonSheduleBLL objSeasonSchedule = new SeasonSheduleBLL();
private void button1_Click(object sender, EventArgs e)
{
GameCollectionBLL gameDay = objSeasonSchedule.GetGameCollection(json_string);
foreach (DeserializedGameBLL game in gameDay) //error occurs here when referencing the DeserialzedGameBLL
{
// do stuff here
}
}
}
我需要做些什么才能使我的 DeserialzedGame 集合正常工作?
我查看了this question(并尝试遵循原始帖子中的@Measuring 建议)。我已经考虑过(但仍然没有消除using DTO's,但我不确定如何正确使用它们)。
如果这属于 Code Review 部分,我会很乐意在那里发帖,但我认为可能有足够多的人正在经历与我一样的挫败感。
【问题讨论】:
-
使用 Automapper 或其他库在图层中的对象之间进行映射。
-
错误告诉你不能在两个不相关的类型之间隐式转换。您必须创建目标类型的新实例并将其属性设置为与源类型匹配。但是......为什么你首先有两个相同的类型?只需在整个系统中使用其中一个并删除另一个。
-
@David 假设您的建议是 DTO 的全部内容是否正确?
-
@Kulstad:嗯,您的对象已经是 DTO。所以我真的不知道你在那里问什么。但关键是你真的不需要完全相同的对象。
-
@David 我认为这是我开始感觉自己在编写体重级别之外的代码的地方。我知道你在告诉我什么,我只是不知道如何实现它。
标签: c#