I am working on a framework that I have been building/planning for a month now. I have started to develop it and need some expertise guidance on how to structure it, I think i am either designing it wrong or simply overcomplicating it.
So the main project at the moment is a class library and it is split into the following:
Framework, Core etc (These house all extension methods and other useful goodies)
BusinessEntities (All the entity framework Entities, including configuration for entities, using Fluent API)
BusinessLogicLayer DataAccessLayer
Now all entities inherit from BaseEntity which also inherits IValidableObject. BaseEntity looks like this:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace uk.BusinessEntities
{
public abstract class BaseEntity : IValidatableObject
{
public int PrimaryKey { get; set; }
public DateTime DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public abstract IEnumerable<ValidationResult> Validate(ValidationContext validationContext);
}
}
Then for the DataAccessLayer each class inherits the GenericObject class which looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
namespace uk.DataAccessLayer
{
public class GenericObject<T> where T : class
{
public GenericObject() { }
public static bool Add(T Entity, out IList<string> validationErrors)
{
using (var db = new DatabaseContext())
{
validationErrors = new List<string>();
try
{
db.Set<T>().Add(Entity);
db.SaveChanges();
return true;
}
catch (Exception ex)
{
InsertValidationErrors(ex, validationErrors);
return false;
}
}
}
public static IList<T> Retrieve()
{
using (var db = new DatabaseContext())
{
IList<T> Query = (IList<T>)(from x in db.Set<T>()
select x).ToList<T>();
return Query;
}
}
public static bool Update(T Entity, out IList<string> validationErrors)
{
validationErrors = new List<string>();
using (var db = new DatabaseContext())
{
try
{
db.Set<T>().Attach(Entity);
db.Entry(Entity).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return true;
}
catch (Exception ex)
{
InsertValidationErrors(ex, validationErrors);
return false;
}
}
}
public static bool Delete(T Entity, out IList<string> validationErrors)
{
validationErrors = new List<string>();
using (var db = new DatabaseContext())
{
try
{
db.Entry(Entity).State = System.Data.Entity.EntityState.Deleted;
db.SaveChanges();
return true;
}
catch(Exception ex)
{
InsertValidationErrors(ex, validationErrors);
return false;
}
}
}
protected static void InsertValidationErrors(Exception ex, IList<string> validationErrors)
{
validationErrors.Insert(0, ex.Message);
if (ex.InnerException != null)
{
validationErrors.Insert(0, ex.InnerException.Message);
validationErrors.Insert(0, ex.InnerException.StackTrace);
}
}
}
}
Now my main point lies all with validation of the entities. For example we got two entities Page and PageURLS URLS are stored separately for pages.
Now when adding a Page the PageURL also needs to be added as well, so if a developer called
PageDAL.AddPage(page, errors)
Would you also expect this method to also add the URL automatically or should this be a separate call?
Any help on this would be greatly appreciated.