NHibernate选择Query Over优化用户角色基础案例

时间:2013-12-30 09:23:28

标签: c# linq nhibernate

我有一个复杂的选择程序,我已经解决了5个查询事务,所以我在这里发布,以获得建议,使其更好。希望有人可以使它成为单一交易。 我在这里使用nhibernate查询。

我有7个表 t_user t_category t_event t_announcement t_event_follower < / strong>, t_event_register t_event_subcribe ,我将在下面的课程中代表它:

class User {
  int id;
  string username;
  IList<Registration> registrations;
  IList<Category> subscribes;
  IList<Event> follows;
}

class Category {
  int id;
  string description;
  IList<User> subscribers;
}

class Event{
  int id;
  string title;
  Category category;
  IList<Registration> registrations;
  IList<User> followers;
  IList<Announcement> announcements;
}

class Announcement {
  int id;
  string title;
  Event event;
  bool isForFollower;
  bool isForRegister;
  bool isForSubscriber;
}

class Registration {
  int id;
  Event event;
  User user;
  string additionalInfo;
  RegistrationStatus status;
}

enum UserRole {
  PUBLIC,
  FOLLOWER,
  SUBSCRIBER,
  REGISTERED
}

目标是根据该事件的用户角色显示1个事件中的所有公告,所以我先做的是在列表中填充用户角色,代码如下:

User requester = _userRepo.SearchBy(username); // 1 query for user detail

// 3 lazy load objects make nhibernate to load the object from db, i assume it takes 3 transactions if objects haven't load in session
bool isFollower = requester.Follows.Any(eventz => eventz.Id == eventId);
bool isSubscriber = requester.Subscribes.Any(category => category.Events.Any(eventz => eventz.Id == eventId));
bool isRegistered = requester.Registrations.Any(registration => registration.Event.Id == eventId);
var userRoleList= new List<UserRole>();

if (isFollower) userRoleList.Add(UserRole.FOLLOWER);
if (isSubscriber) userRoleList.Add(UserRole.SUBSCRIBER);
if (isRegistered) userRoleList.Add(UserRole.REGISTERED);

最后使用下面的存储库层中的代码,我创建了用于查询的动态析取:

Disjunction filterRestriction = Restrictions.Disjunction();
// this condition to select announcement which is for public not specific
filterRestriction.Add(Restrictions.Where<Announcement>(a => a.IsForFollower == false
                                                            && a.IsForSubscriber == false
                                                            && a.IsForRegistered == false));

foreach (UserRole role in userRoleList)
{
    switch (role)
    {
        case UserRole.FOLLOWER:
            filterRestriction.Add(Restrictions.Where<Announcement>(a => a.IsForFollower));
            break;
        case UserRole.SUBSCRIBER:
            filterRestriction.Add(Restrictions.Where<Announcement>(a => a.IsForSubscriber));
            break;
        case UserRole.REGISTERED:
            filterRestriction.Add(Restrictions.Where<Announcement>(a => a.IsForRegistered));
            break;
    }
}
//this is the last query transaction, so total 5 times hit database CMIIW, 
return _session.QueryOver<Announcement>()
               .Where(filterRestriction)
               .And(a => a.Event.Id == id)
               .List();

因此,通过以上代码查询结果将显示公告以及基于用户在相关事件中具有的角色的特定公告。 我承认这是我能想到解决案例的最简单的解决方案,但有人可以通过一个简单的操作提出建议或重做这个问题,或者只是用更好的流程重新制作它? 我真的很感激。

0 个答案:

没有答案