У меня возникла проблема с «переносом» результатов класса уровня DAL в мой класс BLL с тем же определением.Невозможно наложить объект типа «Класс DAL» на тип «BLL Class»
Ошибка: «Невозможно наложить объект типа« 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
@ David Правильно ли было бы предположить, что ваше предложение - это то, что все DTO? – Kulstad