-1

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


}
Kris Martele
  • 29
  • 1
  • 7

2 Answers2

1

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() { }
}
Jasen
  • 14,030
  • 3
  • 51
  • 68
0

Thanks for the response. I solved the problem by creating a generic base class and inherited from that class.

Base class

 public class GenericLogic<T> where T : BaseEntity
{
    private Repository<T> engine;
    private UnitOfWork unitOfWork = new UnitOfWork();
    public T Entity;

    public GenericLogic()
    {
        this.engine = unitOfWork.Repository<T>();
    }        
    #region CRUD

    public void Add()
    {
        engine.Add(Entity);
    }
    public T Get(long id)
    {}
    public void Edit()
    {}
   public void Delete()
    {}
    public List<T> List()
    {}
    #endregion
}

The two BLL classes (and the rest of the BLL) become lighter and not repetitive.

BLL class

public class JobLogic : GenericLogic<Job>
{
    #region Fields
    public Job Job { get; set; }    
    public IEnumerable<SelectListItem> JobTypeList { get; set; }
    #endregion

    #region Constructor
    public JobLogic()
    {
        Job = new Job();
        JobTypeList = GetJobTypeList();
    }
    #endregion

    #region Methode
    private IEnumerable<SelectListItem> GetJobTypeList()
    {
        UnitOfWork unitOfWork = new UnitOfWork();
        Repository<JobType> 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
}

DAL classes is

public class Repository<T> where T : BaseEntity
{
    private readonly FlowContext context;
    private IDbSet<T> entities;
    string errorMessage = string.Empty;

    public Repository(FlowContext context)
    {
        this.context = context;
    }

    public T Get(object id)
    {}

    public void Add(T entity)
    {}

    public void Edit(T entity)
    {}

    public void Delete(T entity)
    {}

    private IDbSet<T> Entities
    {}
}

The only problem I have with this solution is that I can't use the Entity field from my base class in MVC. Therefore I created a Field Job. This has todo with the binding between the controller and the HTML page.

Hopefully is this code a great help for other people.

Kris Martele
  • 29
  • 1
  • 7