I am new to using interfaces and abstract classes. The team I am working with came up with an implementation of what they call a base class that will be implemented by all view model classes. This is what they came up with:
public interface IBaseViewModel<T>
{
#region public properties
string Message { get; set; }
string SaveType { get; set; }
bool IsVisibleMessage { get; set; }
bool IsVisibleDecision { get; set; }
bool IsApiSuccess { get; set; }
Guid EntityId { get; set; }
string EntityQDSId { get; set; }
string EntityType { get; set; }
#endregion
#region public methods
Task Init();
Task Init(Guid Id);
Task<T> Create();
Task<T> Get(Guid Id);
Task<T> Get(string Id);
Task<IEnumerable<T>> GetList(Guid Id);
Task<IEnumerable<T>> GetList(string Id);
Task<T> Insert(T t);
Task<T> Update(Guid Id, T t);
Task<T> Delete(Guid Id);
void Dispose();
#endregion
}
public abstract class BaseViewModel<T> : IBaseViewModel<T>, IDisposable
{
public Guid EntityId { get; set; }
public string EntityQDSId { get; set; }
public string EntityType { get; set; }
public string Message { get; set; }
public string SaveType { get; set; }
public bool IsVisibleMessage { get; set; }
public bool IsVisibleDecision { get; set; }
public bool IsApiSuccess { get; set; }
public virtual Task Init()
{
return Task.FromResult<T>(default);
}
public virtual Task Init(Guid Id)
{
return Task.FromResult<T>(default);
}
public virtual Task<T> Create()
{
return Task.FromResult<T>(default);
}
public virtual Task<T> Get(Guid Id)
{
return Task.FromResult<T>(default);
}
public virtual Task<T> Get(string Id)
{
return Task.FromResult<T>(default);
}
public virtual Task<IEnumerable<T>> GetList(Guid Id)
{
return Task.FromResult<IEnumerable<T>>(default);
}
public virtual Task<IEnumerable<T>> GetList(string Id)
{
return Task.FromResult<IEnumerable<T>>(default);
}
public virtual Task<IEnumerable<T>> GetForGrid(Guid Id)
{
return Task.FromResult<IEnumerable<T>>(default);
}
public virtual Task<T> Insert(T t)
{
return Task.FromResult<T>(default);
}
public virtual Task<T> Update(Guid Id, T t)
{
return Task.FromResult<T>(default);
}
public virtual Task<T> Delete(Guid Id)
{
return Task.FromResult<T>(default);
}
public virtual void ProcessDto(T t)
{
}
public virtual void Dispose()
{
}
}
Below is the implementation:
public interface IAccountBillViewModel<T> : IBaseViewModel<AccountBillGroup>
{
}
public class AccountBillViewModel<T> : BaseViewModel<AccountBillGroup>, IAccountBillViewModel<AccountBillGroup>
{
}
In researching this question I found several comments like the one below
https://stackoverflow.com/a/479154 [@Alex][1]
My points:
- Since there was only code signatures and no implementations of any code in the abstract class and because you can inherit from multiple interfaces, why use an abstract class instead of a interface?
- The inheritance slot will be left open in case we wanted to inherit from another class in the future.
Their points:
- The properties only need to be implemented in the base class and those properties will be available in all implementations without declaring the properties again.
- Only those methods that need to be used in the implementation need to be overridden. If an interface was used all the methods would have to be implemented in every implementation.
Is there value in choosing to go with an abstract base class when there is not implementations, only method signaatures?