简体   繁体   English

按ID添加列表项

[英]Add list item by ID

Let's say: 比方说:

public class Parent
{
    public virtual IList<Child> Childs { get; set; }

    public void AddChild(long childId)
    {
        // Link existing child to parent.
    }
}

I'm trying to implement DDD using NHibernate so I wonder how to link child item to parent without retrieving it from database. 我正在尝试使用NHibernate实现DDD,所以我想知道如何在不从数据库中检索子项的情况下将其链接到父项。

You can't. 你不能 The object oriented approach would be: 面向对象的方法将是:

public class Parent
{
    public virtual IList<Child> Childs { get; set; }

    public void AddChild(Child child)
    {
        child.Parent = this;
        Childs.Add(child);
    }
}

The code that calls this method could add a child without retrieving it using ISession.Load : 调用此方法的代码可以添加一个孩子,而无需使用ISession.Load检索它:

var child = session.Load<Child>(childId);
parent.AddChild(child);

Load will create a dynamic proxy with the ID set. 加载将创建一个具有ID集的动态代理。 Note that child will be loaded if you access any of its other properties, and that accessing the parent's Childs collection will cause it to be loaded. 请注意,如果您访问子级的任何其他属性,则会加载子级,而访问父级的“子级”集合将导致其加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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