0

I'm kinda curious if there's a better way to accomplish what the following. Basically, I have two classes. ClassA and ClassB. ClassA has a foreign key to ClassB. I want to, from ClassB, determine all the elements from ClassA that reference ClassB. Hopefully this small example will help make more sense:

It's also worth noting I'm trying to mimic the ASP.NET MVC 4 book, in terms of "convention":

(In Entities Folder)

public class ClassA
{
   [Key]
   public int Id {get; set;}
   public ClassB MyClassB {get;set;}
}

public class ClassB
{
   [Key]
   public int Id {get; set;}
   public String title {get; set;}

   virtual public IEnumerable<ClassA> ClassAs(IClassA a)
   { return a.ClassA.Where(....).AsEnumerable(); }
}

(In Abstract Folder)

public interface IClassA
{
   IQueryable<ClassA> ClassA { get; set; }
}

public interface IClassB
{
   IQueryable<ClassB> ClassB { get; set; }
}

Where I'm seeing the concern is the need to pass an instance of the object I have (In this case an instance of ClassA to the method ClassAs.

Thinking about this further, it's possible to probably also create the definition within the interface, then declare it in the concrete folder. What's the "best" way to do this? Understanding how the EF still works regarding joins is still a bit of a mystery, any interesting resources would be appreciated, too.

1 Answers1

0

If I understood you correctly, couldn't you just make a List<ClassA> in ClassB:

public virtual List<ClassA> ClassAs { get; set; }

This would be populated by EF and will contain all ClassA that have a reference to ClassB in MyClassB.

JLe
  • 2,835
  • 3
  • 17
  • 30
  • Thanks for the option here. Is there an easy way to test something like this, without the requirement of the database for the EF? The reason I ask is because I spent about an hour building another app to try testing these joins. Here's a link to my gist that gives the basics of the test I just did [https://gist.github.com/TheDarkTrumpet/0232e0d0289f06d95674](https://gist.github.com/TheDarkTrumpet/0232e0d0289f06d95674). – TheDarkTrumpet Jul 23 '13 at 00:25
  • I found a pretty interesting article talking about the testing of more tightly bound EF-problems, kinda like this one. I guess the reason why I'm running into this issue with the public virtual not working is because the real "EF" is not there, so this kinda thing isn't possible to test using simple moq. http://stackoverflow.com/questions/3463017/unit-testing-with-ef4-code-first-and-repository?rq=1 – TheDarkTrumpet Jul 23 '13 at 01:10