[英]C#: Set property in constructor by method
我開始了解C#和OOP,並且遇到了一個我似乎無法解決的問題。 我有一個名為DataSet的類,該類應具有多個屬性(以System.Collections.Generic.Dictionary對象的形式)。 在該類中,我具有從數據庫加載數據的方法,這些方法應用於初始化DataSet。 基本上,當我從Main方法實例化它時,我希望DataSet對象具有所有屬性。
我所擁有的是以下內容(省略了細節):
public class DataSet
{
public IDictionary<string, MyClass1> Property1 { get; set; };
public IDictionary<string, MyClass2> Property2 { get; set; };
public IDictioanry<string, MyClass3> Property3 { get; set; };
public DataSet()
{
Property1 = new Dictionary<string, MyClass1>();
Property2 = new Dictionary<string, MyClass2>();
Property3 = new Dictionary<string, MyClass3>();
// Set Property1
// Set Property2
// Set Property3 (can only be done when Property1 is known)
}
private void GetProperty1(/* something? */)
{
// Loads the data from a database.
}
private static Dictionary<string, MyClass1> GetProperty1Alternative(DataSet dataSet)
{
// Same thing but static, so needs instance ref.
}
// Similarly for Property2 and Property3
}
我想要的是在構造函數中設置屬性。 我的問題基本上是:
任何幫助是極大的贊賞。
您的代碼中有一些問題。 您已將屬性公開,因此也沒有GetProperty1確實沒有任何意義。 另一種選擇是使字典成為私有變量,而不是這樣:
public IDictionary<string,MyClass1> GetProperty1()
{
if ( _property1 == null )
{
_property1 = LoadFromDatabase();
}
return _property1;
}
同樣,對於property3,您還可以在創建和返回proeprty1之前先檢查其是否為null,但同時還需要決定要做什么(首先自動加載property 1,否則為property3返回null)
您可以使用以下代碼。 它可以解決您幾個問題。
public class DataSet
{
private DataSet()
{
}
public DataSet _instance = null;
public static DataSet Instance
{
get{ if (_instance = null){_instance = new DataSet();}return _instance;}
}
private IDictionary<string, MyClass1> _property1 = null;
public IDictionary<string, MyClass1> Property1
{
get
{
result = _property;
if (result == null)
{
//read database
}
return result;
}
}
為什么不添加一個公共函數,例如LoadData(),就可以在構造對象之后調用它。 這樣可以使用正確的順序加載所有數據。 我想您甚至可以在構造函數中調用它。 我還將遵循Lukos的建議,並使用public get屬性創建一些私有成員變量。
在我看來,您需要加載字典,然后再對其進行緩存。 您可以在屬性獲取器中懶惰地執行此操作:
public class DataSet
{
private IDictionary<string, MyClass> property;
public IDictionary<string, MyClass> Property
{
if (property == null)
{
property = LoadProperty();
}
return property;
}
}
或渴望在構造函數中:
public class DataSet
{
public IDictionary<string, MyClass1> Property { get; private set; }
public DataSet()
{
Property = LoadProperty();
}
}
同樣,使用此方法沒有多大意義:
private static Dictionary<string, MyClass1> GetProperty1Alternative(DataSet dataSet)
而不是這樣稱呼:
DataSet.GetProperty1Alternative(anInstance);
您可以簡單地做到這一點:
anIntance.Property;
我在做什么是正確的方式嗎?
距離您不遠。 我認為許多人將您的Get
函數混淆為常規的“ getter”,因此您應該對其進行重命名。
如果是,我應該將我的方法設為靜態(並通過引用該方法傳遞實例),這將要求DataSet類為靜態(及其所有屬性),還是可以在不使DataSet不變的情況下做我正在做的事情靜態的?
您可以將實際加載數據的方法設為靜態,而無需傳遞實例-您只需返回數據即可。 (我更改了功能名稱)。 從實例/構造函數中調用靜態方法很好,但反之則不行。
public DataSet()
{
Property1 = LoadProperty1();
Property2 = LoadProperty2();
Property3 = LoadProperty3();//can only be done when Property1 is known
}
private static Dictionary<string, MyClass1> LoadProperty1()
{
// load data
}
一個重要的問題是,只有在知道/設置Property1后才能設置Property3。 我不知道那是否可能...
如您所見,以上代碼已解決了這一問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.