数据库调用太多 - NHibernate

时间:2012-10-11 02:30:08

标签: c# sql nhibernate

我有一个循环记录并在每条记录上执行选择的方法。 我想要一些指导,以便聚合所有查询并在一次往返中发送到数据库。 我知道CreateMultiQuery()和CreateMultiCriteria()方法,但我不确定如何在下面的当前代码中实现?

由于

foreach (Roll roll in attendanceRegisterRolls.Items)
                {
                    RollAttendeeUpdater attendeeUpdater = new RollAttendeeUpdater(enrolmentRepository, roll);
                    attendeeUpdater.**AddNewStudentEnrolmentsAsAttendees();**
                    attendeeUpdater.RefreshEffectiveAttendance(register);
                }

**Constructor**
public RollAttendeeUpdater(IEnrolmentRepository enrolmentRepository, Roll roll)
    {
        _roll = roll;
        _enrolments = enrolmentRepository.GetByRoll(_roll);
    }

public interface IEnrolmentRepository : IRepository<Enrolment>
{
    IEnumerable<Enrolment> GetByRoll(Roll roll);
    bool IsStudyCompleted(string resultCode);
}
public IEnumerable<Enrolment> GetByRoll(Roll roll)
    {
        var query = _session.CreateQuery(
            @"select e 
            from Enrolment e 
            join fetch e.Student 
            where e.Roll.Id.RollNumber = :rollNumber and e.Roll.Id.Year = :year
            order by e.EnrolmentStatus");

        query.SetString("rollNumber", roll.Id.RollNumber);
        query.SetString("year", roll.Id.YearAsTwoDigitString);
        return query.List<Enrolment>();
    }
public void **AddNewStudentEnrolmentsAsAttendees()**
    {

        foreach (Enrolment enrolment in _enrolments)
        {
            Student student = enrolment.Student;

            if (student.IsActive)
                if (_roll.HasAttendee(student.Id) == false)
                    _roll.AddAttendee(student, new DateTimeRange(enrolment.StudyStartDate, enrolment.StudyEndDate));
        }
    }

public virtual bool HasAttendee(string studentId)
    {
        return _attendees.Any(a => a.Student.Id == studentId);
    }

AddNewStudentEnrolmentsAsAttendees()和enrolmentRepository.GetByRoll(_roll)方法返回以下SQL:

select
 enrolment0_.id_enrolment as id1_1_0_,
 enrolledst1_.id_student as id1_10_1_,
 enrolment0_.study_start_date as study2_1_0_,
 enrolment0_.study_end_date as study3_1_0_,
 enrolment0_.id_eft as id4_1_0_,
 enrolment0_.enrolment_date as enrolment5_1_0_,
 enrolment0_.enrolment_start_date as enrolment6_1_0_,
 enrolment0_.enrolment_status as enrolment7_1_0_,
 enrolment0_.fee_band as fee8_1_0_,
 enrolment0_.id_course as id9_1_0_,
 enrolment0_.limitation_date as limitation10_1_0_,
 enrolment0_.result_date as result11_1_0_,
 enrolment0_.roll_number as roll12_1_0_,
 enrolment0_.year as year1_0_,
 enrolment0_.id_student as id14_1_0_,
 enrolment0_.result_code as result15_1_0_,
 enrolledst1_.surname as surname10_1_,
 enrolledst1_.given_names as given3_10_1_,
 enrolledst1_.preferred_name as preferred4_10_1_,
 enrolledst1_.title as title10_1_,
 enrolledst1_.gender as gender10_1_,
 enrolledst1_.tafe_international_id as tafe7_10_1_,
 enrolledst1_.active_ind as active8_10_1_,
 enrolledst1_.date_of_birth as date9_10_1_ 
from dt_modular_enrolment enrolment0_ inner join dt_student enrolledst1_ on enrolment0_.id_student=enrolledst1_.id_student 
where (
enrolment0_.roll_number=? )
and
(
    enrolment0_.year=? )
order by  enrolment0_.enrolment_status; p0 = 'AH0130',
p1 = '12'

SELECT
 attendees0_.roll_number as roll5_1_,
 attendees0_.year as year1_,
 attendees0_.id_class_attendee as id1_1_,
 attendees0_.id_class_attendee as id1_12_0_,
 attendees0_.attendance_start_date as attendance2_12_0_,
 attendees0_.attendance_end_date as attendance3_12_0_,
 attendees0_.enrolled_ind as enrolled4_12_0_,
 attendees0_.roll_number as roll5_12_0_,
 attendees0_.year as year12_0_,
 attendees0_.prospective_ind as prospect7_12_0_,
 attendees0_.student_identification as student8_12_0_ 
FROM dt_class_attendee attendees0_ 
WHERE attendees0_.roll_number=? and
 attendees0_.year=?; p0 = 'AH0130',
p1 = '12'

1 个答案:

答案 0 :(得分:0)

也许你应该给RollAttendeeUpdater更新整套卷 - 它有足够的知识同时为所有卷做一些GetByRoll()变种。

或者,让调用者完成查询并传入滚动和注册列表,而不是传递存储库实例。