Search code examples
c#asp.net-mvcdata-access-layerbll

repetitive code in BuisnessLogic MVC


I'am working on a MVC solution, my DAL layer I could solve with a Repository classes, everything is working great.

But in my BLL layer I have repetitive code:

My Crud is the same, my fields and consructor are different. I can also have some extra methodes.

Is there a way to solve this on a proper way?

Class 1

public class JobTypeLogic
{
    #region Fields
    public JobType JobType { get; set; }
    private UnitOfWork unitOfWork = new UnitOfWork();
    public Repository<JobType> JobTypeEngine { get; set; }
    #endregion

    #region Constructor
    public JobTypeLogic()
    {
        JobType = new JobType();
        JobTypeEngine = unitOfWork.Repository<JobType>();
    }
    #endregion

    #region CRUD

    public void Add()
    {
        JobTypeEngine.Add(JobType);
    }

    public JobType Get(long id)
    {
        return JobType = JobTypeEngine.Get(id);
    }

    public void Edit()
    {
        JobTypeEngine.Edit(JobType);
    }

    public void Delete()
    {
        JobTypeEngine.Delete(JobType); 
    }

    public List<JobType> List()
    {
        return JobTypeEngine.List.ToList();
    }

    #endregion

}    

Class 2

public class JobLogic
{
    #region Fields
    public Job Job { get; set; }        
    public IEnumerable<SelectListItem> JobTypeList { get; set; }
    private UnitOfWork unitOfWork = new UnitOfWork();
    public Repository<Job> JobEngine;
    private Repository<JobType> JobTypeEngine;
    #endregion

    #region Constructor
    public JobLogic()
    {
        Job = new Job();
        JobEngine = unitOfWork.Repository<Job>();
        JobTypeEngine = unitOfWork.Repository<JobType>();
        JobTypeList = GetJobTypeList();
    }
    #endregion

    #region CRUD

    public void Add()
    {
        JobEngine.Add(Job);
    }

    public Job Get(long id)
    {
        return Job = JobEngine.Get(id);
    }

    public void Edit()
    {
        JobEngine.Edit(Job);
    }

    public void Delete()
    {
        JobEngine.Delete(Job);
    }

    public List<Job> List()
    {
        return JobEngine.List.ToList();
    }

    #endregion

    #region Methode

    private IEnumerable<SelectListItem> GetJobTypeList()
    {
        JobTypeEngine = unitOfWork.Repository<JobType>();   
        var jobs = JobTypeEngine.List
                    .Select(x =>
                            new SelectListItem
                            {
                                Value = x.ID.ToString(),
                                Text = x.Name
                            });

        return new SelectList(jobs, "Value", "Text");
    }

    #endregion


}

Solution

  • You could create a generic base class

    public class GenericJobLogic<T> where T : IJob
    {
        private Repository<T> engine;
    
        public GenericJobLogic()
        {
            this.engine = unitOfWork.Repository<T>();
        }
    
        public virtual T Get(long id)
        {
            return this.engine.Get(id);
        }
    }
    

    This assumes Job and JobType both implement IJob or some other base class JobBase. Or you could always just do where T : class.

    Usage becomes

    var jobBll = new GenericJobLogic<Job>();
    Job job = jobBll.Get(1);
    

    You can still override your base BLL class. Then override or extend only the necessary parts instead of writing a full implementation.

    public class JobLogic : GenericJobLogic<Job>
    {
        public override Job Get(long id) { }
        public IEnumerable<JobType> GetJobTypeList() { }
    }