创建自定义对象并将其保存到SQL Server [英] Create custom objects and save them to SQL server

查看:93
本文介绍了创建自定义对象并将其保存到SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个应用程序来管理具有以下特征的自定义对象:

1)从表单中,用户可以创建代表零件/组件的基本自定义对象。例如,创建温度传感器对象类型。该对象将具有自定义属性,例如温度标签(字符串),最低温度(浮点)和最高温度(浮点)。另一种对象类型可以是具有速度,功率等属性的电机。

注意:对象类型将使用表格创建,可以通过添加或删除属性来编辑它们。 />
2)在另一种形式上,用户可以创建对象实例(sensor1,sensor2,motor1等),并以表格格式查看并输入属性值(我们将有数百个实例)。 br />
3)从另一种形式,用户可以创建更高级别的对象,其中包含基本属性(字符串,浮点数等),但也包括基本对象,如温度传感器或电机。 br />
例如一个名为Air Con的对象类型,它有一个名为setpoint degC(字符串)的基本属性,一个measuredtemperature对象(类型为temperature_sensor)和一个扇形对象(类型为Motor)。 />
4)在定义了基本类型(传感器和电机)和高级对象类型(Air Con)后,我们可以创建这个高的多个实例h级对象,例如AirCon1,AirCon2等。

如果我们在任何级别修改对象类型,则更改应在所有实例中级联。



对象类型及其属性应存储在SQL Server等数据库中。



我不是在寻找代码来解决这个问题,而是更多关于最佳方法的指导。可能有很多图书馆或技术可以很容易地做到这一点...



我尝试了什么:



我看到的选项:

1 - 在我们定义对象类型时编写在运行时动态构建数据库表的代码,即为每个对象类型添加一列属性和删除/重命名编辑对象。一旦已经存在数据,可能很难修改表定义。看起来很乱。

2 - 创建包含大量备用列的静态表,这些列将分配给对象属性。凌乱,不知道如何创建包含更多对象类型的对象类型。

3 - 创建一个包含xml类型列的静态表,并尝试将所有属性放在xml格式中。将xml从列映射到多个datagrid列可能很复杂?

I have to create an application to manage custom objects with the following characteristics:
1) From a form the user can create basic custom objects which will represent parts/components. For example create a "temperature sensor" object type. This object will have custom properties such as "temperature tag (string)", "Minimum temperature (float)" and "Max temperature (float)". Another object type could be Motor with properties such as speed, power, etc.
Note: The object types would be created using a form with the possibility of editing them later by adding or removing properties.
2) On another form the user would be able to create object instances (sensor1, sensor2, motor1, etc) and see and enter the property values in a table format (we will have hundreds of instances).
3) From another form the user would be able to create higher level objects which would contain basic properties (strings, floats, etc) but also basic objects such as "temperature sensor" or "Motor".
For example an object type called "Air Con" which would have 1 basic property called "setpoint degC" (string), a measuredtemperature object (type temperature_sensor) and a fan object (type Motor).
4) After defining the basic types (sensor and Motor) and the high level object types (Air Con) we could then create multiple instances of this high level object, eg AirCon1, AirCon2, etc.
If we then modify the object type at any level, the changes should cascade across all instances.

The object types and its properties should be stored on a database such as SQL Server.

I'm not looking for code to solve this but more of guidance on the best approach. There are probably libraries or technologies out there that can do this quite easily...

What I have tried:

Options as I see it:
1 - write code that builds the database tables dynamically in runtime as we define the object types, ie add a column for each property and delete/rename as the objects are edited. Probably hard to modify the table definitions once there is already data in them. Seems messy.
2 - Create static tables with lots of spare columns which will get allocated to object properties. Messy, not sure how to create object types that contain more object types inside.
3 - Create a static table with a column of the xml type and try to put all the properties in xml format. probably complicated to map xml from a column to multiple datagrid columns?

推荐答案

首先想到的是XML序列化,你已经提到过了。



另一种选择是使用 ORM ,如实体框架,NHibernate,Dapper等。

见概述: https://www.slant.co/topics/16667/~orms-for-c [< a href =https://www.slant.co/topics/16667/~orms-for-ctarget =_ blanktitle =New Window> ^ ]



但要注意,像NHibernate这样的ORM有一个陡峭的学习曲线,而且文档很平庸,所以最好从像Dapper这样的轻量级开始。



您可能感兴趣的另一件事是规则引擎,这是一个例子: GitHub - NRules / NRules:.NET的规则引擎,基于Rete匹配算法,内部D C#中的SL。 [ ^ ]

和CodeProject: SimpleRules.Net - 易于使用的规则引擎 [ ^ ]
XML serialization is the first thing that comes to mind, you already mentioned it.

Another option would be using an ORM like Entity Framework, NHibernate, Dapper etc.
See overview here: https://www.slant.co/topics/16667/~orms-for-c[^]

But beware, an ORM like NHibernate has a steep learning curve and the documentation is mediocre, so it might be best to start with a more lightweight one like Dapper.

Another thing you might be interested in is a Rule Engine, here is an example: GitHub - NRules/NRules: Rules engine for .NET, based on the Rete matching algorithm, with internal DSL in C#.[^]
And on CodeProject: SimpleRules.Net - Easy to use Rules Engine[^]


这可以通过SQL等关系数据库中的3个表来完成:



表1(对象)将具有ObjectID,ObjectName和其他基本项,例如price。

表2(属性)将是一个简单列表(AttributeID,AttributeName)

表3将搭建表1和表1。 2,并且还有一个额外的列值



同样,会有多个类来表示这些表。区别在于ObjectClass(名称)将为分配给对象和值的所有属性提供一组键值对。



你可以当然这使得这个更复杂,可能是价值的第四个表(消除13000对13,000对13K值)。或者扩展有效范围的属性表。



这种方法是参数搜索和电子商务网站的数量。过滤器。
This could be done with as little as 3 tables in a relational database such as SQL:

Table 1 (Object) would have an ObjectID, ObjectName, and other basic items such as price.
Table 2 (Attribute) would be a simple list (AttributeID, AttributeName)
Table 3 would bridge Tables 1 & 2, and have an additional column for the values

Likewise, there would be multiple classes to represent these tables. The difference being that the ObjectClass (ugh the name) would have a collection of Key-Value pairs for all of the Attributes assigned to the object and values.

You could of course make this more complex, perhaps a 4th table for values (to eliminate 13000 vs 13,000 vs 13K values). Or expanding the Attribute table for valid ranges.

This method is how many eCommerce sites do the parametric searches & filters.


根据保存对象的方式(方法),可以将它们转储到sql数据库中,与任何其他对象(如pdf,docx,xl​​ms等)一样。 )。

有特殊字段类型: BLOB [ ^ ]



有关详细信息,请参阅:< a href =https://support.microsoft.com/en-hk/help/309158/how-to-read-and-write-blob-data-by-using-ado-net-with-visual-c- net>如何使用ADO.NET和Visual C#.NET读取和写入BLOB数据 [ ^ ]
Depending on the way (method) you save your objects, you can dump them into sql database in the same as any other object (such as pdf, docx, xlms, etc.).
There's special field type: BLOB[^]

For further details, please see: How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET[^]


这篇关于创建自定义对象并将其保存到SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆