I have two base classes: City and Building:
public class City
{
public string Name { get; set; }
public double Area { get; set; }
}
public class Building
{
public string Name { get; set; }
public double Area { get; set; }
public int Stories { get; set; }
}
With two classes that inherit from Building, with their own Id property:
public class MyBuilding : Building
{
public int MyId { get; set; }
}
public class HisBuilding : Building
{
public int HisId { get; set; }
}
And two classes that inherit from City, with their own Id and IEnumerable<Building> properties:
public class MyCity : City
{
public int MyId { get; set; }
public IEnumerable<MyBuilding> Buildings { get; set; }
}
public class HisCity : City
{
public int HisId { get; set; }
public IEnumerable<HisBuilding> Buildings { get; set; }
}
Below is a method I've created that returns a KeyValuePair<City, Building> using generics:
public static KeyValuePair<TCity, IEnumerable<TBuilding>> GetData<TCity, TBuilding>()
where TCity : City, new()
where TBuilding : Building, new()
{
TCity city = new TCity();
IEnumerable<TBuilding> buildings = new List<TBuilding>();
return new KeyValuePair<TCity, IEnumerable<TBuilding>>(city, buildings);
}
And in my Main method I've logic that instantiates a specific City object based on a user's input.
int input = -1;
string strInput = Console.ReadLine();
int.TryParse(strInput, out input);
string json = string.Empty;
switch (input)
{
case 1:
KeyValuePair<MyCity, IEnumerable<MyBuilding>> myCity
= GetData<MyCity, MyBuilding>();
MyCity my = myCity.Key;
my.MyId = 1;
my.Buildings = myCity.Value;
json = JsonConvert.SerializeObject(my, Formatting.Indented);
break;
default:
KeyValuePair<HisCity, IEnumerable<HisBuilding>> hisCity
= GetData<HisCity, HisBuilding>();
HisCity his = hisCity.Key;
his.HisId = 2;
his.Buildings = hisCity.Value;
json = JsonConvert.SerializeObject(his, Formatting.Indented);
break;
}
Console.WriteLine(json);
Console.ReadLine();
The above generic method is just something I cobbled together in the last half hour, but I'm wondering if there is a more efficient way of creating these custom objects of mine, especially when each City has its own, different IEnumerable<Building> property.
EDIT
Here is the code I have for building the IEnumerable inside my GetData method:
public static IEnumerable<TBuilding> GetBuildingData<TBuilding>()
where TBuilding : Building, new()
{
DataTable table;
string myConnectionString = @"Data Source=(LocalDb)\MSSQLLocalDb;Initial Catalog=Test;Integrated Security=True;";
List<TBuilding> buildings = new List<TBuilding>();
using (SqlConnection connection = new SqlConnection(myConnectionString))
{
using (SqlCommand command = new SqlCommand("[dbo].[GetListData]", connection) { CommandType = CommandType.StoredProcedure })
{
connection.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
table = new DataTable();
adapter.Fill(table);
}
connection.Close();
}
}
foreach (DataRow row in table.Rows)
{
TBuilding building = new TBuilding();
building.Area = Convert.ToDouble(row["Area"]);
building.Name = row["Name"].ToString();
building.Stories = Convert.ToInt32(row["Stories"]);
buildings.Add(building);
}
return buildings;
}
Idproperties are client-specific, so I'm working to build out the base objects generically, then add the custom ids later on. \$\endgroup\$