BuisnessLogic MVC中的重复代码

时间:2015-10-21 17:28:36

标签: c# asp.net-mvc data-access-layer bll

我正在开发一个MVC解决方案,我可以使用Repository类解决我的DAL层,一切都运行良好。

但在我的BLL图层中,我有重复的代码:

我的Crud是一样的,我的领域和建议是不同的。 我也可以有一些额外的方法。

有没有办法以正确的方式解决这个问题?

第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

}    

第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


}

2 个答案:

答案 0 :(得分:1)

您可以创建通用基类

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);
    }
}

这假设JobJobType都实现了IJob或其他一些基类JobBase。或者你可以随时做where T : class

用法变为

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

您仍然可以覆盖基础BLL课程。然后覆盖或扩展必要的部分,而不是编写完整的实现。

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

答案 1 :(得分:0)

感谢您的回复。 我通过创建一个通用基类并从该类继承而解决了这个问题。

基础课程

 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
}

两个BLL类(以及BLL的其余部分)变得更轻,而不是重复。

BLL课程

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班是

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
    {}
}

我对这个解决方案唯一的问题是我不能在MVC中使用我的基类中的Entity字段。因此我创建了一个Field Job。这与控制器和HTML页面之间的绑定有关。

希望这段代码对其他人有很大的帮助。