0

I have a class, NewsItem, and I would like to create a list of child objects IList using entity framework and lazy loading.

My NewsItem class is straight forward:

public class NewsItem { public int ID { get; set; } public DateTime PublicationDate { get; set; } public DateTime LastUpdated { get; set; } public int? TopPictureID { get; set; } public virtual Picture TopPicture{get; set;} public string Headline { get; set; } public string StandFirst { get; set; } public string Body { get; set; } public IList<DirectComment> Comments { get; set; } } 

The comments class is based upon a table that can have comments for other article types (e.g. PR, blogs, features) as well as NewsItems:

public class DirectComment { public int ID { get; set; } public DateTime Published { get; set; } public string Comment { get; set; } public bool Live { get; set; } public int ArticleID { get; set; } public string ArticleType { get; set; } } 

I am unsure as to how I would get all comments for a news item, where I would have a Where statement that specifies the DirectComment.ArticleType as "newsitem" and ArticleID of, for example, 1000 - that will lazy load and also will not create an issue for Dependency injection.

I could just put something akin to the following in the NewsItem.Comments Get statement:

public class NewsItem { [...] [NotMapped] private IList<DirectComment> comments; [NotMapped] public IList<DirectComment> Comments { get { if (null == comments) { IDirectCommentRepository dcRepository = new DirectCommentRepository(); comments = dcRepository.DirectComments.Where(dc => dc.ArticleID == this.ID && dc.ArticleType == "news").ToList(); } return comments; } } } 

...however, that wouldn't be great for DI as it means that I will have to hard-code the "New DirectCommentRepository()" and it will be an issue when it comes to Unit Testing. So, is it possible for me to get around the problem?

3
  • Do not combine logic and data in a single class. A class is either for data-storage (POCO), or logic (well, the implementation of it). This will mean that you have to create a specific class for the retrieval of the data, but it will solve your problem. Commented Nov 30, 2015 at 11:39
  • @Maarten, Domain-Driven Design definitely promotes having both your data and logic in the model (and that can well be in the same class as well). See en.wikipedia.org/wiki/Anemic_domain_model Commented Nov 30, 2015 at 11:43
  • True, but it doesn't combine well with DI, which is what the OP is asking about. Commented Nov 30, 2015 at 11:44

1 Answer 1

0

You can simply inject the repository usind DI. You can use constructor injection or property injection or whichever you want. In this way, you can modify the dependency for unit testing.

If your concern is about instancing the repository when you don't need to use it, there are at least two solutions:

  • depending on your DI framework, it's possible that it supports lazy instancing (you can see this to understand what I'm speaking about: Unit.MVC4’s lazy<T> is not working in ASP.NET MVC 4 app)
  • if you don't want to use that solution, or your DI framework doesn't support it, you can inject a factory that provides instances of the repository. So, the repository are only instanced in the code path that need it. To make this feasible your factory can't be static.

In both solutions, you can inject a fake for your unit tests.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.