patterncsharpMinor
JobTypeLogic and JobType classes in MVC business logic
Viewed 0 times
mvcjobtypelogicbusinesslogicjobtypeclassesand
Problem
I am working on an MVC solution. My DAL layer I could solve with 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 methods.
Is there a way to solve this properly?
Class 1
Class 2
```
public class JobLogic
{
#region Fields
public Job Job { get; set; }
public IEnumerable JobTypeList { get; set; }
private UnitOfWork unitOfWork = new UnitOfWork();
public Repository JobEngine;
private Repository JobTypeEngine;
#endregion
#region Constructor
public JobLogic()
{
Job = new Job();
JobEngine = unitOfWork.Repository();
JobTypeEngine = unitOfWork.Repository();
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 List()
{
return JobEngine.List.ToList();
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 methods.
Is there a way to solve this properly?
Class 1
public class JobTypeLogic
{
#region Fields
public JobType JobType { get; set; }
private UnitOfWork unitOfWork = new UnitOfWork();
public Repository JobTypeEngine { get; set; }
#endregion
#region Constructor
public JobTypeLogic()
{
JobType = new JobType();
JobTypeEngine = unitOfWork.Repository();
}
#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 List()
{
return JobTypeEngine.List.ToList();
}
#endregion
}Class 2
```
public class JobLogic
{
#region Fields
public Job Job { get; set; }
public IEnumerable JobTypeList { get; set; }
private UnitOfWork unitOfWork = new UnitOfWork();
public Repository JobEngine;
private Repository JobTypeEngine;
#endregion
#region Constructor
public JobLogic()
{
Job = new Job();
JobEngine = unitOfWork.Repository();
JobTypeEngine = unitOfWork.Repository();
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 List()
{
return JobEngine.List.ToList();
Solution
This feels bad to me:
This begs for IoC/DI, otherwise I cannot see how you'd test your code.
private UnitOfWork unitOfWork = new UnitOfWork();
public JobTypeLogic()
{
JobTypeEngine = unitOfWork.Repository();
}This begs for IoC/DI, otherwise I cannot see how you'd test your code.
JobTypeEngine.List.ToList();: I don't know what's worse, that you've got a property named List, or that it apparently isn't a List at all.public IEnumerable JobTypeList { get; set; }: Does a SelectListItem belong in the BLL? Why isn't that a JobType instead, and why aren't you calling it JobTypes (especially since it is an IEnumerable and not a List)?GetJobTypeList() doesn't seem to be used. It also again assigns JobTypeEngine even though this already happened in the JobLogic constructor.Code Snippets
private UnitOfWork unitOfWork = new UnitOfWork();
public JobTypeLogic()
{
JobTypeEngine = unitOfWork.Repository<JobType>();
}Context
StackExchange Code Review Q#108364, answer score: 2
Revisions (0)
No revisions yet.