I've created a simplified prototype solution as a sort of proof of concept before starting a larger program.
Here is the test data used to build the objects:
create table blah.dbo.tb_sandpitCreateMultipleLinkedObjects
(
CustomerName varchar(50),
ItemCode varchar(50)
);
insert into blah.dbo.tb_sandpitCreateMultipleLinkedObjects
values
('jq','bat'),
('jq','blackRubber'),
('jq','redRubber'),
('mt','redRubberX'),
('mt','redRubberY'),
('mt','redRubberZ'),
('mt','blackRubberA'),
('mt','blackRubberB'),
('mt','bladeFast'),
('mt','bladeVeryFast'),
('mt','bladeTooFast');
It has two concrete classes representing customers and items that the customers have bought. There are a few customers but potentially many items.
The only use case I have to address is:
An administrator identifies a user, then the system will display the items bought by that user. The system does not need to send any information back to SQL server.
Here are the two concrete classes:
class: Customer
using System;
using System.Collections.Generic;
namespace SandpitCreateMultipleLinkedObjects
{
class Customer
{
//links
List<Item> items;
Item i;
//property
public String Name { get; private set; }
//constructor
public Customer(String name)
{
items = new List<Item>();
Name = name;
}
//method to add link to item bought by customer
public void addItem(Item i)
{
items.Add(i);
}
}
}
class: Item
using System;
namespace SandpitCreateMultipleLinkedObjects
{
class Item
{
//name property
public String Name { get; private set; }
public Item(String name)
{
this.Name = name;
}
}
}
I have an orchestrating/coordinating class that ultimately will talk to the user interface:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace SandpitCreateMultipleLinkedObjects
{
class AppCoordinator
{
Dictionary<String,Customer> customers;
private string connString;
private const string targetTableName = @"blah.dbo.tb_sandpitCreateMultipleLinkedObjects";
private DataTable dt;
public AppCoordinator()
{
//this will contain links to the customers
customers = new Dictionary<string,Customer>();
//fill the main datatable
this.dt = fillDataTable();
//get a distinct view of the customers
//instantiate the customer objects
DataView view = new DataView(dt);
DataTable distinctValues = view.ToTable(true,new String[] { "CustomerName" });
foreach(DataRow row in distinctValues.Rows)
{
String custName = row["CustomerName"].ToString();
customers.Add(custName,new Customer(custName));
}
//then the instantiate the item objects
foreach(DataRow row in dt.Rows)
{
String custName = row["CustomerName"].ToString();
Customer linkedCustomer;
customers.TryGetValue(custName,out linkedCustomer);
String itemName = row["ItemCode"].ToString();
Item i = new Item(itemName);
linkedCustomer.addItem(i);
}
}
//gets data from the server
private DataTable fillDataTable()
{
this.connString = ConfigurationManager.ConnectionStrings["foo"].ConnectionString;
SqlConnection conn = null;
SqlDataAdapter ad = null;
DataSet ds = null;
DataTable returnDt = null;
try
{
conn = new SqlConnection(this.connString);
ad = new SqlDataAdapter("SELECT * FROM " + targetTableName,conn);
ds = new DataSet();
ad.Fill(ds,"AvailableReports");
returnDt = ds.Tables["AvailableReports"];
}
catch(SqlException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if(conn != null)
{
conn.Close();
conn.Dispose();
}
}
return returnDt;
}
//a read-only copy of the dictionary
//read-only for hiding purposes
public ReadOnlyDictionary<String,Customer> getCustomers()
{
return new ReadOnlyDictionary<String,Customer>(customers);
}
}
}
Here is Main
which resides in Program
:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace SandpitCreateMultipleLinkedObjects
{
class Program
{
private AppCoordinator appCoord;
static void Main(string[] args)
{
Program p = new Program();
p.appCoord = new AppCoordinator();
ReadOnlyDictionary<String,Customer> dics = p.appCoord.getCustomers();
Console.WriteLine("{0}",dics.Count);
foreach(String aName in dics.Keys)
{
Customer c;
dics.TryGetValue(aName,out c);
Console.WriteLine("{0} has this many items: {1}",c.Name, c.getItems().Count);
}
Console.WriteLine();
Console.WriteLine("press [enter] to exit");
Console.Read();
}
}
}
It works ok. I am new to OOP so any tips and pointers on this code would be appreciated.