Ho la seguente classe:
internal class LeaveRequest : ServiceBase
{
private const string InvalidRequestMessage = "Specified Request does not exist";
private const string InvalidApproverMessage = "You are not an approver for the specified user";
private const string RequestWrongStatusMessage = "The request status cannot be changed due to the current status";
private const string CancelRequestIncludes = "LeaveRequestStatuses";
internal LeaveRequest(DataAccess.BookItContext context) : base(context) { }
private static IQueryable<Model.LeaveRequest> ProcessIncludeProperties(string includeProperties,
IQueryable<Model.LeaveRequest> query)
{
foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
return query;
}
private static IQueryable<Model.LeaveRequest> ProcessFilter(Expression<Func<Model.LeaveRequest, bool>> filter,
IQueryable<Model.LeaveRequest> query)
{
if (filter != null)
{
query = query.Where(filter);
}
return query;
}
private ICollection<Model.LeaveRequest> Find(Expression<Func<Model.LeaveRequest, bool>> filter = null,
string includeProperties = "")
{
var requestQuery = Context.LeaveRequests.AsQueryable();
requestQuery = ProcessFilter(filter, requestQuery);
requestQuery = ProcessIncludeProperties(includeProperties, requestQuery);
return requestQuery.ToList();
}
private Model.LeaveRequest FindByID(int id,
string includeProperties = "")
{
var requestQuery = Context.LeaveRequests.AsQueryable();
requestQuery = ProcessFilter(r => r.ID == id, requestQuery);
requestQuery = ProcessIncludeProperties(includeProperties, requestQuery);
return requestQuery.FirstOrDefault();
}
private Model.LeaveRequestStatus CreateStatus(RequestStatuses status,
int approverID,
string notes,
DateTime editDate)
{
return new Model.LeaveRequestStatus()
{
IsCurrent = true,
Notes = notes,
StatusID = (int)status,
UserID = approverID,
StatusDate = editDate
};
}
private void SetNewRequestStatus(Model.LeaveRequest request,
Model.LeaveRequestStatus currentStatus,
RequestStatuses status,
int approverID,
string notes,
DateTime editDate)
{
currentStatus.IsCurrent = false;
Context.Entry(currentStatus).State = EntityState.Modified;
var newStatus = CreateStatus(status, approverID, notes, editDate);
request.LeaveRequestStatuses.Add(currentStatus);
Context.Entry(currentStatus).State = EntityState.Added;
}
internal OperationResult CancelRequest(int requestID, int userID, string notes)
{
var result = new OperationResult() { Success = true };
DateTime editDate = DateTime.UtcNow;
var request = this.FindByID(requestID, CancelRequestIncludes);
if (request == null)
{
throw new ArgumentException(InvalidRequestMessage);
}
var currentStatus = request.LeaveRequestStatuses.Where(s => s.IsCurrent).FirstOrDefault();
if (currentStatus.StatusID == (int)RequestStatuses.RequestPending || currentStatus.StatusID == (int)RequestStatuses.RequestApproved)
{
if (currentStatus.StatusID == (int)RequestStatuses.RequestPending)
{
SetNewRequestStatus(request, currentStatus, RequestStatuses.CancellationApproved, userID, notes, editDate);
}
else if (currentStatus.StatusID == (int)RequestStatuses.RequestApproved)
{
var approver = new Approver(Context);
if (approver.Validate(request.UserID, userID))
{
SetNewRequestStatus(request, currentStatus, RequestStatuses.CancellationPending, userID, notes, editDate);
}
else
{
throw new InvalidApproverException(InvalidApproverMessage);
}
Context.SaveChanges();
}
}
else
{
result.Success = false;
result.Message = RequestWrongStatusMessage;
}
return result;
}
}
Usa EF per accedere ai dati. La mia domanda dovrebbe essere divisa in due classi?
Sto iniziando a pensare che forse dovrebbe essere diviso in una classe per gestire le varie convalide (regole di business) e un'altra per fare effettivamente la creazione del grafico dell'oggetto corretto e impostare lo stato dell'entità corretta e fare le modifiche di salvataggio. La mia unica preoccupazione per la suddivisione in due classi, potrei finire con vari metodi "pass-through".
Inoltre, se divido in due classi, non sono sicuro se dovrei utilizzare due spazi dei nomi, ad es. Business.LeaveRequest e Service.LeaveRequest o se dovrei nominare le classi LeaveRequest e LeaveRequestDB.