English 中文(简体)
数据存取——设计应当负责创造储蓄的类别
原标题:Data Access Layer - Designing Class where should responsibility of creating saving be

我正在与ADO公司一道设计数据进入层。 2005年Sql服务器。 我常常与我的大脑一道,在什么地方发出这些呼吁。 Which way of the below i should follow for unable solid Code.

Method 1

Public Class Company
{

public string CompanyId
{get;set;}

public string CompanyAddress
{get;set;}

public bool Create()
{
}

public bool Update()
{
}

public bool Delete()
{
}

}

Method 2

Public Class Company
{

public string CompanyId
{get;set;}

public string CompanyAddress
{get;set;}
}

(一) 使用下文等另一类数据获取核心数据。 和

Public Class CompanyRepository
{

public Company CreateCompany(string companyId,string companyDescription)
{
}

public bool UpdateCompany(Company updateCompany)
{
}

public bool DeleteCompany(string companyId)
{
}

public List<Company> FindById(string id)
{
}


}
最佳回答

采用方法2。 从数据来源(_single responsibility principle上读/做不是公司类别的责任。 然而,我甚至想建立<条码>。 国际文件接口,然后创建<代码>CompanyRepository界面实施。 这样,你就可以将ICompany监管机构注入需要拯救/检索公司信息的类别。 该系统还便于进行单位测试,并能够在未来产生不同的执行(从数据库到Xml文档或任何东西)。

问题回答

If you follow the principle of separation of concerns, you will go with your method 2.

不同班级的职责不同,有助于制定可测试、可维持的法典。

这还会产生较小的、更有凝聚力的班级,便于书写、理性和检查正确性。

请注意,你可以使用ORM,而不是手手工艺你的数据检索层。

我赞成第二种选择,理由

  • first you create your data holder
  • after crate your operational unit

因此,在这种情况下,你将数据与在它们中运作的职能分开,制作<编码>Unit Testing,明显更容易和淡化你守则不同领域之间的责任,并可能容易地将数据本地化。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签