简体   繁体   English

我应该为每个使用的数据库都有一个类吗?

[英]Should I have one class for every database I use?

First, let me explain what I am doing. 首先,让我解释一下我在做什么。 I need to take an order, which is split up into different databases, and print out this very large order. 我需要接受一个订单,它被分成不同的数据库,并打印出这个非常大的订单。 What I need from the orders is about 100 or so columns from different databases. 从订单中我需要的是来自不同数据库的大约100个列。 The way I was doing in was querying with a join and assigning all of the column values to a variable in my one large Order class. 我正在进行的方式是使用连接查询并将所有列值分配给我的一个大型Order类中的变量。 This has started to become troublesome. 这已经开始变得麻烦。 I am wondering of instead of having one class that is comprised of 100 or so members that make up the order. 我想知道的是,有一个由100个左右的成员组成的班级组成了订单。 Should I have just one class for every database I use, and then work with that? 我应该为我使用的每个数据库只有一个类,然后使用它吗?

Let me add to this. 让我补充一点。 Basically, is it better to map you objects to the original database tables, or the result set. 基本上,将对象映射到原始数据库表或结果集是否更好。 Because I have my object mapped to the result set and not the individual tables. 因为我的对象映射到结果集而不是单个表。

I would recommend an object-oriented solution to this. 我建议使用面向对象的解决方案。 Presumably your database is designed with tables that represent logical groupings of data. 据推测,您的数据库设计有表示逻辑数据分组的表。 Each of these tables can likely be mapped onto a class in your system, although in some cases, it may be more than one table that makes up an object or there might be multiple classes that a table maps onto using subclassing. 这些表中的每一个都可能映射到系统中的一个类,尽管在某些情况下,它可能不止一个表组成一个对象,或者表可能有多个类使用子类映射到它们。 If you need to display data from multiple tables -- say a list of orders with some data from the customer associated with the order -- then you can either use views, joins, or stored procedures to construct an object of a view class that represents the selected data in the view/join/sp. 如果您需要显示来自多个表的数据 - 比如包含来自与订单关联的客户的一些数据的订单列表 - 那么您可以使用视图,联接或存储过程来构造表示视图类的对象视图/ join / sp中的选定数据。

Essentially what I am describing is an N-tier data architecture where you have a low-level data access layer that deals with data from a SQL orientation -- tables, views, stored procedures. 基本上我所描述的是一个N层数据架构,你有一个低级数据访问层来处理来自SQL方向的数据 - 表,视图和存储过程。 Above this may be a generic object layer that deals with generic data objects and interfaces with the data access layer to store/retrieve objects from the database. 在此之上可以是通用对象层,其处理通用数据对象并与数据访问层接口以从数据库存储/检索对象。 Finally, above this you have a strongly-typed business object layer where your application works with classes that semantically linked to your application -- orders, customers, invoices, etc. There are many different patterns for implementing this type of general architecture and you should investigate several to see which fits your application needs the best. 最后,在此之上,您有一个强类型的业务对象层,您的应用程序可以使用语义链接到您的应用程序的类 - 订单,客户,发票等。实现此类通用架构有许多不同的模式,您应该调查几个,看看哪个适合您的应用程序需要最好的。 You might want to directly use an object-relational mapping like LINQ or nHibernate or you might want to layer a repository on top of an ORM. 您可能希望直接使用像LINQ或nHibernate这样的对象关系映射,或者您可能希望在ORM之上层叠存储库。

Personally, I think that structuring your application to deal with objects within the context of your domain, rather than simply as table data, will improve your code. 就个人而言,我认为构建应用程序来处理域内环境中的对象,而不仅仅是表数据,将改善您的代码。 It should improve understandability and maintainability. 它应该提高可理解性和可维护性。 You will be able to encapsulate behavior within your domain classes rather than have it spread throughout your application. 您将能够在域类中封装行为,而不是将其传播到整个应用程序中。 Of course, this assumes that you follow good design practices, but using OO design will encourage this. 当然,这假设您遵循良好的设计实践,但使用OO设计将鼓励这一点。 Separating out the business and data logic from your display logic will also make your application much more testable, as will breaking down monolithic classes into smaller, more focused classes that are interrelated. 将业务和数据逻辑与显示逻辑分离也将使您的应用程序更加可测试,将单个类拆分为相互关联的更小,更集中的类。

why not just load the data from individual db's inidividuallly? 为什么不直接加载来自各个数据库的数据呢?

For example, your constructor for the Order object would look like this: 例如,Order对象的构造函数如下所示:

Method New Order(orderId) {
   Get Database 1 Details
   Load Details into appropriate Variables
   Get Database 2 Details 
   Load Details into appropriate Variables
   Get Database **N** Details 
   Load Details into appropriate Variables
}

it makes it easier to maintain the sql which touches the individual DB's and you won't have a dozen different classes out there for each DB. 它可以更容易地维护接触单个数据库的sql,并且每个数据库都不会有十几个不同的类。

Another Alternative is to have a Stored Procedure which returns multiple result sets which you can access via a DataSet in your code. 另一种方法是使用存储过程返回多个结果集,您可以通过代码中的DataSet访问这些结果集。

Or you could make your join easier to deal with and maintain by turning it into a VIEW in one of your databases. 或者,您可以通过将其转换为其中一个数据库中的VIEW来使您的联接更易于处理和维护。

One thing you really need to think about here is maintenance. 你真正需要考虑的一件事是维护。 How easy is it for you to maintain the code after you've not read it for six months, or even how easy will it be for some other developer to maintain the code with no prior knowledge of it. 在六个月没有阅读代码之后维护代码是多么容易,或者对于其他开发人员在没有事先了解的情况下维护代码是多么容易。 Pick the paradigm which you believe will be easiest to maintain and then code it that way 选择您认为最容易维护的范例,然后以这种方式编写代码

One elegant and simple approach to attacking this problem is the Active Record pattern: 攻击此问题的一种优雅而简单的方法是Active Record模式:

http://en.wikipedia.org/wiki/Active_record_pattern http://en.wikipedia.org/wiki/Active_record_pattern

Of course, it may not be feasible in every scenario. 当然,在每种情况下都可能不可行。 It could also be integrated with other patterns, as implied in other answers. 它也可以与其他模式集成,如其他答案所暗示的那样。 I'm one who believes you will face tradeoffs no matter what approach you choose. 无论你选择何种方法,我都相信你会面临权衡。 All the best! 祝一切顺利!

It really just sounds like your preference to me. 这听起来像是你对我的偏好。 How would you prefer to work with it? 您希望如何使用它? Would it be easier for you to work with it as separate C# objects, or would it be easier for you to work with it as several SQL tables? 您是否更容易将它作为单独的C#对象使用,或者您是否更容易使用它作为多个SQL表?

I would consider creating a class to handle the data in each seperate data store. 我会考虑创建一个类来处理每个单独的数据存储中的数据。

Then, I would consider using a pattern, something like a Facade to group those sub-systems together. 然后,我会考虑使用一个模式,比如Facade,将这些子系统组合在一起。 Search the web on 'Design Patterns' and 'Facade' for more information. 在“设计模式”和“外观”上搜索网页以获取更多信息。 The facade can then account for any specific interaction between the data in the seperate data stores. 然后,外观可以解释单独数据存储中的数据之间的任何特定交互。

To me, it is much easier to maintain code that is logically grouped, and having seperate classes for seperate data stores makes sense here for me. 对我来说,维护逻辑上分组的代码要容易得多,而且对于我来说,为单独的数据存储分配单独的类是有意义的。

I'm going against the grain here, but I'd say that keeping this object mapped to the result set, and keeping the join in the database, might be a better idea. 我在这里反对谷物,但我会说保持这个对象映射到结果集,并保持连接在数据库中,可能是一个更好的主意。

For the business logic, an "order" is usually a single cohesive concept (at least, that's how you started out framing it). 对于业务逻辑,“订单”通常是一个单一的概念(至少,这就是你开始构建它的方式)。 Could the fact that it is mapped into multiple tables (or databases) be an artifact of how the data is captured? 它被映射到多个表(或数据库)的事实可能是如何捕获数据的工件? I would ask myself these questions before breaking it up: 在分解之前我会问自己这些问题:

  • Are there tangible benefits to composing the order out of other objects? 从其他对象中组合订单是否有实际好处?
  • Do the attributes have different cardinality? 属性是否具有不同的基数? Ie are some per-order and others per-line-item? 即每个订单和其他每个订单项?
  • Can you reuse existing code for the composed objects? 您可以重用组合对象的现有代码吗?
  • Are you enabling some other interaction that's easier to do with multiple objects? 您是否正在启用一些更容易对多个对象进行的其他交互?

If you don't answer yes to any of those questions, I'd wager your code will be simpler and more readable if it deals with just the order as an atomic object, and lets the database hide the complexity of where it's coming from (you could even use a view for that). 如果你没有对这些问题的回答是肯定的,那么如果它只将订单作为一个原子对象来处理,我会下注你的代码会更简单,更具可读性,并让数据库隐藏它来自哪里的复杂性(你甚至可以使用一个视图)。

Sheer number of attributes isn't usually a reason to break up an interface. 纯粹的属性数量通常不是分解界面的理由。 But, if the complexity (or size) of the order object itself is what's getting you down, you might try to simplify it internally to use some sort of generic accessor method, like: 但是,如果订单对象本身的复杂性(或大小)让你失望,你可能会尝试在内部简化它以使用某种通用的访问器方法,例如:

private object GetOrderAttribute(string attributeName){
    // use a data structure (like a hash table) to store and access internally
}
...
output("Quantity: " + GetOrderAttribute("quantity"));
// etc.

One other note: while performance should rarely be your starting concern in logical system design, most cases involving database table joins will perform better if the database does the join, because the DBMS can use indexes and other mechanisms to perform the join efficiently and avoid loading pages from disk that aren't needed. 另外需要注意的是:虽然性能很少是逻辑系统设计中的首要问题,但是如果数据库进行连接,大多数涉及数据库表连接的情况都会表现得更好,因为DBMS可以使用索引和其他机制来有效地执行连接并避免加载磁盘中不需要的页面。 Maybe all your individual queries do that too, but typically that's something the database can do an order of magnitude more efficiently than business logic code. 也许您所有的个人查询都会这样做,但通常情况下,数据库可以比业务逻辑代码更高效地执行一个数量级。 (Of course, if the join is across physical database boundaries, that benefit might be lost.) (当然,如果连接跨越物理数据库边界,则可能会丢失该优势。)

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

相关问题 我应该为具有页面对象模式的每个页面使用不同的类吗? - Should i use different class for every page with Page Object Pattern? 我有 4 个 class 和 class 属性,但我应该如何关联这些类? (一对一,一对多……) - I have 4 class and class properties, but how should I relate the classes? (one-one, one-many …) 我应该在C#.net中使用静态类进行数据库连接吗? - Should I use Static class for Database connectivity in C#.net? 我应该为数据库连接/模型类使用接口吗 - Should I use an Interface for a database connection / model class 数据库触发器或应用程序代码? 在这种情况下,我应该使用哪一个? - Database trigger or application code? which one should I use in this case? 我应该将每个班级放在单独的文件中吗? - Should I place every class in separate file? 如果我只有一个静态变量的类-我应该将此类声明为静态的吗? - In case i have class with only one static variable - should i declare this class as static? 我应该使用Convert类吗? - should I use Convert class? 我应该使用结构体还是类? - Should I use struct or class? 我应该为每个Model类提供一个实现InotifyPropertyChanged的ViewModel类吗? - Should I have one ViewModel class implementing INotifyPropertyChanged for each Model class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM