如何将数据库表设计转换为C#类?

如何将数据库表设计转换为C#类?,c#,database,C#,Database,我基于SQL图创建了两个C#类联系人和客户(见图) 我只是想看看我是否做得对,我需要一些建议?下数据库图和类别客户和联系人 Customer Class public class Customer { public int CustomerID { get; set; } public string Title { get; set; } public string FirstName { get; set; } pu

我基于SQL图创建了两个C#类联系人和客户(见图) 我只是想看看我是否做得对,我需要一些建议?下数据库图和类别客户和联系人

Customer Class


 public class Customer
    {
        public int CustomerID { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Postion { get; set; }
        public Char Gender { get; set; }
        public DateTime BecomeCustomer { get; set; }     
        public DateTime ModifiedDate { get; set; }



        public Customer() { }


        public static bool AddNewCustomer_Contact(Customer cust,Contacts cont)
        {
            try
            {
                // get a configured DbCommand object
                DbCommand comm = GenericDataAccess.CreateCommand();


                //Set the store Proc name 
                comm.CommandText = "AddNewCustomer_Contact";

                //create new parameter @Title 
                DbParameter param = comm.CreateParameter();
                param = comm.CreateParameter();
                param.ParameterName = "@Title";
                param.Value = cust.Title;
                param.DbType = DbType.StringFixedLength;
                comm.Parameters.Add(param);

                //create new parameter @FirstName
                param = comm.CreateParameter();
                param.ParameterName = "@FirstName ";
                param.Value = cust.FirstName;
                param.DbType = DbType.StringFixedLength;
                comm.Parameters.Add(param);

                //create new parameter @LastName
                param = comm.CreateParameter();
                param.ParameterName = "@LastName";
                param.Value = cust.LastName;
                param.DbType = DbType.StringFixedLength;
                comm.Parameters.Add(param);

                //create new parameter @Postion 
                param = comm.CreateParameter();
                param.ParameterName = "@Postion ";
                param.Value = cust.Postion;
                param.DbType = DbType.StringFixedLength;
                comm.Parameters.Add(param);

                //create new parameter @BecomeCustomer
                param = comm.CreateParameter();
                param.ParameterName = "@BecomeCustomer";
                param.Value = DateTime.Now;
                param.DbType = DbType.DateTime;
                comm.Parameters.Add(param);

                //create new parameter @Gender 
                param = comm.CreateParameter();
                param.ParameterName = "@Gender";
                param.Value = cust.Gender;
                param.DbType = DbType.String;
                comm.Parameters.Add(param);

                //create new parameter @ModifiedDate  
                param = comm.CreateParameter();
                param.ParameterName = "@ModifiedDate";
                param.Value = DateTime.Now;
                param.DbType = DbType.StringFixedLength;
                comm.Parameters.Add(param);

                //create new parameter @LabelContactTypeID
                param = comm.CreateParameter();
                param.ParameterName = "@LabelContactTypeID";
                param.Value = cont.LabelContactTypeID;
                param.DbType = DbType.StringFixedLength;
                comm.Parameters.Add(param);

                //create new parameter @ContactDetails
                param = comm.CreateParameter();
                param.ParameterName = "@ContactDetails";
                param.Value = cont.ContactDetail;
                param.DbType = DbType.StringFixedLength;
                comm.Parameters.Add(param);

                //create new parameter @Status
                param = comm.CreateParameter();
                param.ParameterName = "@Status";
                param.Value = cont.Status;
                param.DbType = DbType.StringFixedLength;
                comm.Parameters.Add(param);

                //create new parameter @Notes
                param = comm.CreateParameter();
                param.ParameterName = "@Notes";
                param.Value = cont.Notes;
                param.DbType = DbType.StringFixedLength;
                comm.Parameters.Add(param);


                return (GenericDataAccess.ExecuteNonQuery(comm) != -1);

            }
            catch
            {
                return false;
            }
        }
    }
Contact Class

public class Contacts
{
    public int ContactsID { get; set; }
    public int CustomerID { get;set; }
    public string ContactDetail { get; set; }
    public bool Status { get; set; }
    public int LabelContactTypeID { get; set; }
    public string Notes { get; set; }


}


看起来不错,但我会将数据访问层放在另一个静态类中,因为它并没有真正操纵模型。一个类应该被封装到它的数据和与模型本身上的数据交互的方法中

另外,查看AddWithValue():


可能更容易使用

大多数架构问题/“我做得对吗?”归结为“视情况而定”

有几种方法可以解决这个问题。在一个不是“高性能”的场景中,您希望您的对象直接映射到数据库表,那么使用类似于ORM的实体框架是非常好的

如果您想将域层与数据存储的逻辑设计分离,那么您应该研究类似于存储库模式的东西,并使用DTOs/straight ADO.NET。不过,代码要多得多,总的来说需要付出更多的努力

从你在这里展示的一点来看,它看起来像一个简单的、非高性能的应用程序,所以我会选择最简单的、运行良好的应用程序,在本例中是实体框架或其他ORM


如果要使用ado.net并规划旧的数据传输对象以保持代码整洁,我建议在设计类时,将数据库访问代码放入不同于数据传输对象(DTO,在本例中,客户是DTO,如果愿意的话是属性包)的单独类中,第一步是确定需要哪些类以及它们的属性/职责。据我所知,您想要表示的唯一高级类是客户。Customer是您的高级类,我的意思是Customer的一些属性可以由其他对象(比如Name、Contacts等)构成

此外,像Id这样的一些字段在数据库之外没有任何意义(我假设在本例中Id是Customer表的主键)。如果您执意基于数据库设计(规范化设计),那么我建议您在继续使用类模型之前“取消规范化”您的设计。规范化提供的优化仅在关系模型的范围内,通常不应扩展到内存中表示的类

此外,正如@Yatrix所建议的,最好从实际数据模型中抽象出数据访问层

这里有一些建议,

public class Customer
{
    // public int CustomerID { get; set; } // Database specific primary key
    public string Title { get; set; } 
    public string FirstName { get; set; } // Is the setter required to be publicly exposed? 
                                          // Or can it be private to the class?
    public string LastName { get; set; }  // FirstName and LastName can be part of a nested class 
                                          // Name, so that the access is more natural... 
                                          // Customer.Name.FirstName 
    public string Postion { get; set; }
    public Char Gender { get; set; } // Can be an enumeration
    public DateTime BecomeCustomer { get; set; }     
    public DateTime ModifiedDate { get; set; }
    public IList<Contact> Contacts { get; private set; } // The getter can be private and you can 
                                                         // expose only some methods like 
                                                         // Customer.GetContactOfType(type)


    public bool AddContact(Contact contact)
    {
         // ...
    }

    public bool RemoveContact(Contact contact)
    {
         // ...
    }       
}

public class Contact
{
    public string ContactDetail { get; set; } // Can be named as Detail or something more specific
    public bool Status { get; set; } // Can be enumeration
    public ContactType Type { get; set; } // ContactType is an enumeration
    public string Notes { get; set; }
}
公共类客户
{
//public int CustomerID{get;set;}//特定于数据库的主键
公共字符串标题{get;set;}
公共字符串FirstName{get;set;}//是否需要公开setter?
//或者它可以是班级的私人物品吗?
公共字符串LastName{get;set;}//FirstName和LastName可以是嵌套类的一部分
//名称,使访问更自然。。。
//Customer.Name.FirstName
公共字符串位置{get;set;}
公共字符{get;set;}//可以是枚举
public DateTime BecomeCustomer{get;set;}
公共日期时间修改日期{get;set;}
public IList联系人{get;private set;}//getter可以是private的,您可以
//只公开一些方法,如
//Customer.GetContactOfType(类型)
公共bool AddContact(联系人)
{
// ...
}
公共布尔删除联系人(联系人)
{
// ...
}       
}
公共类联系人
{
公共字符串ContactDetail{get;set;}//可以命名为Detail或更具体的名称
公共布尔状态{get;set;}//可以是枚举
公共ContactType类型{get;set;}//ContactType是一个枚举
公共字符串注释{get;set;}
}

为什么不使用类似ORM的实体框架或NHibernate?我对这些框架一无所知,这取决于您所做的工作,它们可能会为您节省大量时间和精力……这些框架很好,但并不总是合适的。你所做的看起来很简单,所以没有必要用带图钉的锤子。