Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Neo4j 使用继承存储和检索对象的推荐方法_Neo4j - Fatal编程技术网

Neo4j 使用继承存储和检索对象的推荐方法

Neo4j 使用继承存储和检索对象的推荐方法,neo4j,Neo4j,在Neo4j中,存储和检索从其他对象继承属性的对象的推荐方法是什么 模型如下所示: public class Base { public string BaseProperty { get; set; } } public class DerivedA : Base { public string DerivedAProperty{ get; set; } } public class DerivedB : Base { public string DerivedBP

在Neo4j中,存储和检索从其他对象继承属性的对象的推荐方法是什么

模型如下所示:

public class Base
{
    public string BaseProperty { get; set; }
}

public class DerivedA : Base
{
    public string DerivedAProperty{ get; set; }
}

public class DerivedB : Base
{
    public string DerivedBProperty{ get; set; }
}
我们有很多衍生模型。我们的第一个解决方案是为每个派生类型创建一个事务,其中也包括来自Base的属性。问题:每当基发生更改时,我们都必须更改所有派生类型的所有事务

下一个解决方案是为Base编写一个单独的事务,首先将其属性存储到neo4j中。然后,在确定哪个派生类型之后,将创建另一个事务来存储派生类型的剩余属性。通过这种方式,事务号增加了一倍,但我们有一个清晰的分隔,使更改更容易

检索更加困难。如果我们想获得存储在Neo4j中的所有DerivedA类型,我们首先要有一个检索基本属性的事务。然后,我们确定派生类型,并使用从派生类型检索属性的新事务进行后续处理。现在,我们必须以正确的方式将两个事务的结果混合在一起,以获得包含所有属性的完整derivedA列表


有更简单/更好的方法吗?

尽管您的问题很广泛,但继承还有其他选择。如果这是你的问题,我认为这是一个正确的问题

继承的另一种选择是(例如)装饰器模式:

核心原则(取自首件优先设计模式):

decorator模式的关键点在于,它是继承的替代方案,具有在运行时改变和扩展行为的相同能力,而不必绑定到具有特定版本或其他依赖项的基类

下面是一个例子:

有关此主题的更多信息,请访问

我会把它改成更具代表性的

public interface IStudent //this would rather be called an IInformationDisplayer
{
     string DisplayInformation();
}

public class Student : IStudent
{
    public string Name, Grade, Age, etc... { get; set; }
    private IStudent _student = null;

    public Student() { }
    public Student(IStudent student) {  _student = student; }

    public string DisplayInformation()
    {
        return $"{_student?.DisplayInformation()}" + 
               $"{Name} - {Age} years old is in {Grade} grade";
    }
}

public class ScienceStudent : IStudent //it's still a decorator
{
    public string Labs { get; set; }
    private IStudent _student;
    public ScienceStudentDecorator(IStudent student)
    {
        _student = student;
    }

    public string DisplayInformation()
    {
        var info =  _student?.DisplayInformation();
        return $"{info}. Labse are {Labs}";
    }
}