Reputation: 23
To store data I read from an XML file, i have created a data structure as displayed in the picture above. All boxes with a numbering 1 to n are instances of a class.
First question:
Each class that can hold multiple instances of other classes does so with a list element, e.g. FundamentalNamingConvention stores many UnitTypes in a
List<UnitType>
Is this a proper way of doing it?
Second question:
All classes (FundamentalNamingConvention, UnitType, UnitName) are only to be accessed by the NameGenerator class. To ensure this, I could simply make all these classes be inner classes of the NameGenerator but then that file would be several hundred lines of code long.
Is there a way to have the classes stored in their separate files (e.g. UnitType.cs) but still only be accessible by the NameGenerator?
Upvotes: 1
Views: 225
Reputation: 36629
Another way to control access to classes is by using projects. Create a new project with all the classes, make NameGenerator
public and all other classes internal
.
Using fine grained projects like this have various advantages and disadvantages. Many projects may reduce performance in various ways, and dependencies can be more cumbersome to manage. On the other hand it can improve code reuse since users can depend only on what they need.
Upvotes: 0
Reputation: 174795
C# allows you to split a class definition into multiple parts, by using the partial
keyword:
// Parent.cs
using System;
public partial class Parent
{
public static void WriteLine()
{
Console.WriteLine(Nested.Value);
}
}
// Nested.cs
using System;
public partial class Parent
{
private class Nested
{
public static int Value { get => 123; }
}
}
You'll find that Parent.WriteLine()
correctly prints the value 123
Upvotes: 0
Reputation: 17668
Each class that can hold multiple instances of other classes does so with a list element (e.g. FundamentalNamingConvention stores many UnitTypes in a List). Is this a proper way of doing it?
Yes - it is fine. Most likely any ICollection
, IList
or IEnumerable
would work. But List
is fine. Do note: List
does allow you to add elements as well. This is sometimes undesired.
All classes (FundamentalNamingConvention, UnitType, UnitName) are only to be accessed by the NameGenerator class. To ensure this, I could simply make all these classes be inner classes of the NameGenerator but then that file would be several hundred lines of code long.
Is there a way to have the classes stored in their separate files (e.g. UnitType.cs) but still only be accessible by the NameGenerator?
Yes, you can use a partial class to split the files. E.g.:
//file1.cs
public partial class Foo
{
//normal definition
}
//file1.cs
public partial class Foo
{
private class InnerBar
{
//private inner class definition
}
}
Upvotes: 1