C# Refactor Code

Below is a mock up of some code.  I have a PopulateModels method.  It is dynamically populating the models.  How do you refactor to remove the code from MyForm?

public class PersonModel
{
	public int PersonID { get; set; }
	public string FristName { get; set; }
	public string LastName { get; set; }
	public datetime DOB { get; set; }
}

public class CourseModel
{
	public int PersonID { get; set; }
	public int InstructorID { get; set; }
	public string CourseName { get; set; }
	public datetime CourseDate { get; set; }
}

public class MyForm
{
	private List<PersonModel> _person = new List<PersonModel>();
	private List<CourseModel> _courses = new List<CourseModel>();

	public MyForm()
	{
		PopulateModels();
	}
	
	private PopulateModels()
	{
		int count = 0;
		DateTime addDays = new DateTime(2017, 06, 01);

		_person.Add(new PersonModel()
		{
			PersonIDID = 1,
			FirstName = "John",
			FirstName = "Doe",
			DOB = DateTime(1992, 03, 21);
		});

		for (int i = 0; i < 10; i++)
		{
			_courses.Add(new CourseModel()
			{
				PersonIDID = 1,
				InstructorID = count++,
				CourseName = GetCourse(count);
				CourseDate = addDays.Date.AddDays(days)
			});


			if (count == 5)
				count = 1;

			count++;
			days += 3;
		}	
	}
	
	//More Methods
}

Open in new window

LVL 2
CipherISAsked:
Who is Participating?
 
AndyAinscowFreelance programmer / ConsultantCommented:
Add a data/business layer of some kind:

Public class MyData
{
	private List<PersonModel> _person = new List<PersonModel>();
	private List<CourseModel> _courses = new List<CourseModel>();

	public void PopulateModels()
	{
		int count = 0;
		DateTime addDays = new DateTime(2017, 06, 01);

		_person.Add(new PersonModel()
		{
			PersonIDID = 1,
			FirstName = "John",
			FirstName = "Doe",
			DOB = DateTime(1992, 03, 21);
		}

		for (int i = 0; i < 10; i++)
		{
			_courses.Add(new CourseModel()
			{
				PersonIDID = 1,
				InstructorID = count++,
				CourseName = GetCourse(count);
				CourseDate = addDays.Date.AddDays(days)
			});


			if (count == 5)
				count = 1;

			count++;
			days += 3;
		}	
	}

}

public class PersonModel
{
	public int PersonID { get; set; }
	public string FristName { get; set; }
	public string LastName { get; set; }
	public datetime DOB { get; set; }
}

public class CourseModel
{
	public int PersonID { get; set; }
	public int InstructorID { get; set; }
	public string CourseName { get; set; }
	public datetime CourseDate { get; set; }
}

public class MyForm
{
private MyData myData = new MyData();

	public MyForm()
	{
		myData.PopulateModels();
	}
	
	
	//More Methods
}

Open in new window

0
 
Robberbaron (robr)Commented:
you can then place the new Public classes into a new codefile to make viewing the Formcode easier.
0
 
ste5anSenior DeveloperCommented:
Important: This is not refactoring. Your changing the architecture. It's the S in SOLID. The single responsibility principle. A class should have only one responsibility.

Your original sample has two: Handling the UI and the data.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CipherISAuthor Commented:
Hence the refactoring to separate the two.
0
 
ste5anSenior DeveloperCommented:
Imho refactoring means improving code quality of existing code. Refactoring's should not create new classes. But for sure they don't create whole new layers.

The data layer proposed by Andy is a design pattern consisting of more than one common design pattern.

Design patterns are part of the software architecture. Whereas refactoring's are tools and techniques to develop/write actual code. They focus different aspects in the software development cycle.
1
 
CipherISAuthor Commented:
Similar to what I did.  Thx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.