C #: set property in constructor by method

I am starting to understand C # and OOP, and I have a problem that I can’t get around. I have a class called DataSet that should contain several properties (as System.Collections.Generic.Dictionary objects). Inside the class, I have methods that load data from the database, and these methods should be used to initialize the DataSet. Basically, I want the DataSet to have all the properties set when I create it from the Main method.

I have the following (without considering the details):

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 } 

I would like to have the properties defined in the constructor. My questions are mostly:

  • Am I doing everything right to do this?
  • If so, should I put my methods (and pass the instance using method references), why should the DataSet class be static (and all its properties) or is there a way to do what I do without a static DataSet?
  • An important issue is that Property3 can only be installed after Property1 is known / installed. I have no idea if this is possible ...

Any help is greatly appreciated.

+4
source share
5 answers

Am I doing everything right to do this?

You are not that far. I think many people confuse your Get functions like regular "getters", so you should rename them.

If so, should I make my methods static (and pass the instance a reference to the methods), which will require that the DataSet class be static (and all its properties), or is there a way to do what I do without a static DataSet?

You can force these methods to actually load static data, and you do not need to pass an instance - you can simply return the data. (I changed the name of the function). It is normal to call a static method from an instance / constructor, but not vice versa.

 public DataSet() { Property1 = LoadProperty1(); Property2 = LoadProperty2(); Property3 = LoadProperty3();//can only be done when Property1 is known } private static Dictionary<string, MyClass1> LoadProperty1() { // load data } 

An important issue is that Property3 can only be installed after Property1 is known / installed. I have no idea if this is possible ...

As you can see, this is solved in the code above.

0
source

There are several problems in the code. You made the properties public, so it really doesn't make sense to have GetProperty1 either. An alternative would be to make dictionary private variables, and then you can:

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

Similarly for property 3, you can also check that proeprty1 is not null before creating and returning it, but you will also need to decide what to do (automatically load property 1 first or return null for property3 otherwise)

+2
source

You can use the following code. He can solve several issues.

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

Why not add a public function, for example. LoadData (), which can be called after creating your object. It can load all your data using the correct order. I suppose you could call it in the constructor as well. I will also follow Lucos recommendations and make some private member variables with public properties.

0
source

I think you need to upload your dictionary and then want to cache it. You can do it lazily in getter:

 public class DataSet { private IDictionary<string, MyClass> property; public IDictionary<string, MyClass> Property { if (property == null) { property = LoadProperty(); } return property; } } 

or look in the constructor:

 public class DataSet { public IDictionary<string, MyClass1> Property { get; private set; } public DataSet() { Property = LoadProperty(); } } 

In addition, this method makes sense:

 private static Dictionary<string, MyClass1> GetProperty1Alternative(DataSet dataSet) 

Instead of calling it like this:

 DataSet.GetProperty1Alternative(anInstance); 

you can just do this:

 anIntance.Property; 
0
source

Source: https://habr.com/ru/post/1483314/


All Articles