简体   繁体   English

在类中有效使用静态字段和方法? 在构造函数或声明中初始化? 我需要指导

[英]Effective use of static fields and methods in a class? Initialised in the constructor or in declaration? I need guidance

I know that it exist numerous questions about when to use or not use static fields and methods in Object Oriented Programming, but most examples boils down to using Math.Pi or similar simplified examples. 我知道关于面向对象编程中何时使用或不使用静态字段和方法存在很多问题,但是大多数示例归结为使用Math.Pi或类似的简化示例。

I have a serious defunct as a programmer, and it is caused by my disability to conform to OOP programming in general I guess. 我作为一名程序员已经严重不适合工作,这是由于我无法使用OOP编程导致的。 If it was up to me I'd use a functional approach to many of my programs, but my fellow programmers does not necessarily agree. 如果要由我决定,我会在许多程序中使用功能性方法,但是我的同伴程序员不一定同意。

I understand that not all problems are best solved in an OOP context, but I'll try to present a problem I'm currently facing that I believe would be best solved with OOP techniques. 我知道并非所有问题都可以在OOP上下文中得到最好的解决,但是我将尝试介绍我目前面临的一个问题,我相信使用OOP技术可以最好地解决。

My main problem is that I don't understand how to best utilize system resources when using OOP in C#. 我的主要问题是,我不了解在C#中使用OOP时如何最好地利用系统资源。 I can't visualize the memory allocation and flow of execution when introducing static fields and methods to my classes, and when to declare a resource as static or non-static. 将静态字段和方法引入类时,以及何时将资源声明为静态或非静态时,我无法直观地看到内存分配和执行流程。

So, this is a short explanation of my problem at hand: 因此,这是我手头问题的简短解释:

I need to develop a "utility" that synchronises information between two disconnected systems. 我需要开发一个“实用程序”,用于在两个断开连接的系统之间同步信息。 "System 1" operates with "Tickets" containing information about bugs reported by customers and verified by testers. “系统1”与“票据”一起运行,其中包含有关由客户报告并由测试人员验证的错误的信息。 I'm able to access the Ticket contents as objects through a Linq query to the SQL database via .Net's Entity Framework. 我可以通过.Net的实体框架,通过对SQL数据库的Linq查询,以对象的形式访问票据内容。

"System 2" operates with Work Items that needs to be created based on "System 1"'s Tickets. “系统2”与需要根据“系统1”的故障单创建的工作项一起运行。 The "utility" needs to keep this information in synch, and it could be changed from both systems. “实用程序”需要使此信息保持同步,并且可以在两个系统中对其进行更改。 So I need to keep the information about the state of the different objects persistent between program runs. 因此,我需要在程序运行之间保持有关不同对象状态的信息。

So I use "System 1"s database (read-only) to create local objects that matches "System 2"s requirements for creating initial Work Items and store the local objects through binary serialisation, so that I can create a new List in the next run and compare the values locally and avoid unnecessary network and server load, then update the systems through their respective API's. 因此,我使用“系统1”的数据库(只读)来创建符合“系统2”创建初始工作项要求的本地对象,并通过二进制序列化来存储本地对象,以便在数据库中创建新列表。下次运行并在本地比较值,避免不必要的网络和服务器负载,然后通过各自的API更新系统。

While I have a working solution for doing this, I'm unsure if it runs efficient. 虽然我有一个可行的解决方案,但是我不确定它是否有效。 I have a class (LocalTicket) and a class (LocalWorkItem) that is used to hold state between runs, but the LocalTicket class uses a Database Connection to gather information about Tickets and I've declared a static List to hold the values from the server to compare against the local objects. 我有一个类(LocalTicket)和一个类(LocalWorkItem),用于保存两次运行之间的状态,但是LocalTicket类使用数据库连接来收集有关票证的信息,并且我声明了一个静态列表来保存服务器中的值与本地对象进行比较。 I don't initialise the list before entering the constructor for the first LocalTicket object and I do so via a LINQ to Object query. 在输入第一个LocalTicket对象的构造函数之前,我不会初始化列表,而是通过LINQ to Object查询进行初始化。

Will this cause my LINQ query to be executed each time a LocalTicket gets created, or just once? 这会导致我的LINQ查询在每次创建LocalTicket时执行一次,还是仅执行一次? Would I be better off initialising the List from the LINQ query via a static method in the class, or by having a non-static method populate the List with the objects and demand that calling methods in my utility class creates a new object to be able to compare the objects? 我最好通过类中的静态方法从LINQ查询中初始化List,或者通过让对象使用非静态方法填充List并要求我的实用工具类中的调用方法创建一个新对象,从而使状态更好比较对象? Or by initialising it directly when defining it? 还是在定义它时直接对其进行初始化? The resulting LocalTickets created from the query's content is used locally and is created anew with each run, to detect the actual changes made to the database through the Help Desk application. 根据查询的内容创建的结果LocalTickets将在本地使用,并在每次运行时重新创建,以检测通过Help Desk应用程序对数据库所做的实际更改。

I'm sorry if this seems like a pile of rubbish, but I don't know how to describe it in a more explicative way... 如果这看起来像一堆垃圾,我很抱歉,但是我不知道如何用更明确的方式描述它。

I guess I need some guidance on designing classes that contains calls to external sources that generates objects, like a factory thing or something. 我想我需要一些设计类的指导,这些类包含对生成对象(例如工厂事物)的外部源的调用。

When reading this in context, I'm surprised if anyone would actually be able to give me any advice, but I take my chances... 当阅读上下文时,如果有人真的能够给我任何建议,我会感到惊讶,但是我还是抓住了机会...

Make a "service" class to manage each separate remote server, hold & query the list of tickets or workitems via methods there. 创建一个“服务”类来管理每个单独的远程服务器,通过其中的方法来保存和查询票证或工作项列表。

You don't want the list of tickets or workitems to be perpetual for all time -- your application will need to refresh some of these, from time to time. 您不希望票证或工作项的列表永远都是永久的-您的应用程序将需要不时刷新其中的一些。

So: 所以:

  • TicketService. 票务服务。
  • WorklistService. WorklistService。

For good programming form these Services would be better not as statics, but owned as global variables in the main program. 对于良好的编程形式,这些服务最好不要作为静态变量,而应作为主程序中的全局变量拥有。

Your main workloop can then consist of: 然后,您的主要工作循环可以包括:

this.ticketService = new TicketService( /*some DB location*/);
this.worklistService = new WorklistService( /*worklist DB or URL location*/);
while (stillRunning) {
    List<Ticket> tickets = ticketService.queryTickets();
    syncTickets( tickets);
    // wait 1 minute.
}

Hope this helps. 希望这可以帮助。

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

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