2017-03-21 10 views
0

私は2つのインターフェースを持っています。私はリストと配列のためにそれらを使用したい。2つのジェネリックと無限

public interface IBook<T> 
{ 
    string Name { get; set; } 
    T Authors { get; set; } 
    int PagesCount { get; set; } 
} 

public interface IAuthor<T> 
{ 
    string Name { get; set; } 
    T Books { get; set; } 
} 

class Author<T> : IAuthor<IBook<T>[]> 
    where T : IAuthor<IBook<T>[]> 
{ 
    public string Name { get; set; } 
    public IBook<T>[] Books { get; set; } 

} 
class Book<T> : IBook<IAuthor<T>[]> 
    where T : IBook<IAuthor<T>[]> 
{ 
    public string Name { get; set; } 
    public IAuthor<T>[] Authors { get; set; } 
    public int PagesCount { get; set; } 

    public static void Main(string[] args) 
    { 
     Author<IBook<IAuthor<...>[]>[]> a = new Author<>(); 
    } 
} 

mainのようにオブジェクトを作成する方法はありますか。コンパイラは、インターフェイスとクラスの記述にエラーはないと言います。私を助けてください。

+0

をそれらをオブジェクトとして扱うか、リストを作成するか? –

+1

ここでジェネリックを使用したい理由は何ですか?また、 'Books 'と' Authors'はなぜT []の代わりに 'T'ですか?結局、それらの名前は複数形です。 –

+1

IEnumerableを実装しているIAookOolCollectionを作成し、使用します。IAuthor MistyK

答えて

3

個人的には、ここでジェネリックを使い過ぎて、これを少し複雑にしていると思います。

あなたの基準を評価:

著者(1 - >多くの)ブック
ブック(1 - >多くの)著者

あなたは以下のクラスを持っていることによってこれを行うことができます。

public class Book 
{ 
    public string Name { get; set; } 
    public Author[] Authors { get; set; } 
    public int PageCount { get; set; } 
} 

public class Author 
{ 
    public string Name { get; set; } 
    public Book[] Books { get; set; } 
} 

このようにすると、親クラス内のすべての本と著者が含まれ、linqクエリを使用して、間違いなくあなたがこのような一般的なクラスを使用しない場合、これはあなたがオブジェクトを初期化しようとするたびに、首の痛みになります原因あなたのために良いだろう

public class BookStore 
{ 
    public List<Book> Books { get; set; } 
    public List<Author> Authors { get; set; } 

    public Book GetBook(string name) 
    { 
     var query = Books.Where(b => b.Name.Equals(name)); 
     if (query.Count() == 1) 
      return query.ElementAt(0); 
     else return null; 
    } 

    public Author GetAuthor(string name) 
    { 
     var query = Authors.Where(a => a.Name.Equals(name)); 
     if (query.Count() == 1) 
      return query.ElementAt(0); 
     else return null; 
    } 
} 
1

:書籍/著者はに関連しています。少なくとも、ジェネリッククラスの既定の実装を作成します。ここでは、作成者の型を指定する必要はありません。どのようなスケーラビリティが必要か、メンテナンスが容易かどうかに依存するアプリケーションのための何らかの戦略を間違いなく実装しなければなりません.Nathangradは素晴らしい例を与えてくれました。

私はあなたのオブジェクトは、あなたの書籍の著者は、同様に直接変更し、著者の帳簿することが開いていないところ、このようなものになるはずだと思う:あなただけインスタンス化することはできません、私はあなたの質問を理解していない

public interface IBook 
{ 
    string Name { get; set; } 

    ICollection<IAuthor> GetAuthors(); 

    void AddAuthor(IAuthor book); 

    int PagesCount { get; set; } 
} 

public interface IAuthor 
{ 
    string Name { get; set; } 

    ICollection<IBook> GetBooks(); 

    void AddBook(IBook book); 
} 

public class Author : IAuthor 
{ 
    private ICollection<IBook> books; 

    public Author() 
    { 
     this.books = new HashSet<IBook>(); 
    } 


    public Author(ICollection<IBook> books) 
    { 
     this.books = books; 
    } 

    public string Name { get; set; } 

    public void AddBook(IBook book) 
    { 
     this.books.Add(book); 
    } 

    public ICollection<IBook> GetBooks() 
    { 
     return this.books; 
    } 
} 

public class Book : IBook 
{ 

    private ICollection<IAuthor> authors; 

    public Book() 
    { 
     this.authors = new HashSet<IAuthor>(); 
    } 

    public Book(ICollection<IAuthor> Authors) 
    { 
     this.authors = Authors; 
    } 

    public void AddAuthor(IAuthor author) 
    { 
     this.authors.Add(author); 
    } 

    public ICollection<IAuthor> GetAuthors() 
    { 
     return this.authors; 
    } 

    public string Name { get; set; } 

    public int PagesCount { get; set; } 
} 
関連する問題