gpt4 book ai didi

c# - 查看文档后不了解 ICollection 在 C# 中的作用

转载 作者:行者123 更新时间:2023-11-30 23:07:16 25 4
gpt4 key购买 nike

在 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);

由于书籍和作者之间也存在双向导航,因此现在这两个对象之间存在循环(无限)引用,从这个导航可以看出:

Console.WriteLine(tolkien.Books.First().Authors.First().Books.First()...);

除了双向导航之外,类层次结构还有很多其他问题,例如:

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

在这一切之后,我们剩下的是:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com