简体   繁体   English

将数据加载到通用类C#的对象中

[英]Load data in object of Generic class C#

I want to have an instance of a generic class and get data loaded in that instance. 我想要一个通用类的实例,并在该实例中加载数据。 The sample code is given as under: 示例代码如下:

public class AuthorCollector : EvoObjectCollector<Author>
{
   static ConnectionClass _cs = new ConnectionClass();
   SqlConnection _con;
   public AuthorCollector()
   {
      _con = _cs.GetConnection();
   }

   public override List<Author> CollectAuthors(int _startYear, int _endYear)
   {  
      List<Author> _eAthors = new List<Author>();
      // ... method implementation...  
      // ... using SqlDataReader
      return _eAuthors;  
   }  
}  

and in the main class 在主要班级

public class Test  
{
   public static void Main(String[] _args)
   {
      int _ITERATIONS = 100;
      // EvoNetwork main class
      EvoNetwork<Author> _mEvoNetwork = new EvoNetwork<Author>(/*constructor properties*/);

      // Create a collector which collects Author from DB. 
      EvoObjectCollector<Author> _mCollector = new AuthorCollector();  

      // Create a converter which converts Author into EvoObject.
      EvoObjectConverter<Author> _mConverter = new AuthorPCVConverter();  

      var _result = _mEvoNetwork.Build(_mCollector, _mConverter, _ITERATIONS);

      // ... rest of the implementation...  
   }
}  

I want to have data in _mCollector ie object of EvoObjectCollector<Author> is created. 我想在_mCollector拥有数据,即创建了EvoObjectCollector<Author>对象。
Should I put CollectAuthors() method of class AuthorCollector() inside constructor to get data as object of class is created? 我应该在创建类对象时将CollectAuthors()类的AuthorCollector()放入构造函数中以获取数据吗?

No you should not use a overrideable method in the constructor. 不,您不应在构造函数中使用可重写的方法。 instead use constructor injection or call a initialize method afterwards. 而是使用构造函数注入或之后调用initialize方法。 This could be abstracted by using a factory which calls the initialize method and returns the initialized object. 这可以通过使用factory来抽象,该factory调用initialize方法并返回初始化的对象。

Another possiblity would also be to use the _startYear and the _endYear as input parameters for the constructor and call a private method which initializes the object. 另一个可能的方法是使用_startYear_endYear作为构造函数的输入参数,并调用用于初始化对象的私有方法。

You could change your design like followed: 您可以按照以下方式更改设计:

public abstract class EvoObjectCollector<T> { 

    protected List<T> collection; 

    // Instead of the startYear and the endYear a CollectionContext could be added
    public abstract void Collect(int startYear, int endYear); 

    public List<T> GetCurrentCollection() { 
        return collection; 
    } 
} 

public AuthorCollector : EvoObjectCollector<Author> { 

    public AuthorCollector(int startYear, int endYear) { 
        Collect(startYear, endYear); 
    } 

    public void Collect(int startYear, int endYear) { 
        // Collect the collection 
        collection = ... 
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM