C# - Entity handler correct use clean code
I have a question about setting up my service. My main concern is if the design is clean and in order. Whether it meets the SOLID principles and does not spill out on me. I have entities like order and item. These entities will be added. Each entity has a different structure. However, these entities need all the same methods - store in database, download from storage, calculate correctness, delete, etc. separately, the entity should not be extensible. Entities are then further divided into import and export. This is my idea: IBaseEntityHandler public interface IBaseEntityHandler { EntityType EntityType { get; set; } Task SaveToStorageAsync(string filePath); Task LoadFromStorageAsync(string filePath); Task CalculateAsync(); Task SaveToDatanodeAsync(); ....... } BaseEntityHandler public abstract class BaseEntityHandler : IBaseEntityHandler { private readonly IDatabase _database; private readonly IStorage _storage; EntityType EntityType { get; set; } Task SaveToStorageAsync(string filePath) { _storage.SaveAsync(filePath); } Task LoadFromStorageAsync(string filePath) { _storage.Load(filePath); } Task SaveToDatabaseAsync() { _database.Save(); } Task CalculateAsync() { await CalculateAsyncInternal(); } abstract Task CalculateAsyncInternal(); } BaseImportEntityHandler public abstract class BaseImportEntityHandler : BaseEntityHandler { abstract Task SomeSpecial(); } OrderHandler public class OrderHandler : BaseImportEntityHandler { public EntityType EntityType { get; set; } = EntityType.Order; public async Task CalculateAsyncInternal() { } public async Task SomeSpecial() { } } EntityHandlerFactory public class EntityHandlerFactory { public static IBaseEntityHandler CreateEntityHandler(EntityType entityType) { switch (entityType) { case EntityType.Order: return new OrderHandler() as IBaseEntityHandler; default: throw new NotImplementedException($"Entity type {entityType} not implemented."); } } } My question. Is it okay to use inheritance instead of folding here? Each entity handler needs to have the same methods implemented. If there are special ones - import/export, they just get extended, but the base doesn't change. Thus it doesn't break the idea of inheritance. And the second question is this proposal ok? Thank you
I have a question about setting up my service. My main concern is if the design is clean and in order. Whether it meets the SOLID principles and does not spill out on me.
I have entities like order and item. These entities will be added. Each entity has a different structure. However, these entities need all the same methods - store in database, download from storage, calculate correctness, delete, etc. separately, the entity should not be extensible. Entities are then further divided into import and export.
This is my idea:
IBaseEntityHandler
public interface IBaseEntityHandler {
EntityType EntityType { get; set; }
Task SaveToStorageAsync(string filePath);
Task LoadFromStorageAsync(string filePath);
Task CalculateAsync();
Task SaveToDatanodeAsync();
.......
}
BaseEntityHandler
public abstract class BaseEntityHandler : IBaseEntityHandler {
private readonly IDatabase _database;
private readonly IStorage _storage;
EntityType EntityType { get; set; }
Task SaveToStorageAsync(string filePath) {
_storage.SaveAsync(filePath);
}
Task LoadFromStorageAsync(string filePath) {
_storage.Load(filePath);
}
Task SaveToDatabaseAsync() {
_database.Save();
}
Task CalculateAsync() {
await CalculateAsyncInternal();
}
abstract Task CalculateAsyncInternal();
}
BaseImportEntityHandler
public abstract class BaseImportEntityHandler : BaseEntityHandler {
abstract Task SomeSpecial();
}
OrderHandler
public class OrderHandler : BaseImportEntityHandler {
public EntityType EntityType { get; set; } = EntityType.Order;
public async Task CalculateAsyncInternal() {
}
public async Task SomeSpecial() {
}
}
EntityHandlerFactory
public class EntityHandlerFactory {
public static IBaseEntityHandler CreateEntityHandler(EntityType entityType) {
switch (entityType) {
case EntityType.Order:
return new OrderHandler() as IBaseEntityHandler;
default:
throw new NotImplementedException($"Entity type {entityType} not implemented.");
}
}
}
My question. Is it okay to use inheritance instead of folding here? Each entity handler needs to have the same methods implemented. If there are special ones - import/export, they just get extended, but the base doesn't change. Thus it doesn't break the idea of inheritance. And the second question is this proposal ok?
Thank you