C# MVC3简单继承
我遇到过这样一种情况,答案应该是非常直截了当的,但我却找不到C# MVC3简单继承,c#,asp.net-mvc,inheritance,C#,Asp.net Mvc,Inheritance,我遇到过这样一种情况,答案应该是非常直截了当的,但我却找不到 public class Note { #region Properties public int Id { get; set; } public int ClientId { get; set; } public int CandidateId { get; set; } public int TypeId { get; set; } public DateTime DateCre
public class Note
{
#region Properties
public int Id { get; set; }
public int ClientId { get; set; }
public int CandidateId { get; set; }
public int TypeId { get; set; }
public DateTime DateCreated { get; set; }
public string UserId { get; set; }
public string UserName { get; set; }
public string Message { get; set; }
#endregion
#region Methods
public void Save()
{
}
#endregion
}
public class History : Note
{
}
如您所见,历史继承了注释。它们完全相同,唯一的区别是类型Id
我从数据库中获取数据时具有此功能
public static Note Parse(SqlDataReader dr)
{
int TypeId = Convert.ToInt32(dr["TypeId"]);
Note Note;
if (TypeId == 1)
Note = new Note();
else
Note = new History();
Note.Id = Convert.ToInt32(dr["Id"]);
Note.TypeId = TypeId;
if (dr["ClientId"] != DBNull.Value) Note.ClientId = Convert.ToInt32(dr["ClientId"]);
if (dr["CandidateId"] != DBNull.Value) Note.CandidateId = Convert.ToInt32(dr["CandidateId"]);
Note.DateCreated = Convert.ToDateTime(dr["DateCreated"]);
Note.UserId = Convert.ToString(dr["UserId"]);
Note.UserName = Convert.ToString(dr["UserName"]);
Note.Message = Convert.ToString(dr["Message"]);
return Note;
}
然后在我的MVC页面上,我有以下内容:
<ol id="interview-comments">
@foreach (Note Note in Model.Notes().OfType<Note>())
{
}
</ol>
<ol id="history-comments">
@foreach (History Note in Model.Notes().OfType<History>())
{
}
</ol>
@foreach(type()的Model.Notes()中的注释)
{
}
@foreach(type()的Model.Notes()中的历史记录注释)
{
}
我的问题很简单。这是正确的方法吗
我在这里可能没有完整的图片,但我只考虑使用一个类-<代码>注释< /代码>。而是在该类上有一个属性-
IsHistoric
。然后根据属性而不是类型进行检查。由于历史记录
是注释
,因此您的Model.Notes().OfType()
还将包括历史记录
实例-这是故意的吗
您可能只想使用一个实体,并添加一个标志,以确定是否为注释;这将使它更清晰,并避免继承问题
或者,您可以使用公共接口或抽象基类,而不是让这些类从另一个继承一个,而是让它们都从同一个基类继承,这也将解决类型的问题
或者如果继承是正确的,那么像这样过滤:
Model.Notes().Where(n=>n.GetType()=typeof(Note))
,或者只是Model.Notes().Where(n=>!(n是历史))
-有很多方法可以到达罗马。如果这是全部情况,我不会在这里使用继承。
您说过便笺和历史记录项有不同的TypeId:s
然后我会做以下几件事:
@foreach(var item in Model.Notes().Where(x => x.TypeId == Note.NoteTypeId))
{
}
//and
@foreach(var item in Model.Notes().Where(x => x.TypeId == Note.HistoryTypeId))
{
}
public class Note
{
public static int HistoryTypeId = 1;
public static int NoteTypeId = 0;
/* ... the rest of the implementation */
}
您还可以将TypeId更改为枚举并“隐藏”一些神奇的数字
编辑:
根据使用情况,您还可以将检查历史注释作为注释上的属性
public class Note
{
/* ... other properties ... */
public bool IsHistoric { get { return this.TypeId != 1; } }
}
那么支票就很简单了
@foreach(var note in Model.Notes().Where(x => x.IsHistoric))
{
}
// and
@foreach(var note in Model.Notes().Where(x => !x.IsHistoric())
{
}
正如答案所说,你可能不应该这样做。一般来说,我发现遗产会咬你,所以如果可能的话,我倾向于远离它。@jesus.tesh,没有必要“远离”遗产,但只有在适当的时候才使用它。不要用螺丝刀敲入钉子……;)@卢塞罗-当然,我不是说不应该使用继承。为工作使用正确的工具,对吗?:)@天哪,泰什,没错是的,我在另一节课上也有类似的经历,在你们的第一部分,linq。我会采纳你的建议,对类型使用枚举,因为我不喜欢使用硬编码的Id,并且我可以在将来添加任意数量的枚举。仍然是硬编码,但让我感觉更好:D