简体   繁体   English

将类作为设置项存储在sql中是一个好主意吗?

[英]Is it a good idea to store classes as settings items in sql?

I am doing this project in c# and when designing a database, i am using a rule that each class is basically sql table (at least the class that has to be persisted). 我正在用C#进行此项目,并且在设计数据库时,我使用的规则是每个类基本上都是sql表(至少必须保留的类)。

Since some classes are purely used to define business settings and the classes are rather flat, I am curios does it make any sense to do something like this.. 由于某些类仅用于定义业务设置,并且这些类相当扁平,因此我想做这样的事情没有任何意义。

Transform business layer class 转换业务层类

class Contact
{
public string Name {get;set;}
public string PhoneNumber {get;set;}
public bool AcceptsTextMessages {get;set;}
public bool AllowedHoursForTextMessagesStart {get;set;}
public bool AllowedHoursForTextMessagesEnd {get;set;}
public List<DayOfWeek> SendMessagesOnlyOnWorkdays {get;set;}
}

to a data layer class that look something like (and persist it in sql) 到看起来像的数据层类(并在sql中保留)

public Settings
{
public ID {get;set}
public Name {get;set}
public Value {get;set;}
}

with real life data 与现实生活中的数据

ID   Name                                Value
1    Name                                John Doe
2    PhoneNumber                         01234657
3    ExceptsTextMessages                 true
4    AllowedHoursForTextMessagesStart    0
5    AllowedHoursForTextMessagesEnd      24
6    SendMessagesOnlyOnDays              1,2,3,4,5

The primary reason for this is to have one settings table instead of having as many tables as classes, possibly easier class modification, easier manipulation of properties between classes (in case there is a business logic need to move one property from one class to another) 这样做的主要原因是拥有一个设置表,而不是拥有与类一样多的表,可能是更容易的类修改,更容易地在类之间进行属性操作(以防业务逻辑需要将一个属性从一个类移到另一个类)

Decomposing your objects into IDs and attribute-value pairs is one of those techniques that's sometimes extremely useful. 将对象分解为ID和属性值对是其中一种有时非常有用的技术。 EAV data is much more complicated to manage than a flat table with individual columns, so it's not something to implement lightly. EAV数据的管理要比带有单独列的平面表要复杂得多,因此实现起来并不容易。

Given what you've posted, I probably wouldn't. 鉴于您发布的内容,我可能不会。 All the fields you have seem reliably relevant to being-a-contact and unlikely to require changing around dynamically in production (since one starts or stops accepting text messages, rather than ascending to a plane of existence where text messages are epistemologically irrelevant). 您似乎已经可靠地联系了所有领域,并且不太可能需要在生产中动态更改(因为一个域开始或停止接受文本消息,而不是上升到与认识论无关的生存层面)。

Even if it made sense to represent certain fields as pairs, I'd only do it for those fields: keep a users table with a primary key and the essential data, then put the rest off in an EAV table with a foreign key relationship to users. 即使将某些字段表示为对有意义,我也只会对这些字段进行处理:保留一个具有主键和基本数据的用户表,然后将其余的放在具有外键关系的EAV表中,用户。

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

相关问题 将我的DataContext存储在会话内存中是个好主意吗? - Is it a good idea to store my DataContext in Session Memory? 为特定设置创建类是一种好习惯吗? - Is it good practice to create classes for specific settings? 存储XNA屏幕保护程序设置的好方法? - Good way to store settings for an XNA screensaver? C#winform,将所有相关类部分化是一个好主意吗? - C# winform, is it a good idea to have all Related Classes Partial? 将带有标签的整个xml文件(.xml)文档存储在mongodb中是个好主意吗? - Is it good idea to store whole xml file (.xml) document with tags in mongodb? struct是一种在内存中存储程序设置的好方法吗? - Would struct be a good way to store program settings in memory? CSV 数据(未知格式)到 XML 数据(固定格式)通过业务类。 使用反射是个好主意吗? - CSV data (unkown format) to XML data (fixed format) via business classes. Is using reflection a good idea? 部署SqlCeDataReader是一个好主意吗? - Is disposing a SqlCeDataReader a good idea? 多个FileSystemWatchers是个好主意? - Multiple FileSystemWatchers a good idea? 这是一个扩展方法的好主意吗? - Is this a good idea for an extension method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM