The question is about conception.
I have simple class like this:
public class Data
{
string Server = "";
public Data(string Server)
{
this.Server = Server;
}
public class Items
{
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
public Item Get(int Id)
{
var result = new Item();
// here im am using sql connection to set result, but i need Server value in connnection string
return result;
}
}
}
It may not make sense but I explain what I want to achieve.
Right now i can make instance like this:
var data = new Data("0.0.0.0");
var item = new Data.Items().Get(3);
But the objects are not connected in any way.
And I would like to get/set data in this way:
var item = new Data("0.0.0.0").Items().Get(3);
or like this:
var data = new Data("0.0.0.0");
var item1 = data.Items().Get(3);
var item2 = data.Items().Save(3);
var item3 = data.OtherItems().Get(3);
In short, I would like to initiate the main class and pass a parameter that is required to execute methods in subclasses. Using methods in subclasses should not be possible if they are not called from the parent class.
Is it possible?
The goal is to determine once from which database instance the data come from without using static prop. And then calling individual methods without having to pass the name of the SQL instance.
Edit for a deeper explanation:
Right now i can write all the methods in main class like this.
public class Data
{
string Server = "";
public Data(string Server)
{
this.Server = Server;
}
public List<Item> Items_List() { return new List<Item>(); }
public Item Items_Get(Int32 id) { return new Item(); }
public void Items_Save(Item item) { }
public List<Firm> Firms_List() { return new List<Firm>(); }
public Firm Firms_Get(Int32 id) { return new Firm(); }
public void Firms_Save(Firm item) { }
public List<Location> Location_List() { return new List<Location>(); }
public Location Location_Get(Int32 id) { return new Location(); }
public void Location_Save(Location item) { }
}
Then i call like this:
var firms = data.Firms_List();
var firm = data.Firms_Get(firms[0].Id);
firm.Name = "New name";
data.Firms_Save(firm);
I want to change "_" to "." in method name for better visibility and for type grouping like this:
var firms = data.Firms.List();
var firm = data.Firms.Get(firms[0].Id);
firm.Name = "New name";
data.Firms.Save(firm);
Is this possible?
Maybe there is a way for grouping methods like above?
Or maybe idea is wrong.