Answer the question
In order to leave comments, you need to log in
How to avoid code repetition of repositories?
There are a lot of different entities. Here is the diagram
And you need to create repositories for these devils. Here is the native interface
interface IBaseService<T>:IDisposable
{
AccountModel Get<T>(int id) where T : class, new();
AccountModel GetAll<T>() where T : class, new();
bool Save<T>(AccountModel entity);
bool Delete(int id);
}
class AccountBaseService : IBaseService <AccountModel>
{
List<AccountModel> AllAccountList = new List<AccountModel>();
private AccountModel a;
VideoHosting.AccountModel IBaseService<VideoHosting.AccountModel>.Get<AccountModel>(int id)
{
foreach (var a in AllAccountList)
{
if (a.AccId == id)
{
return a;
}
return a;
}
return a;
}
AccountModel IBaseService<AccountModel>.GetAll<T>()
{
foreach (var a in AllAccountList)
{
return a;
}
return a;
}
bool IBaseService<AccountModel>.Save<T>(AccountModel entity)
{
entity = new AccountModel();
AllAccountList.Add(entity);
return true;
}
bool IBaseService<AccountModel>.Delete(int id)
{
foreach (var a in AllAccountList)
{
if (a.AccId == id)
{
AllAccountList.Remove(a);
}
}
return true;
}
public void Dispose()
{
throw new NotImplementedException();
}
}
Answer the question
In order to leave comments, you need to log in
interface IBaseService<T>:IDisposable where T: BaseEntity
{
AccountModel Get<T>(int id) where T : class, new();
AccountModel GetAll<T>() where T : class, new();
bool Save<T>(AccountModel entity);
bool Delete(int id);
}
class BaseEntity {
public int Id {get;set;}
}
public BaseService: IBaseService<T> {
protected List<T> Source = new List<T>();
public bool Delete(int id) {
foreach (var a in Source )
{
if (a.Id == id)
{
Source.Remove(a);
}
}
return true;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question