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 關注
- 124 瀏覽
添加回答
舉報