C# MVC C3继承类的正确格式是什么?
我创建了以下类,它描述了用户可以上载的任何类型的文件:C# MVC C3继承类的正确格式是什么?,c#,oop,C#,Oop,我创建了以下类,它描述了用户可以上载的任何类型的文件: namespace MyModels.Models { public class File { public string FileName { get; set; } public string FileTypeId { get; set; } public string URLFileName { get; set; } //cleaned for web
namespace MyModels.Models
{
public class File
{
public string FileName { get; set; }
public string FileTypeId { get; set; }
public string URLFileName { get; set; } //cleaned for web
public string Dir { get; set; } //which directory is located in
public long FileSize { get; set; } //size in bytes
public DateTime DateAdded { get; set; } //date uploaded
public int UploadedByUser {get; set;} //UserID of user
public bool inCloud { get; set; } // moved to cloud
public bool inGlacier { get; set; } // moved to glaciers
public DateTime? DateTrashed { get; set; } //date user deleted
public int TrashedByUser { get; set; } //UserID of user
public List<FileDescendent> Descendents { get; set; } //List of copies
}
}
还是这个
namespace MyModels.Models
{
public class Image
{
public int OrigHeight { get; set; }
public int OrigWidth { get; set; }
public File File { get; set; }
}
}
请问有什么区别
谢谢 我认为区别在于API。您希望将inCloud查找为
image.inCloud
还是image.File.inCloud
在大多数情况下,我更喜欢
image.inCloud
。但是,例如,如果实例化文件的成本很高,则可能首选第二个访问器模式。第一个示例是继承,第二个示例不是
在第二个示例中,您还将拥有文件属性,但将其捆绑在文件
属性中。例如,要访问FileName
属性,可以使用obj.File.FileName
而不是继承类时获得的obj.FileName
另外,由于
文件
是图像
类中的一个属性,因此在创建图像
类的实例时,不会自动创建它。您还必须创建文件
类的实例,并将其放入图像
实例的文件
属性中。我选择继承。它将允许您为这两种应用程序使用依赖项注入模型。MVC3非常有用的东西
第二大优势是,您可以拥有显示文件列表的共享视图,并且可以传入这两种类型的集合。更不用说你写的任何东西了。这是一本书。在这种情况下,图像是一个文件
public class Image : File
{
public int OrigHeight { get; set; }
public int OrigWidth { get; set; }
}
public class Image
{
public int OrigHeight { get; set; }
public int OrigWidth { get; set; }
public File File { get; set; }
}
这是一个很好的例子。在这种情况下,图像有一个文件
public class Image : File
{
public int OrigHeight { get; set; }
public int OrigWidth { get; set; }
}
public class Image
{
public int OrigHeight { get; set; }
public int OrigWidth { get; set; }
public File File { get; set; }
}
对我来说,图像是一个文件。因此,继承在这里更合适。我认为这是一个一般的对象设计问题,而不是特定于MVC3。谢谢,这是一个非常清楚的答案。从概念上讲,我现在可以理解其中的区别了。非常感谢。谢谢你的回复!他们都很有帮助。谢谢你!