我有一个customers类,它定义了属性和方法。目前,它包含与客户关联的任何类型任务的方法。例如,它包含一个方法"InsertOrUpdateCustomer“。此方法可以将新的客户记录插入数据库,也可以方便编辑现有的客户记录。
这个类还包含一些客户字段的验证方法。
我认为这不是一个更好的办法。我想把它弄成这样:
interface ICustomer
{
string CustomerName;
date FinancialYearStartDate;
date FinancialYearEndDate;
string TaxNo;
string Address;
}我想将这个接口实现到另一个类,比如Customers:
class Customers: ICustomer
{
// Properties
CustomerName { get; set; }
FinancialYearStartDate { get; set; }
FinancialYearEndDate { get; set; }
TaxNo { get; set; }
Address { get; set; }
// Constructor
}我想知道:
发布于 2010-11-26 08:39:55
总的来说,我认为每个类都有一个单一责任是个好主意,例如业务状态Customer、持久存储NHibernateCustomerRepository : ICustomerRepository和验证CustomerValidator。
存储库的一个示例:
interface ICustomerRepository
{
// Get by id
Customer Get(int id);
void Delete(Customer customer);
IList<Customer> GetAll();
// creates a new instance in datastore
// returns the persistent identifier
int Save(Customer customer);
// updates if customer exists,
// creates if not
// returns persistent identifier
int SaveOrUpdate(Customer customer);
// updates customer
void Update(Customer customer);
// specific queries
IList<Customer> GetAllWithinFiscalYear(DateTime year);
// ...
}如您所见,这个接口的第一种方法对于大多数业务实体来说都是相似的,可以抽象为:
interface IRepository<TId, TEntity>
{
// Get by id
TEntity Get(TId id);
void Delete(TEntity entity);
IList<TEntity> GetAll();
// creates a new instance in datastore
// returns the persistent identifier
TId Save(TEntity entity);
// updates if customer exists,
// creates if not
// returns persistent identiefier
TId SaveOrUpdate(TEntity entity);
// updates customer
void Update(TEntity entity);
}
interface ICustomerRepository : IRepository<int, Customer>
{
// specific queries
IList<Customer> GetAllWithinFiscalYear(DateTime year);
}发布于 2010-11-26 08:35:54
我的回答是:
发布于 2010-11-26 08:35:11
像'InsertOrUpdateCustomer‘这样的动作通常是客户实体服务的一部分(在适配器模型中)(如您建议的那样,在另一个类中)。
思考这个问题的方法是:“谁有责任拯救客户?”
一种可能是将ICustomerValidator“注入”到保存方法中。
https://stackoverflow.com/questions/4283624
复制相似问题