模型约束验证

发布于 2024-11-29 22:26:46 字数 454 浏览 2 评论 0原文

给定一个基于通用树(Java)的数据模型。假设

abstract class XModel {
  long id;
  XModel parent;
}

class ProjectModel extends XModel {
  String customer;
}

class FileModel extends XModel {
  String name;
}

class FolderModel extends XModel {
  String name;
  String attributes;
}

我的挑战是确保 FileModel 只在 FolderModels(或 ProjectModels)中使用,FolderModels 只在 ProjectModels 中使用。 由于模型应该是可扩展的 - 是否有任何通用方法可以执行此约束验证(如 XML 模式),而无需在验证方法中对父子关系进行硬编码?

Given a generic tree based (Java) data model. Let's say

abstract class XModel {
  long id;
  XModel parent;
}

class ProjectModel extends XModel {
  String customer;
}

class FileModel extends XModel {
  String name;
}

class FolderModel extends XModel {
  String name;
  String attributes;
}

My challenge is to make sure that FileModels are only used in FolderModels (or PrjectModels) and FolderModels are only used in ProjectModels.
Since the model should be extensible - is there any generic way to do this constraint validation (like XML Schema does) without hardcoding the parent-child-realtions in a validation method?

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

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

发布评论

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

评论(3

空心↖ 2024-12-06 22:26:46

我不确定它有多大用处 - 取决于您如何创建树,但这可能有效:

abstract class XModel<T extends XModel> {
    long id;
    T parent;
}

class ProjectModel extends XModel { ... }

class FolderModel extends XModel<ProjectModel> { ... }

class FileModel extends XModel<FolderModel> { ... }

更新

要将验证逻辑与模型本身分开,您可以使用一些运行时验证器,例如 HibernateSpring 验证器。

I'm not sure how useful it would be - depends on how you create the tree, but this might work:

abstract class XModel<T extends XModel> {
    long id;
    T parent;
}

class ProjectModel extends XModel { ... }

class FolderModel extends XModel<ProjectModel> { ... }

class FileModel extends XModel<FolderModel> { ... }

UPDATE

To separate validation logic from model itself you could use some runtime validators like Hibernate or Spring validators.

篱下浅笙歌 2024-12-06 22:26:46

我将使用描述限制的泛型和接口

  interface Model {

  }

  abstract class AbstractModel<P extends Model> implements Model {

    Long id;
    P    parent;
  }

  class ProjectModel extends AbstractModel implements HasFileModel,
      HasFolderModel {

  }

  interface HasFileModel extends Model {
  }

  static class FileModel extends AbstractModel<HasFileModel> {

  }

  interface HasFolderModel extends Model {

  }

  class FolderModel extends AbstractModel<HasFolderModel> implements
      HasFileModel {

  }

这样,子级仅将父级限制为接口而不是具体类型

I would use generics and interfaces that describe the restrictions

  interface Model {

  }

  abstract class AbstractModel<P extends Model> implements Model {

    Long id;
    P    parent;
  }

  class ProjectModel extends AbstractModel implements HasFileModel,
      HasFolderModel {

  }

  interface HasFileModel extends Model {
  }

  static class FileModel extends AbstractModel<HasFileModel> {

  }

  interface HasFolderModel extends Model {

  }

  class FolderModel extends AbstractModel<HasFolderModel> implements
      HasFileModel {

  }

This way the child only restricts the parent to an interface not a concrete type

天暗了我发光 2024-12-06 22:26:46

如果您不想将这些约束放入代码中,则必须自己编写一个由某些外部数据提供的验证方法。据我所知,除了使用泛型之外,没有这样的内置选项(在编译器中),这需要您在代码中表达约束。

If you don't want to put those constraints into code, you'd have to write a validation method that is fed by some external data yourself. AFAIK there's no such built in option (in the compiler) besides using Generics, which would require you to express the constraints in code.

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