ASP.NET:确定序列化为 XML 的对象类型的最佳方法

发布于 2024-10-09 16:07:38 字数 838 浏览 0 评论 0 原文

我的 SQL Server 数据库中有一个表,其中保存用户在系统中执行的某些操作的审核信息。诸如谁执行了操作、何时执行以及执行什么操作等信息都是可以轻松跨越多个操作的信息。但根据所执行的操作,我可能还想捕获特定于该操作的其他信息。为了处理这个问题,我选择向表中添加一个“XML 元数据”列,该表保存我创建的不同元数据对象的序列化 XML。我为每个我有兴趣跟踪额外操作的操作创建了一个元数据对象。因此,每个对象负责跟踪其操作的特定额外信息(元数据)。这些对象被序列化并写入我的新列。

我有 SystemAction 对象,用于存储该表中的信息,并且添加了一个字符串字段来保存来自数据库的 XML 字符串。问题是,当我从 SystemAction 对象读回此 XML 时,我正在努力寻找一种方法来将其一般地转换回正确的元数据对象。每个元数据对象都将具有不同的字段,并且每个对象都有其自己的静态方法,该方法采用 XML 字符串并尝试返回元数据对象类型。所以我可以说:

SomeActionMetadata mdObj = SomeActionMetadata.BuildFromXML(xmlStringFromDB);

但我真的不知道如何说“这里有一些 XML 可以转换为任意数量的不同对象。图一下”出去,把合适的东西还给我。”

鉴于我当前的实现,我始终可以为每个元数据对象分配一个唯一的 ID,该元数据对象作为字段存储在每个对象中,然后使用 case 语句打开该 ID 并使用适当的类的静态构建方法来构建正确的对象。但我希望有比这更自动化的东西。如果我有一个 SystemAction 对象列表并且只想循环它们并生成正确的元数据对象类型怎么办?

我希望有人以前遇到过类似的事情,或者能给我指出一篇可以帮助我的文章或帖子。非常感谢。

I have a table in my SQL Server DB that holds auditing information for certain actions a user takes within my system. Things like who performed the action, when it was performed, and what action are all pieces of information that can easily span multiple actions. But depending on the action performed, there may be other information that I want to capture, that is specific to the action. To handle this, I elected to add an "XML Metadata" column to the table that holds serialized XML of different metadata objects that I've created. I created a metadata object for each of the actions that I'm interested in tracking extra for. So each object is responsible for tracking specific extra information (metadata) for it's action. The objects are serialized and written to my new column.

I have SystemAction objects that I use to store information from this table, and I've added a string field that holds the XML string from the DB. The problem is, when I'm reading this XML back from the SystemAction objects, I'm struggling with a way to generically translate it back into it's correct metadata object. Each metadata object is going to have different fields, and each object has it's own static method that takes an XML string and attempts to return the metadata object type. So I could say:

SomeActionMetadata mdObj = SomeActionMetadata.BuildFromXML(xmlStringFromDB);

But I really don't know of a way to say "Here's some XML that could translate to any number of different objects. Figure it out and give me the right object back."

Given my current implementation, I could always just assign a unique ID to each metadata object that is stored as a field in each object, then use a case statement to switch on that ID and use the appropriate class's static build method to build the right object. But I was hoping for something a little more automatic than that. What if I have a List of SystemAction objects and just want to loop through them and generate the correct metadata object type?

I was hoping someone might have run across something similar to this before, or could point me to an article or post that could help me out. Thanks very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高跟鞋的旋律 2024-10-16 16:07:38

正如 Subhash Dike 在下面的评论中指出的,有一个类似的问题 这里能够为我指明正确的方向。

As indicated by Subhash Dike in the comments below, there is a similar SO question here that was able to point me in the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文