1 回答
TA贡献1803条经验 获得超3个赞
我认为您应该分别为 User 和 Contact 类创建一个通用接口及其实现。如果出现一个新类,例如 Employee - 您将对此接口进行新的实现,而无需对 User 和 Contact 类进行任何更改。如果源不是二进制文件,而是数据库 - 那么该接口的单独实现。
如下:
interface IManager<TEntity> where TEntity : class
{
IList<TEntity> GetAll();
TEntity GetById(int id);
void Add(TEntity entity);
void Update(TEntity entity);
void Remove(int id);
int GenerateContactId();
IList<TEntity> Search(Func<TEntity, bool> p);
}
class BinaryContactManager : IManager<Contact>
{
public void Add(Contact entity)
{
throw new NotImplementedException();
}
public int GenerateContactId()
{
throw new NotImplementedException();
}
public IList<Contact> GetAll()
{
throw new NotImplementedException();
}
public Contact GetById(int id)
{
throw new NotImplementedException();
}
public void Remove(int id)
{
throw new NotImplementedException();
}
public IList<Contact> Search(Func<Contact, bool> p)
{
throw new NotImplementedException();
}
public void Update(Contact entity)
{
throw new NotImplementedException();
}
}
class BinaryUserManager : IManager<User>
{
public void Add(User entity)
{
throw new NotImplementedException();
}
public int GenerateContactId()
{
throw new NotImplementedException();
}
public IList<User> GetAll()
{
throw new NotImplementedException();
}
public User GetById(int id)
{
throw new NotImplementedException();
}
public void Remove(int id)
{
throw new NotImplementedException();
}
public IList<User> Search(Func<User, bool> p)
{
throw new NotImplementedException();
}
public void Update(User entity)
{
throw new NotImplementedException();
}
}
- 1 回答
- 0 关注
- 66 浏览
添加回答
举报