简体   繁体   English

Web应用程序类设计建议?

[英]Web Application Class Design Advice?

My web application is going to consist of some people information. 我的Web应用程序将包含一些人员信息。 Basically in my database I will have fields such as 基本上在我的数据库中,我将拥有诸如

personid              autoid               clubid              personalinfoid
fname                 carmodel             clubname            personid 
mname                 cartype              clubaddress         height
lname                 carcolor                                 ethnicity
address               license#                                 driverslicense#
address2              boattype                                 
homephone             boatsize
cellphone

I am not worried about the table design just did this for an example. 我不担心表格设计只是为了举例。

Edited my question to try to make it more clear. 编辑了我的问题,以使其更加清楚。 How would you approach designing classes for a web application of this nature. 您将如何设计这种性质的Web应用程序的类。 Should I have separate classes for each table? 每个表格都应该有单独的类吗?

What I had in mind was to create a properties class that holds all the fields and properties gets and sets everything from the db. 我想到的是创建一个包含所有字段的属性类,并且属性从db获取和设置所有内容。 Then creating a person.cs, auto.cs, personinfo.cs, and clubs.cs classes would this be the right way to do this? 然后创建一个person.cs,auto.cs,personinfo.cs和clubs.cs类是这样做的正确方法吗?

What you have suggested makes sense and is what Object to Database frameworks do! 您所建议的是有道理的,并且是对象数据库框架的功能!

If you were using Entity Framewor k for example, you could create you classes first (imaginatively called "code first") and then EF would generate the database for you. 例如,如果您使用的是Entity Framewor k,则可以先创建类(想象中称为“代码优先”),然后EF将为您生成数据库。 Equally you could do "database first" and create you database schema then EF will generate the classes for you. 同样,您可以“首先执行数据库”并创建数据库架构,然后EF将为您生成类。 Personally I prefer the former as it gives you a bit more control with database versioning. 就我个人而言,我更喜欢前者,因为它使您可以更好地控制数据库版本。

There are a plethora of other frameworks available and this is just one example. 还有很多其他可用的框架,这只是一个示例。

So your PersonalInfo object would look something like this using EF: 因此,使用EF,您的PersonalInfo对象将如下所示:

public class PersonalInfo
{
    public int Id { get; set; } //EF will automatically figure out this is your PK
    public double Height { get; set; }
    public string Ethnicity { get; set; }
    public string DriversLicence { get; set; }

    //EF automatically figures out that the PersonId property refers to your Person table
    public int PersonId { get; set; }
    public virtual Person Person { get; set; } //navigation property so you can do personalInfo.Person.FirstName

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM