此场景的数据库架构

发布于 2024-10-31 15:20:41 字数 728 浏览 1 评论 0原文

嗯,这个架构问题已经在我脑海中徘徊了一段时间。假设以下场景:
我有一个 Licenses 表。从概念上讲,每个许可证可以限制为(许可证类型):

  • 类型 1:尝试次数。 (例如允许运行7次)
  • 类型2:试用(限时)。
  • 类型 3:完整
  • ...

因此,每个许可证都应该存储一些自定义值。 (类型 1:整数,类型 2:日期时间,类型 3:空)

此场景的最佳架构是什么?

  • 如果我决定将所有许可证放入一个表中,则每一行至少有 1 个未使用的列(对于类型 1,EndDate 将为空,对于 EndDate,TryTimes 将为空,对于类型 3,两者都将为空):
    LicenseID---LicenseType---CustomerID---EndDate---TryTimes
    另一方面,我希望我的设计尽可能灵活(将来可能会有更多许可证类型?)
  • 另一种可能的解决方案是使用一些类似元数据的方法:
    许可证ID---许可证定义---客户ID---
    其中 LicenseDefinition 包含有关许可证类型和限制的信息,并在代码端进行解析。

您建议哪一种更传统?您建议任何其他方式来实现它吗?

更新: 刚刚发现稀疏列是SQL Server。听起来很有希望...

Well, this architectural issue has been wandering in my mind for a while. Suppose the following scenario:
I have a Licenses table. Conceptually, each license can be limited to (License Types):

  • Type 1: Number of tries. (e.g. allowing to run 7 times)
  • Type 2: Trial (Time-limited).
  • Type 3: Full
  • ...

So, each license should store some custom value. (Type 1: Integer, Type 2: DateTime, Type 3: null)

What's the best architecture for this scenario?

  • If I decide to put all the licenses in one table, I have at least 1 unused column for every row (EndDate would be null for Type 1, TryTimes would be null for EndDate and both would be null for Type 3):
    LicenseID---LicenseType---CustomerID---EndDate---TryTimes
    On the other hand, I'd like my design to be as flexible as possible (Maybe more license types in the future?)
  • Another possible solution would be using some metadata-like approach:
    LicenseID---LicenseDefinition---CustomerID---
    Where LicenseDefinition contains information about the type and limit of the license and is parsed on the code side.

Which one do you suggest to be more conventional? Do you suggest any other way to implement it?

UPDATE:
Just found out about Sparse columns is SQL Server. Sounds very promising...

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

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

发布评论

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

评论(3

彩扇题诗 2024-11-07 15:20:41

我突然想到(我还没有实现类似的东西),我可能会做这样的事情:

CustomerLicense 创建数据库表。在 Customer 中,连同所有其他通用客户信息,我将添加一个 licenseType 列,它将引用 License< /代码>表。

License 将存储 licenseId 以及任何相关元数据。 这不会包括特定于许可证的规则。

在代码方面,我将创建一个 LicenseFactory 类,它将创建许可证接口的实例(ILicense)。 ILicense 可能看起来像这样(在 PHP 中):

interface ILicense
{
    public isValid($customer);
}

然后,我会有特定于许可证的实现:

class TrialLicense implements ILicense
{
    public isValid($customer)
    {
        // business logic for this specific license type here
    }
}

工厂类看起来像这样:

class LicenseFactory
{
    public static function getInstance($type)
    {
        switch($type)
        {
            case 0:
                return new TrialLicense();
                break;
        }
    }
}

所以现在,我的应用程序代码可能看起来像这样:

public function isLicenseValid($customer)
{
    return LicenseFactory::getLicense($customer->licenseType)->isValid($customer);
}

没有这已经过测试,只是我(相当冗长)的初步想法。希望它能有所帮助(即使您的应用程序可能不是 php 驱动的:))

编辑:
忘记提及 - 这种方法的强大之处在于可扩展性。每当您想要添加新的许可证类型时,只需向 License 表中添加一个新行,并添加一个新的 ILicense 实现(其中包含所需的任何业务规则)。

Off the top of my head (I haven't implemented anything like this yet), I might do something like this:

Create database tables for Customer and License. Within Customer, along with all of the other generic customer information I would add a column for licenseType, which would reference the License table.

License would store the licenseId as well as any relevant meta data. This would not include license-specific rules.

On the code side, I would create a LicenseFactory class which would create an instance of a license interface (ILicense). ILicense might look something like this (in PHP):

interface ILicense
{
    public isValid($customer);
}

Then, I would have license-specific implementations:

class TrialLicense implements ILicense
{
    public isValid($customer)
    {
        // business logic for this specific license type here
    }
}

The factory class would look something like:

class LicenseFactory
{
    public static function getInstance($type)
    {
        switch($type)
        {
            case 0:
                return new TrialLicense();
                break;
        }
    }
}

So now, my application code might look something like this:

public function isLicenseValid($customer)
{
    return LicenseFactory::getLicense($customer->licenseType)->isValid($customer);
}

None of this has been tested and are just my (rather long winded) initial thoughts. Hope it helps a little though (even though your app may not be php driven :))

Edit:
Forgot to mention - the power in this approach is the extensibility. Any time you want to add a new license type, you'd simply add a new row to the License table and a new ILicense implementation with whatever business rules are required.

懵少女 2024-11-07 15:20:41

您应该为每种许可证类型保留一个单独的表:

LicenseID(PK)---Type

LicenseID(FK)---EndDate

LicenseID(FK)---TryTimes

并保持数据完整性,例如只有计时许可证才会进入第二个表。

这也将是一个相当可扩展的模型,您将需要添加一个表、一个完整性规则和一个接口,以便添加另一种许可证类型。

You should keep a separate table for each license type:

LicenseID(PK)---Type

LicenseID(FK)---EndDate

LicenseID(FK)---TryTimes

and also maintain data integrity such as only time-trial licenses would come into the second table.

This will be also quite a scalable model, you will need to add one more table, one more integrity rule and one more interface in order to add another license type.

凉城已无爱 2024-11-07 15:20:41

您可以将单个表作为父表,并使用其他三个表来引用扩展架构的基表。

这是第一个选择。

另一种选择是使用占用空间小的简单NoSql数据库或OO数据库(如db4o),甚至简单的数据存储(如加密XML)文件

You can have a single table as a parent and three other tables referencing the base table extending the schema.

That's the first option.

Another option is using a simple NoSql Database with low footprint or an OO database like db4o or even simple data stores like an encrypted XML file.

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