NHibernate Aggregate Traversal (C #)

My application has a very simple model now, and I'm trying to find the best way to get through the aggregate. As you can see in my model diagram below, I have an account, a trip, and a list of people visiting the trip. I would like to be able to view all the trips the account is on, and I came 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 person list contains this accountid // then add this trip to the userTrips list. if(person.Account.Id == accountid) userTrips.Add(trip); } } return userTrips; } 

It seems to me not very effective and that I do not think about things correctly. Do any of you have any ideas on a better way to implement this? Perhaps I'm thinking wrong about my model? Thanks

alt text http://i43.tinypic.com/214qb0l.png

+4
source share
3 answers

Hey guys, thanks for your help. I think last night I realized what I'm trying to achieve. I'm not quite sure if this is the best way, but it still works. I was able to come up with an interface similar to 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 status for a trip. A user must pay some kind of deposit //before they are considered active. var userStatus = userTrips.SingleOrDefault().GetStatusFor(user); 

To do this, I created a many-to-many table that stores the primary keys for the user and the trip, and then the relationships in NHibernate are mapped to the user class and disconnect class. Also, to achieve user status for the trip, I created an object that stores the state of the user along with trip information and the user. This is a little duplication of data, it seems, but I like the way it works and looks.

If you have any comments or a better way to implement this, let me know. I always try to improve something! Thanks again

alt text http://i41.tinypic.com/10yfncw.png

+1
source

I do not know that this does not seem right to you, it is not. Infact its so wrong that it hurts! But instead of just giving you grief, poorly explain the whole reason why this is wrong @

Firstly, your download in ALL cases is brought into memory from the database, this essentially means that you request a data warehouse to get the entire amount of data that you do not need, and then transfer it via cable. This is normal if you have several trips, but it will not scale at all.

Then you return through each trip and name the property "trip.People". Then it will go back to the database and upload ALL the data for people on EVERY one of these trips. Again, this will kill you if you have multiple trips with multiple participants. Now, this assumes that you do not have filters on your mappings, or you specifically asked NHibernate not to be lazy to download the People collection, but in any case you do not want to download all the information.

My advice would be to look at NHibernate docs regarding querying your object model using either HQL or Linq-To-NHibernate and you will get queries that look something like this:

HQL (MY HQL Sucks, so this can be very wrong):

var hql = @ "from Trip as t Join t.People as p with p.Account.Id =: accountId select t"

Edit:

Actually, my brain is a little slow right now, because it is late, but I just realized that you are doing it a bit here. In fact, in fact, after all the trips that a person had, why does your account object have no correlation with traffic? You should essentially aim for something like this:

var trip = accountRepo.GetAccount (123) .Trips;

Edit:

I'm tired again, so that might be nonsense, but I think the matching you are looking for will look like this:

  <bag name="Trip" cascade="all" table="TripToAccount" lazy="true"> <key column="AccountId" /> <many-to-many class="Trip"> <column name="TripId" not-null="true"/> </many-to-many> </bag> 

Edit:

Gosh, I have to go to bed. Now I see that you already have a comparison between people and their trips, so why not:

var query = "from TripPeople as tp Where tp.Account.Id =: accountId AND tp.IsActive = true select tp.Trip"

Now we stop responding before doing more stupid things.

+3
source

Owen said that this is the best way to do this if you have not loaded your trips before you hit this method.

I suggest building some sort of compare () method for the List of travel class

public class Trips: List <Ride> {

 public Trips() { // // TODO : // } #region methods /// <summary> /// return trip /// </summary> public Trip FindTrip(int accountId) { return this.Find(delegate(trip t) { return t.AccountId == accountId; }); } #end region 

}

I can’t be sure it’s better, but the way I do it is hahan hahan it looks like the Find () method of an object class

0
source

Source: https://habr.com/ru/post/1285924/


All Articles