简体   繁体   中英

Generic Objects C#

So I have been working with the Neo4jClient library for C# and I am fairly new to both worlds.

I have this POCO here:

public class SetEntity
{
    public string GUID { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string CreatedDate { get; set; }
}

This object class is used in various methods, one in particular for creating a relationship between two nodes however I have to explicitly say which POCO is used to create it IRelationshipAllowingSourceNode<SetEntity> and IRelationshipAllowingTargetNode<EntityInstance> . Below is the entire class that handles that.

class GraphRelationshipEntityInstanceToSetEntity : Relationship, IRelationshipAllowingSourceNode<EntityInstance>, IRelationshipAllowingTargetNode<SetEntity>
    {
        string RelationshipName;

        public GraphRelationshipEntityInstanceToSetEntity(NodeReference targetNode)
            : base(targetNode)
        {

        }

        public GraphRelationshipEntityInstanceToSetEntity(string RelationshipName, NodeReference targetNode)
            : base(targetNode)
        {
            this.RelationshipName = RelationshipName;
        }

        public override string RelationshipTypeKey
        {
            get { return RelationshipName; }
        }
    }

Is there a way that I can pass <SetEntity> or any other objects into IRelationshipAllowingSourceNode<Object> . I see it as unnecessary to create this class for every node type that will have a relationship with another node type.

I'm not familiar with the Neo4jclient but can comment on generics in c#.

In c# you can define an interface with which is said to have an open generic type. That is, the neo4jclient presumably declares an interface IRelationshipAllowingSourceNode<T> with some method on which presumably use an instance of T/returns T.

This is said to be an interface with an open generic type.

When you implement that interface you have close the open generic type by specifying the exact type you're working with. You can however, make your class use two open generic types as follows, and then close the generic types when you instantiate GraphRelationshipEntityInstanceToSetEntity. See below.

class GraphRelationshipEntityInstanceToSetEntity<T, T1> : Relationship, IRelationshipAllowingSourceNode<T>, IRelationshipAllowingTargetNode<T1>
    {
        string RelationshipName;

        public GraphRelationshipEntityInstanceToSetEntity(NodeReference targetNode)
            : base(targetNode)
        {

        }

        public GraphRelationshipEntityInstanceToSetEntity(string RelationshipName, NodeReference targetNode)
            : base(targetNode)
        {
            this.RelationshipName = RelationshipName;
        }

        public override string RelationshipTypeKey
        {
            get { return RelationshipName; }
        }
    }

See here for another question which has been answered on generics:

Generics -Open and closed constructed Types

Hope that this helps.

Tim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM