Say i have a base class, called BaseService, then i have another class called AuditService, which inherits from BaseService, and maybe i have another class called FooService that also inherits from BaseService.
So hierarchy looks like this
BaseService
|
---- AuditService
|
---- FooService
Now what if i need a new service that contains all the features of AuditService and FooService?
c# doesnt allow you to inherit from multiple objects, so how would i go about this?
I dont want to have to inherit from 1 service and re-code all the rest of the items again.
EDIT:
To include a few more details about my classes, some code below.
As you can see i override particular functions from base service in the foo service and auditable service. In future i will need a service that implements all functionality of baseservice, foo service and auditable service.
BASESERVICE
public abstract class BaseService<TEntity>
where TEntity : class, IBaseEntity
{
protected DataContext _context;
protected BaseService(DataContext context)
{
_context = context;
}
public virtual async Task<ICollection<TEntity>> GetAllAsync()
{
return await _context.Set<TEntity>().ToListAsync();
}
public virtual Task<TEntity> GetAsync(long id)
{
return _context.Set<TEntity>().FindAsync(id);
}
public virtual Task<int> AddAsync(TEntity t)
{
if (_context.Entry(t).State == EntityState.Detached)
{
_context.Set<TEntity>().Add(t);
}
_context.Entry(t).State = EntityState.Added;
return _context.SaveChangesAsync();
}
public virtual Task<int> AddAllAsync(ICollection<TEntity> all)
{
foreach (var item in all)
{
if (_context.Entry(item).State == EntityState.Detached)
{
_context.Set<TEntity>().Add(item);
}
_context.Entry(item).State = EntityState.Added;
}
return _context.SaveChangesAsync();
}
public virtual Task<int> UpdateAsync(TEntity updated)
{
_context.Entry(updated).State = EntityState.Modified;
return _context.SaveChangesAsync();
}
public virtual async Task<int> DeleteAsync(long key)
{
TEntity entity = await GetAsync(key);
_context.Entry(entity).State = EntityState.Deleted;
return await _context.SaveChangesAsync();
}
public virtual Task<int> DeleteAsync(TEntity t)
{
_context.Entry(t).State = EntityState.Deleted;
return _context.SaveChangesAsync();
}
}
AUDITABLESERVICE
public class AuditableService<TEntity> : BaseService<TEntity>
where TEntity : class, IAuditableEntity
{
public virtual override Task<int> DeleteAsync(long key)
{
return DeleteAsync(key, true);
}
public virtual async Task<int> DeleteAsync(long key, bool useFlag)
{
TEntity entity = await GetAsync(key);
return await DeleteAsync(entity, useFlag);
}
public virtual override Task<int> DeleteAsync(TEntity t)
{
return DeleteAsync(t, true);
}
public virtual Task<int> DeleteAsync(TEntity t, bool useFlag)
{
if (useFlag)
{
// flag item as deleted
t.IsDeleted = true;
return UpdateAsync(t);
}
else
{
return base.DeleteAsync(t);
}
}
}
FOOSERVICE
public class FooService<TEntity> : BaseService<TEntity>
where TEntity : class, IBaseEntity
{
protected IValidator<TEntity> _validator;
protected ValidatorService(DataContext context)
: base(context)
{
}
public override async Task<int> AddAsync(TEntity t)
{
var results = await _validator.ValidateAsync(t);
if (results.IsValid)
{
return await base.AddAsync(t);
}
else
{
throw new ValidationException(results.Errors);
}
}
public override async Task<int> UpdateAsync(TEntity updated)
{
var results = await _validator.ValidateAsync(updated);
if (results.IsValid)
{
return await base.UpdateAsync(updated);
}
else
{
throw new ValidationException(results.Errors);
}
}
}