oop - C# class organization and lists -
I am working on the C # application which includes the objects department, course and section. Each department has several courses, And each course has many sections. Currently I have three classes: Department, Courses and Sections. There are some qualities in the department and then a list course, which includes courses provided in the curriculum. The curriculum has some properties and then there are a list section, in which the courses are included in the class. Is this a good way to organize the code or am I doing it in a different way?
Second, when I instruct a department in my application, then I set some properties and then want to start adding curriculum to the list courses defined in the department class, however, I can not do From the curriculum Application (course) from the application. What can I do within the department class so that I can break the object without any injunction principle in that list?
I have an example in this list:
class department {// ...... list & lt; Curriculum & gt; Course = New list & lt; Curriculum & gt; }
however the department. After starting the programs, the program is not available in the code (all the other class properties are available).
Install the internal course list immediately within the parameterized constructor of your class.
Private list & lt; Curriculum & gt; _coursesList; Public Department () {_coursesList = New list & lt; Curriculum & gt; (); }
In addition to this, another way to ensure encapsulation is to provide a method to add the curriculum to your department of class instead of highlighting the curriculum list. Public disorder AddCourse (Course C) {...} // Or (some composite method to add method to call method) Addsours (Course C) {. ..} // or Public Zero AddCource (string name, etc.) {...}
I think it is not a good idea in your case that directly exposes the list Because the class list can provide methods such as add and remove that could potentially create an invalid state on your parent class. So if you choose to manipulate internal collections as I have suggested, then you can expose an array of courses for your API client (remember to read the arrays only), so your API consumer side effects You will not be able to create your department on class.
public course [] course {get {return _coursesList.ToArray ()}}}
In addition, you also apply IEnumerable interface of your department class can do. This enables you to take advantage of all LINQ expansion methods available in C # 3.0.
I hope it helps, Carlos.
Comments
Post a Comment