在扩展方法中如何根据实现类创建对象
在扩展方法中如何基于实现类创建对象。因此,在下面的代码中我想添加一个“AddRelationship”扩展方法,但是我不确定如何在扩展方法中创建一个关系对象?即不想将扩展方法与关系的这个特定实现联系起来,
public static class TopologyExtns
{
public static void AddNode<T>(this ITopology<T> topIf, INode<T> node)
{
topIf.Nodes.Add(node.Key, node);
}
public static INode<T> FindNode<T>(this ITopology<T> topIf, T searchKey)
{
return topIf.Nodes[searchKey];
}
public static bool AddRelationship<T>(this ITopology<T> topIf, INode<T> parentNode, INode<T> childNode)
{
var rel = new RelationshipImp(); // ** How do I create an object from teh implementation
// Add nodes to Relationship
// Add relationships to Nodes
}
}
public interface ITopology<T>
{
//List<INode> Nodes { get; set; }
Dictionary<T, INode<T> > Nodes { get; set; }
}
public interface INode<T>
{
// Properties
List<IRelationship<T>> Relationships { get; set; }
T Key { get; }
}
public interface IRelationship<T>
{
// Parameters
INode<T> Parent { get; set; }
INode<T> Child { get; set; }
}
namespace TopologyLibrary_Client
{
class RelationshipsImp : IRelationship<string>
{
public INode<string> Parent { get; set; }
public INode<string> Child { get; set; }
}
}
public class TopologyImp<T> : ITopology<T>
{
public Dictionary<T, INode<T>> Nodes { get; set; }
public TopologyImp()
{
Nodes = new Dictionary<T, INode<T>>();
}
}
谢谢
In an extension method how do a create an object based on the implementation class. So in the code below I wanted to add an "AddRelationship" extension method, however I'm not sure how within the extension method I can create an Relationship object? i.e. don't want to tie the extension method to this particular implementation of relationship
public static class TopologyExtns
{
public static void AddNode<T>(this ITopology<T> topIf, INode<T> node)
{
topIf.Nodes.Add(node.Key, node);
}
public static INode<T> FindNode<T>(this ITopology<T> topIf, T searchKey)
{
return topIf.Nodes[searchKey];
}
public static bool AddRelationship<T>(this ITopology<T> topIf, INode<T> parentNode, INode<T> childNode)
{
var rel = new RelationshipImp(); // ** How do I create an object from teh implementation
// Add nodes to Relationship
// Add relationships to Nodes
}
}
public interface ITopology<T>
{
//List<INode> Nodes { get; set; }
Dictionary<T, INode<T> > Nodes { get; set; }
}
public interface INode<T>
{
// Properties
List<IRelationship<T>> Relationships { get; set; }
T Key { get; }
}
public interface IRelationship<T>
{
// Parameters
INode<T> Parent { get; set; }
INode<T> Child { get; set; }
}
namespace TopologyLibrary_Client
{
class RelationshipsImp : IRelationship<string>
{
public INode<string> Parent { get; set; }
public INode<string> Child { get; set; }
}
}
public class TopologyImp<T> : ITopology<T>
{
public Dictionary<T, INode<T>> Nodes { get; set; }
public TopologyImp()
{
Nodes = new Dictionary<T, INode<T>>();
}
}
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以添加一个表示关系实现类的额外类型参数,指定它必须是 IRelationship 并具有无参数构造函数的约束:
然后您应该像这样调用它来指定 IRelationship 具体类型( 'R'类型参数):
编辑:替代方法,没有扩展方法:您定义(并稍后实例化)一个RelationshipFactory类:
You could add an extra type parameter representing the relationship implementation class, specifying the constraints that it must be an
IRelationship
and have a parameterless constructor:Then you should call it like this to specify the IRelationship concrete type (the 'R' type parameter):
EDIT: alternative approach, without extension method: you define (and later instantiate) a RelationshipFactory class:
一种方法是通过修改其接口来支持
IRelationship
或INode
类,将责任推给它们。 code> 工厂方法,例如ITopology
可以这样修改:然后
AddRelationship
看起来像这样:由于您正在使用扩展方法,因此修改这些接口可能是不可能或不需要的,但它是一种选择。
One way would be to push the responsibility to the
ITopology<T>
orINode<T>
classes by modifying their interfaces to support anIRelationship<T>
factory method, e.g.ITopology<T>
could be modified as such:And then
AddRelationship<T>
would look something like:Since you're using extension methods, modifying the interfaces might not be possible or desired, but it's an option.
解决有关您的设计的另一个(可能的)问题。当您可以简单地将 AddRelationship 方法放入拓扑实现中时,为什么还要使用扩展方法呢?在这种情况下是否有特定需要使用扩展方法?另外,是否会出现单个节点可以有多个父节点的情况?
我认为这样的事情是有序的:
不需要扩展方法,也不需要中间 IRelationship 类。这一切都取决于修改接口的能力,并且 INode 只能有一个父节点。
编辑(基于OP的评论):
考虑到您专门在使扩展方法的API看起来更干净之后,您也许可以这样做:
然后调用它是:
我还没有测试过这个,所以我可能会与指定泛型的位置无关。我不知道推理引擎是否可以推断出它需要的东西。
作为一个兴趣点,您为什么决定选择“节点有许多父母和许多孩子”而不是“节点有邻居”?我最近构建了一个图形结构,并且发现字典或节点列表足以对方向进行建模。只需再次阅读原始问题 - 关系是否应该存储在拓扑中?这会更有意义,而且这似乎就是您试图用原始扩展方法做的事情。
如果关系驻留在拓扑中,那么我会采用发布的其他答案:
并调用此:
Addressing another (possible) problem about your design. Why are you using extension methods when you could simply put the AddRelationship method within your Topology implementation? Is there a specific need to use Extension Methods in this case? Also, is there ever going to be a case where a single node can have many parents?
I'd think something like this would be in order:
No need for extension methods, or the intermediate IRelationship class. This all hinges on the ability to modify the interface for one, and that an INode can only have one parent.
Edit (Based on Comment from OP):
Considering that you're specifically after making the API for the extension method looking cleaner, you could perhaps do this:
And then calling this would be:
I haven't tested this, so I may be off with where the generics are specified. I don't know if the inference engine can deduce what it needs to here.
As a point of interest, why did you decide to go with a "node has many parents and many children" instead of "node has neighbours"? I've recently built a graph structure and I found a Dictionary or List of Nodes more than adequate for modeling the directions. Just reading over the original question again - are the relationships supposed to be stored in the Topology? That'd make a lot more sense, and is what it seems you're trying to do with your original extension method.
In the case where the relationships reside in the Topology then I'd go with the other answer posted:
And calling this: