Let's say I'm making a library database with books and authors, and I want to model the domain with interfaces, should I also specify the relations in the interfaces?
For example, if I have the following interfaces:
public interface IAuthor
{
public string Name { get; }
}
public interface IBook
{
public string Title { get; }
public string Isbn { get; }
public IEnumerable<IAuthor> BookAuthors { get; }
}
public interface ILibrary
{
public string Name { get; }
public IEnumerable<IBook> LibraryBooks { get; }
}
Then my entities would look like this:
public class Author : IAuthor
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Book : IBook
{
public int Id { get; set; }
public string Title { get; set; }
public string Isbn { get; set; }
public List<Author> Authors { get; set; }
[NotMapped]
public IEnumerable<IAuthor> BookAuthors // Clunky
{
get => Authors;
set => Authors = value.Select(a => a as Author).ToList();
}
}
public class Library : ILibrary
{
public int Id { get; set; }
public string Name { get; set; }
public List<Book> Books { get; set; }
[NotMapped]
public IEnumerable<IBook> LibraryBooks // Clunky
{
get => Books;
set => Books = value.Select(b => b as Book).ToList();
}
}
But the whole casting process from IEnumerable<ISomething>
to List<Something>
(or ICollection
or IEnumerable
or HashSet
or what have you) seems a bit clunky. Is there not a more elegant way?
interface IBook<Author> where Author: IAuthor { ... }
.