0

I am trying to combine the data from two repositories in MVC4. I am using a join statement in my Action function to gather information from two tables. I am passing this this data onto my view. I am a little confused to how to correctly combine the data from the two repositories. Some help would be appreciated. THANKS!

In controller:

 public IOwnerRepository OwnerDB;
 public IDwellingRepository DwellingDB;
 public OwnerController() : this(new OwnerRepository(), new DwellingRepository()) {}
 public OwnerController(IOwnerRepository oRepository, IDwellingRepository dRepository)
 {
     OwnerDB    = oRepository;
     DwellingDB = dRepository;
 }

Action:

public ActionResult Account(int id, int? activelink)
{
    // Link values
    // Account         = 0
    // Listings        = 1
    // Profile         = 2
    // Create Property = 3
    // Check for a value
    // If no value set to zero
    if (!activelink.HasValue)
        activelink = 0;
    // ####ing hackers
    if (activelink > 3)
        activelink = 0;
    ViewBag.ActiveLink = activelink.ToString();
    switch (activelink)
    {
        case 0:
            ViewBag.Title = "Account Details";
        break;
        case 1:
            ViewBag.Title = "Listings";
        break;
        case 2:
            ViewBag.Title = "Profile";
        break;
        case 3:
            ViewBag.Title = "Create Property";
        break;
    }
    var oOwner = OwnerDB.FindOne(GetUserId(), id);
    if (oOwner == null)
    {
    return new FileNotFoundResult { Message = "No Account found for id " + id.ToString() };
    }
    else
    {
    return View(oOwner);
    }
}

Owner Repository:

public Owner FindOne(string UserId, int id)
    {
      var dwelling = (from o in db.Owner 
                    join d in db.Dwelling on o.ID equals d.Owner_ID into ps
                    from k in ps.DefaultIfEmpty()
                    select o );
      var oOwner = (from p in dwelling
                    where p.ID == id
                    select p);
      return oOwner.FirstOrDefault();
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
SINGULARITY
  • 1,124
  • 11
  • 11

1 Answers1

0

First question you need to ask yourself is: Do I need those 2 repositories?
Does it make sens in your domain definition to have both of them? Does Dwelling make sense without an Owner and vice versa?

One of the DDD best practices is to limit one repository per aggregate root. Have a look at this SO question where someone was wondering if there was a need to build both User and Phone repostories.

If you do need to have separate repositories then you might have to consider adding a Service layer on top of them.

Please have a look at this SO answer where I answered someone how to decouple things (there's a Unit of Work pattern in this example but it doesn't matter)

Community
  • 1
  • 1
MaxSC
  • 4,698
  • 2
  • 23
  • 36