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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.