Reputation: 2842
I am building an app with NO persistence data. So it will be in memory.
I have the following POCO entities
public class Book
{
public Book()
{
BorrowedBooks = new List<BorrowedBooks>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public virtual ICollection<BorrowedBooks> BorrowedBooks { get; set; }
}
public class Borrower
{
public Borrower()
{
BorrowedBooks = new List<BorrowedBooks>();
}
public int Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public virtual ICollection<BorrowedBooks> BorrowedBooks { get; set; }
}
public class BorrowedBooks
{
public int Id { get; set; }
public int BookId { get; set; }
public int BorrowerId { get; set; }
public DateTime DateBorrowed { get; set; }
public virtual Book Book { get; set; }
public virtual Borrower Borrower { get; set; }
}
I have created a class that will populated some sample data
public class DemoData
{
static Book book1 = new Book { Id = 1, Title = "Queen of the road", Author = "Tricia Stringer" };
static Book book2 = new Book { Id = 2, Title = "Don't look now", Author = "Paul Jennings" };
static Book book3 = new Book { Id = 3, Title = "Too bold to die", Author = "Ian McPhedran" };
static Book book4 = new Book { Id = 4, Title = "The rosie project", Author = "Graeme Simson" };
static Book book5 = new Book { Id = 5, Title = "In great spirits", Author = "Archie Barwick" };
static Book book6 = new Book { Id = 6, Title = "The vale girl", Author = "Nelika Mcdonald" };
static Book book7 = new Book { Id = 7, Title = "Watching you", Author = "Michael Robotham" };
static Book book8 = new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" };
static Borrower borrower1 = new Borrower { Id = 1, Firstname = "John", Lastname = "Smith" };
static Borrower borrower2 = new Borrower { Id = 2, Firstname = "Mary", Lastname = "Jane" };
static Borrower borrower3 = new Borrower { Id = 3, Firstname = "Peter", Lastname = "Parker" };
static Borrower borrower4 = new Borrower { Id = 4, Firstname = "Eddie", Lastname = "Brock" };
static BorrowedBooks borrowed1 = new BorrowedBooks { BookId = 8, Book = book8, BorrowerId = 2, Borrower=borrower2, DateBorrowed = DateTime.Parse("01/04/2014") };
static BorrowedBooks borrowed2 = new BorrowedBooks {BookId = 6, Book = book6, BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014")};
static BorrowedBooks borrowed3 = new BorrowedBooks { BookId = 2, Book = book2, BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014") };
static BorrowedBooks borrowed4 = new BorrowedBooks { BookId = 1, Book = book1, BorrowerId = 1, Borrower = borrower1, DateBorrowed = DateTime.Parse("26/03/2014") };
public List<BorrowedBooks> borrowedBooks = new List<BorrowedBooks>
{
borrowed1, borrowed2, borrowed3, borrowed4
};
public List<Book> books = new List<Book>
{
book1, book2, book3, book4, book5, book6, book7, book8
};
private List<Borrower> borrowers = new List<Borrower>
{
borrower1, borrower2, borrower3, borrower4
};
}
data access code
public class BookRepository : IBookRepository
{
private DemoData data = new DemoData();
public bool Add(Book book)
{
try
{
this.data.books.Add(book);
}
catch (Exception ex)
{
return false;
}
return true;
}
public bool BorrowBook(BorrowedBooks details)
{
try
{
this.data.borrowedBooks.Add(details);
}
catch (Exception ex)
{
return false;
}
return true;
}
public IEnumerable<Book> Search()
{
return data.books;
}
}
controller code
public class BookController : Controller
{
private IBookRepository _bookRepo;
public BookController(IBookRepository bookRepo)
{
_bookRepo = bookRepo;
}
public ActionResult Search()
{
var test = _bookRepo.Search();
return View(test);
}
}
But when I get the data from the repository, the navigation properties are empty... what am I doing wrong?
Upvotes: 0
Views: 64
Reputation: 13495
I think you need to make your lists static in the DemoData
class as well and remove the instance private members of DemoData
from you repositories. That way all your repositories will use the same data.
public static class DemoData
{
static Book book1 = new Book { Id = 1, Title = "Queen of the road", Author = "Tricia Stringer" };
static Book book2 = new Book { Id = 2, Title = "Don't look now", Author = "Paul Jennings" };
static Book book3 = new Book { Id = 3, Title = "Too bold to die", Author = "Ian McPhedran" };
static Book book4 = new Book { Id = 4, Title = "The rosie project", Author = "Graeme Simson" };
static Book book5 = new Book { Id = 5, Title = "In great spirits", Author = "Archie Barwick" };
static Book book6 = new Book { Id = 6, Title = "The vale girl", Author = "Nelika Mcdonald" };
static Book book7 = new Book { Id = 7, Title = "Watching you", Author = "Michael Robotham" };
static Book book8 = new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" };
static Borrower borrower1 = new Borrower { Id = 1, Firstname = "John", Lastname = "Smith" };
static Borrower borrower2 = new Borrower { Id = 2, Firstname = "Mary", Lastname = "Jane" };
static Borrower borrower3 = new Borrower { Id = 3, Firstname = "Peter", Lastname = "Parker" };
static Borrower borrower4 = new Borrower { Id = 4, Firstname = "Eddie", Lastname = "Brock" };
static BorrowedBooks borrowed1 = new BorrowedBooks { BookId = 8, Book = book8, BorrowerId = 2, Borrower=borrower2, DateBorrowed = DateTime.Parse("01/04/2014") };
static BorrowedBooks borrowed2 = new BorrowedBooks {BookId = 6, Book = book6, BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014")};
static BorrowedBooks borrowed3 = new BorrowedBooks { BookId = 2, Book = book2, BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014") };
static BorrowedBooks borrowed4 = new BorrowedBooks { BookId = 1, Book = book1, BorrowerId = 1, Borrower = borrower1, DateBorrowed = DateTime.Parse("26/03/2014") };
public static List<BorrowedBooks> borrowedBooks = new List<BorrowedBooks>
{
borrowed1, borrowed2, borrowed3, borrowed4
};
public static List<Book> books = new List<Book>
{
book1, book2, book3, book4, book5, book6, book7, book8
};
private static List<Borrower> borrowers = new List<Borrower>
{
borrower1, borrower2, borrower3, borrower4
};
}
Then in your book repository, you access via the static members rather than an instance field.
public class BookRepository : IBookRepository
{
public bool Add(Book book)
{
try
{
DemoData.books.Add(book);
}
catch (Exception ex)
{
return false;
}
return true;
}
public bool BorrowBook(BorrowedBooks details)
{
try
{
DemoData.borrowedBooks.Add(details);
}
catch (Exception ex)
{
return false;
}
return true;
Upvotes: 0
Reputation: 4559
You need to fill collections for books in DemoData
. If you don't set them yourself they are null. So in short if you don't use any persistence framework you must create relations from both sides.
For example for book1
you'll need to add:
book1.BorrowedBooks.Add(borrowed4);
And so on for all collections in all entities in your in memory database.
Upvotes: 1