My application has a very simple model right now and I'm trying to find the best way to traverse through an aggregate. As you can see in my model diagram at the bottom, I have an account, a trip and a list of people attending a trip. I would like to be able to view all of the trips an account is apart of and I have come up with something like this:
public List<Trip> GetAllTripsFor(int accountid)
{
var tripRepository = new TripRepository();
var trips = tripRepository.FindAll();
var userTrips = new List<Trip>();
foreach (Trip trip in trips)
{
foreach (TripPeople person in trip.People)
{
// If this trip's person list contains this accountid
// then add this trip to the userTrips list.
if(person.Account.Id == accountid)
userTrips.Add(trip);
}
}
return userTrips;
}
To me this doesn't seem very efficient and that I'm not thinking about things correctly. Do any of you have ideas on a better way of implementing this? Perhaps I'm thinking about my model wrong? Thanks
thanks for your help. I think I figured out last night what I was trying to achieve. I'm not totally sure it's the best way but it is working good so far. I was able to come up with an interface like the following:
//Return a list of trips for the user
var user = accountRepository.GetUserBy(123);
var userTrips = user.Trips;
//Return all of the users attending a trip
var peopleAttendingTrip = tripRepository.GetTripBy(234).People;
//Check user's status for a trip. A user must pay some kind of deposit
//before they are considered active.
var userStatus = userTrips.SingleOrDefault().GetStatusFor(user);
To achieve this I created a many-to-many table that holds the the primary keys from User and Trip and then mapped the relationship in NHibernate to the User Class and Trip Class. Also to achieve the user's status for a trip, I created an Entity that stores the user's state along with the trip and user info. That's a little duplication of data it seems like but I like the way it currently works and looks.
If you guys have any comments or a better way to implement this let me know. I'm always up for improving something! Thanks again