在 Microsoft 和 StackOverflow 上找了几个小时之后,我真的不明白为什么 ICollection 是编码中的另一个类。

例如,为什么 Author.cs 文件中有一个 ICollection of Book 和一个新的书籍列表?我只是不明白。

如果有帮助,我正在使用 Visual Studio 2017。

//Author.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Library2.Models
{
    public class Author
    {
        private ICollection<Book> _books;

        public Author()
        {
            _books = new List<Book>();
        }

        public int Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public virtual ICollection<Book> Books
        {
            get { return _books; }
            set { _books = value; }
        }
    }
}

//Books.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Library2.Models
{
    public class Book
    {
        private ICollection<Author> _authors;

        public Book()
        {
            _authors = new List<Author>();
        }

        public int Id { get; set; }

        public string Title{get; set;}

        public string Publisher { get; set; }

        public virtual ICollection<Author> Authors
        {
            get { return _authors; }
            set { _authors = value; }
        }
    }
}

最佳答案

ICollection<T> 是一个通用接口(interface),它由许多常见的集合类实现,如 List<T>Arrays 。但重要的是,它确实允许 changes 到集合中,即可以添加新元素或从中删除现有元素。在这个例子中,两个类在内部实现了另一个类的 List<>,并且都公开了一个 ICollection<> 属性,允许访问内部集合。

我相当确定显示的两个类(作者和书籍)可能是来自 ORM(例如 Entity Framework )的多对多数据库序列化实体,甚至可能是生成的代码,例如来自数据库表,在这种情况下,“面向对象的良好原则”并不真正适用,因为 ORM 需要在反序列化期间写入字段

但是出于学术兴趣,我们可以将这两个类视为一流的“域”类,我们可以稍微收紧一些。

猜测一下,该示例想提供一个对多对多关系进行建模的示例,也许还展示了双向导航固有的困难,即书籍可以由多个作者编写,而作者可以编写多本书。

无论如何,我想我们可以将类层次结构用于测试驱动器。

首先我们有鸡和蛋的问题——先有什么?
从逻辑上讲,我猜在写书之前必须存在 Author,所以:

var tolkien = new Author
{
     Id = 1,
     FirstName = "J. R. R.",
     LastName = "Tolkien"
     // We *could* add some books here, but in doing so, we wouldn't be able to set
     // the linked author on the book yet, because the author hasn't been fully created
};

var lotr = new Book
{
    Id = 1,
    Title = "Lord of the Rings",
    Publisher = "Allen & Unwin",
    // We do have created the author, so we can do this ...
    Authors = new[]{ tolkien }
};

这里的问题是链接不一致 - lotr 引用 tolkien ,但 tolkien 没有书。

这需要手动修复:
tolkien.Books.Add(lotr);

由于 book 和 author 之间也有双向导航,现在两个对象之间有一个循环(无限)引用,从这个导航可以看出:
Console.WriteLine(tolkien.Books.First().Authors.First().Books.First()...);

除了双向导航,类层次结构还有很多其他问题,比如:
  • 公开对集合的直接、可变访问不是一个好主意——理想情况下,我们应该封装对内部字段的更改,如 booksauthors
  • 我们可以通过强制将 Author 作为 Book 上的构造函数参数来强制 book 和 Author 之间的“创建顺序” - 即一本书必须由作者编写。这可以防止书籍处于没有作者的暂时“无效”状态。
  • 书籍本身的创建可以通过工厂方法执行 - 这样我们可以确保“一次性”创建,没有任何暂时的无效状态。我随意将 PublishBook 方法添加到 Author ,但这可以分开。
  • 同时,我们可以通过将 Author 的 'this' 引用传递给 Book 来确保 Author 和 Book 之间的双向链接保持完整。

  • 在这一切之后,我们留下了类似的东西:
    public class Author
    {
        // Restrict creation of books to just the once, at construction time
        private readonly ICollection<Book> _books;
    
        public Author()
        {
            _books = new List<Book>();
        }
    
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    
        // Take away the setter, and prevent direct external change to the collection
        public virtual IEnumerable<Book> Books
        {
            get { return _books; }
        }
    
        // Provide a more controlled mechanism to book creation
        public void PublishBook(int id, string title, string publisher)
        {
            _books.Add(new Book(this)
            {
                Id = id,
                Title = title,
                Publisher = publisher
            });
        }
    }
    
    public class Book
    {
        private readonly ICollection<Author> _authors;
    
        // Book cannot be published without an author
        public Book(Author author)
        {
            _authors = new List<Author>{author};
        }
    
        public int Id { get; set; }
        public string Title{get; set;}
        public string Publisher { get; set; }
    
        // As above, don't allow direct change
        public virtual IEnumerable<Author> Authors
        {
            get { return _authors; }
        }
    }
    

    这有望导致更简单和更清洁的使用:
    var tolkien = new Author
    {
         Id = 1,
         FirstName = "J. R. R.",
         LastName = "Tolkien"
    };
    
    tolkien.PublishBook(1, "Lord of the Rings", "Allen & Unwin");
    

    关于c# - 浏览文档后没有掌握 ICollection 在 C# 中的作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47474237/

    10-13 03:06