0
\$\begingroup\$

Given the following two interfaces what would the proper way to set up the two actual classes?

public interface IPortfolio
{
    string FirstName { get; set; }
    string LastName { get; set; }
    string Name { get; set; }
    int Id { get; set; }
    List<IDocument> Documents { get; set; }
}
public interface IDocument
{
    string ExtensionType { get; set; }
    int Id { get; set; }
    DateTime? DateFiled { get; set; }
    int? Size { get; set; }
    int? SortOrder { get; set; }
}

The actual classes:

 public class Portfolio : IPortfolio
{
    public Portfolio()
    {
        Documents = new List<IDocument>();
    }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Name { get; set; }
    public int Id { get; set; }
    public List<IDocument> Documents { get; set; }
}

public class Document : IDocument
{
     public Document()
     {
       Pages = new List<IPage>();
     }
     public string ExtensionType { get; set; }
     public int Id { get; set; }
     public DateTime? DateFiled { get; set; }
     public int? Size { get; set; }
     public List<IPage> Pages { get; set; }
     public int? SortOrder { get; set; }
}

When using the code above I get an error about not being able to implicitly convert List<Document> to List<IDocument>

This is a learning project to understand the concept of Interfaces and how they should best be used.

\$\endgroup\$

closed as off-topic by 200_success, 1201ProgramAlarm, Heslacher, yuri, Pieter Witvoet May 30 at 9:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, 1201ProgramAlarm, Heslacher, yuri, Pieter Witvoet
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ It sounds like you plan to create only one implementation per interface. If that's the case, then what's the reason for having those interfaces? \$\endgroup\$ – Pieter Witvoet May 29 at 22:41
3
\$\begingroup\$

There no such thing like an interface class (term used in initial version of post). You have interfaces and classes implementing the interfaces.

I don't see any List<Document> in your code, but If you have defined the list as List<IDocument>, you must stick to this definition. You cannot assign it a List<Document>. But you can add Document objects to this list.

Generally two types T<A> and T<B> are not assignment compatible, even if A and B are assignment compatible. An exception is when the in or out keywords are used in generic interfaces. But this can only be done if the generic type occurs exclusively as input or exclusively as output as explained in Variance in C#.NET 4.0 Covariance and Contravariance.

I would convert the list properties Documents and Pages to getter-only properties.

public interface IPortfolio
{
    string FirstName { get; set; }
    string LastName { get; set; }
    string Name { get; set; }
    int Id { get; set; }
    List<IDocument> Documents { get; }
}

And create the class like this

public class Portfolio : IPortfolio
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Name { get; set; }
    public int Id { get; set; }
    public List<IDocument> Documents { get; } = new List<IDocument>();
}

This makes the Documents property read-only. The list itself remains read/write.

var portfolio = new Portfolio();
portfolio.Documents.Add(new Document());
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.