如何在视图模型中处理循环引用

时间:2018-12-04 09:53:25

标签: c# asp.net asp.net-mvc model-view-controller

问题很简单:

public class UserViewModel {
    public string Name {get; set;}
    public IEnumerable<BookViewModel> Books {get; set;} 

    public UserViewModel FromUser(User user) {
        this.Name = user.Name;
        this.Books = user.Books.select(x => new BookViewModel.FromBook(x)).ToList();
    }
}

public class BookViewModel : Entity {
    public string Title {get; set;}
    public UserViewModel User {get; set;}

    public BookViewModel FromBook(Book book) {
        this.Title = book.Title;
        this.User = UserViewModel.FromUser(book.User);
    }
}

解决该问题的最佳方法是什么?当我得到UserViewModel时,我将去BookViewModel,然后从那里回到UserViewModel,wchich给了我stack Exception

0 个答案:

没有答案