BuisnessLogic MVC 中的重复代码
repetitive code in BuisnessLogic MVC
我正在研究 MVC 解决方案,我的 DAL 层可以用存储库解决 类,一切都很好。
但是在我的BLL层我有重复的代码:
我的Crud是一样的,我的fields和constructor不一样。
我也可以有一些额外的方法。
有什么好的方法可以解决这个问题吗?
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
}
您可以创建一个通用基础 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);
}
}
这假设 Job
和 JobType
都实现了 IJob
或其他一些基础 class JobBase
。或者你总是可以做 where T : class
.
用法变为
var jobBll = new GenericJobLogic<Job>();
Job job = jobBll.Get(1);
您仍然可以覆盖您的基本 BLL class。然后只覆盖或扩展必要的部分,而不是编写完整的实现。
public class JobLogic : GenericJobLogic<Job>
{
public override Job Get(long id) { }
public IEnumerable<JobType> GetJobTypeList() { }
}
感谢您的回复。
我通过创建一个通用基础 class 并从 class.
继承来解决了这个问题
基地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
}
两个 BLL classes(以及 BLL 的其余部分)变得更轻且不重复。
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 class是
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 中使用我的基础 class 中的实体字段。因此,我创建了一个 Field Job。这与控制器和 HTML 页面之间的绑定有关。
希望这段代码对其他人有很大帮助。
我正在研究 MVC 解决方案,我的 DAL 层可以用存储库解决 类,一切都很好。
但是在我的BLL层我有重复的代码:
我的Crud是一样的,我的fields和constructor不一样。 我也可以有一些额外的方法。
有什么好的方法可以解决这个问题吗?
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
}
您可以创建一个通用基础 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);
}
}
这假设 Job
和 JobType
都实现了 IJob
或其他一些基础 class JobBase
。或者你总是可以做 where T : class
.
用法变为
var jobBll = new GenericJobLogic<Job>();
Job job = jobBll.Get(1);
您仍然可以覆盖您的基本 BLL class。然后只覆盖或扩展必要的部分,而不是编写完整的实现。
public class JobLogic : GenericJobLogic<Job>
{
public override Job Get(long id) { }
public IEnumerable<JobType> GetJobTypeList() { }
}
感谢您的回复。 我通过创建一个通用基础 class 并从 class.
继承来解决了这个问题基地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
}
两个 BLL classes(以及 BLL 的其余部分)变得更轻且不重复。
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 class是
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 中使用我的基础 class 中的实体字段。因此,我创建了一个 Field Job。这与控制器和 HTML 页面之间的绑定有关。
希望这段代码对其他人有很大帮助。