简体   繁体   English

设计要在N层应用程序中使用的数据库

[英]Designing a database to be used in N-Tier application

I am working on an N-tier application using .NET (C#, SQL Server). 我正在使用.NET(C#,SQL Server)开发N层应用程序。 I started by designing the database because I believe starting from bottom would a good idea. 我从设计数据库开始,因为我相信从底部开始是一个好主意。 Now I need your suggestions in building the database to be very flexible. 现在我需要您建立数据库的建议非常灵活。 Knowing that the application is intended to built as a group of plug-ins. 知道该应用程序旨在构建为一组插件。

The application is a remote control app for many different hardware devices (Cars, Engines, etc) Here is a fragment of the database tables. 该应用程序是许多不同硬件设备(汽车,引擎等)的远程控制应用程序。这是数据库表的一个片段。

  1. Devices Table 设备表
  2. Personnel Table 人员表
  3. Users Table 用户表
  4. Roles Table 角色表

As you can see the devices table is a table to represent the devices we are controlling and since each each device may have more or less properties than others I want to link each device to its properties which might be in another table in way that is as elegant as possible. 正如您所看到的,设备表是一个表,用于表示我们正在控制的设备,因为每个设备可能具有比其他设备更多或更少的属性,我希望将每个设备链接到其属性,这些属性可能位于另一个表中,如同优雅尽可能。 The personnel table is for the people responsible for the device for example: Car drivers (there might be more than one driver for each car), Engine operator, etc. 人员表适用于负责设备的人员,例如:汽车驾驶员(每辆车可能有多个驾驶员),发动机操作员等。

Users are the application users as you can see I have separated the users and roles into two different tables. 用户是应用程序用户,您可以看到我已将用户和角色分成两个不同的表。

What I am looking for is a way to link the devices to their properties. 我正在寻找的是一种将设备链接到其属性的方法。 Plus, In the application each device is an instance of an attachable device. 另外,在应用程序中,每个设备都是可连接设备的实例。 Meaning that I can attach a device called say: SIM card to a device called Vehicle. 这意味着我可以将名为say:SIM卡的设备连接到名为Vehicle的设备。 Moreover, are there any design patterns regarding this subject? 此外,有关于这个主题的设计模式吗?

Thanks in advance, and please pardon me if I am not clear. 在此先感谢,如果我不清楚,请原谅我。

The real design decision here is around your Device and Property tables. 这里真正的设计决策是围绕您的设备和属性表。 The others are pretty standard. 其他是非常标准的。

For a Device to be optionally associated with one or many properties, then all you really need is a nullable foreign key in your Property table. 对于可选择与一个或多个属性关联的设备,您真正需要的只是属性表中的可为空的外键。 But this raises one very important question: Is it necessary for certain devices to have a very specific set of properties? 但这提出了一个非常重要的问题:某些设备是否有必要拥有一组非常具体的属性? Will your application be expecting a 'Model' property for your 'Car' device? 您的应用程序是否期望为您的“汽车”设备提供“模型”属性? How do we ensure database integrity with this kind of design? 我们如何通过这种设计确保数据库的完整性?

You can do this through database-defined constraints. 您可以通过数据库定义的约束来完成此操作。 But how should you know that a 'Car' device requires a 'Model' property? 但是你怎么知道'Car'设备需要'Model'属性? Perhaps you need a template definition for each device: DeviceType and PropertyType tables. 也许您需要为每个设备定义模板:DeviceType和PropertyType表。 This way your constraint will always know what properties are expected when you are updating/inserting a record. 这样,您的约束将始终知道更新/插入记录时预期的属性。 Also, this will make it easy to work with the data in your application. 此外,这将使您可以轻松处理应用程序中的数据。

Depending on how you are going to be handling your data layer this database design may be a moot point. 根据您处理数据层的方式,这种数据库设计可能没有实际意义。 If you were to use EntityFramework with a code first approach you can create your logical entities using that and generate the database from your models. 如果您使用EntityFramework和代码优先方法,您可以使用它创建逻辑实体并从模型生成数据库。

Most ORM based solutions provide some way to create a database from your objects relationship, but although you can quickly and easily get a database up and running which will do EXACTLY what you NEED, if you want to start playing with the schema you may run into troubles, although you can do quite alot of inferring to help it make the database behind the scenes the way you want it. 大多数基于ORM的解决方案提供了一些从对象关系创建数据库的方法,但是尽管您可以快速轻松地启动和运行数据库,这将完全满足您的需求,如果您想开始使用您可能遇到的架构麻烦,虽然你可以做很多推断,以帮助它以你想要的方式在幕后制作数据库。

If this is a greenfield project and you dont need to accommodate any legacy database system I would stop worrying about your database for the moment and just get to writing your logical models and defining their relationships, and let the data storage concerns handle themselves. 如果这是一个绿地项目并且您不需要适应任何遗留数据库系统,那么我暂时不再担心您的数据库,只需编写逻辑模型并定义它们之间的关系,并让数据存储问题自行处理。

There is no ideal database design for anything. 没有理想的数据库设计。 It can quickly change according to the information you are manipulating. 它可以根据您操作的信息快速更改。

One simple approach is to list all the classes in your design and then break those classes to the lowest level possible without any repeating data. 一种简单的方法是列出设计中的所有类,然后将这些类拆分为最低级别,而不需要任何重复数据。

Even each device has various properties, you can classify the devices into their respective group and a group will have mostly similar properties. 即使每个设备都具有各种属性,您也可以将设备分类到各自的组中,并且组将具有大多数相似的属性。 You can build a property table including all the properties for that group. 您可以构建包含该组的所有属性的属性表。 As you said, you can connect them in a different table with deviceID and propertyID. 如您所说,您可以使用deviceID和propertyID在不同的表中连接它们。 Same thing goes for a person and device. 一个人和设备也是如此。 You can break the person table into smaller pieces depending on the attributes you have. 您可以根据您拥有的属性将人员表分成更小的部分。

If numerous tables form a cluster in terms of data extrection, you can consider a star schema for your solution with a fact table (real numbers) in the center and the associated data around it. 如果许多表在数据扩展方面形成一个集群,您可以考虑使用中心的事实表(实数)及其周围的相关数据为您的解决方案设置星型模式。

Good Luck! 祝好运!

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

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