C#:按方法在构造函数中设置属性

时间:2013-05-29 09:03:21

标签: c# properties constructor

我开始理解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
}

我想要的是在构造函数中设置属性。我的问题基本上是:

  1. 我正在以正确的方式做这件事吗?
  2. 如果是,我应该将我的方法设为静态(并通过引用方法传递实例),这将要求类DataSet是静态的(及其所有属性),或者是否有办法做我正在做的事情不使DataSet静态?
  3. 一个重要的问题是Property3只能在Property1已知/设置后设置。我不知道这是否可能......
  4. 非常感谢任何帮助。

5 个答案:

答案 0 :(得分:2)

您的代码中存在一些问题。您已将属性设为公共,因此拥有GetProperty1并没有多大意义。另一种方法是使字典成为私有变量,然后你可以:

public IDictionary<string,MyClass1> GetProperty1()
{
    if ( _property1 == null )
    {
        _property1 = LoadFromDatabase();
    }
    return _property1;
}

同样对于property3,你也可以在创建并返回之前检查proeprty1是否为null,但是你还需要决定做什么(首先自动加载属性1或者为property3返回null)

答案 1 :(得分:0)

您可以使用以下代码。它可以解决你的几个问题。

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;
        }
    }

答案 2 :(得分:0)

为什么不添加公共功能,例如在构造对象后可以调用的LoadData()。这可以使用正确的顺序加载您的所有数据。我想你甚至可以在构造函数中调用它。我也会遵循Lukos的建议,并使用公共获取属性制作一些私有成员变量。

答案 3 :(得分:0)

在我看来,你需要加载你的字典,然后想要缓存它。你可以在属性getter中懒洋洋地做这件事:

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;

答案 4 :(得分:0)

  

我正在以正确的方式做这件事吗?

你不是那么遥远。我想很多人都会将你的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
}
  

一个重要的问题是Property3只能在Property1已知/设置后设置。我不知道这是否可能......

如您所见,这已在上面的代码中解决。