0

我有两个 POCO 对象:

///<summary>
///Auto-generated concrete POCO for persistance use <strong>Log</strong>
///</summary>
public class Log : ILogExtended
{
    #region Primitive Properties

    ///<summary>
    /// The log entry ID
    ///</summary>
    public virtual int LogId { get; set; }

    ///<summary>
    /// The timestamp of the log entry (UTC)
    ///</summary>
    public virtual DateTime TimeStamp { get; set; }

    ///<summary>
    /// The thread ID that generated the log entry
    ///</summary>
    public virtual string Thread { get; set; }

    ///<summary>
    /// The severity of the log entry
    ///</summary>
    public virtual string Severity { get; set; }

    ///<summary>
    /// The source module of the log entry
    ///</summary>
    public virtual string Source { get; set; }

    ///<summary>
    /// The log entry message
    ///</summary>
    public virtual string Message { get; set; }

    ///<summary>
    /// The associated exception text associated with the log entry
    ///</summary>
    public virtual string Exception { get; set; }
    #endregion

    #region Navigation Properties

    ///<summary>
    /// The user associated with the log entry
    ///</summary>
    public virtual UserProfile UserProfile { get; set; }
    #endregion

    #region Navigation Collections
    #endregion
} 

///<summary>
///Auto-generated concrete POCO for persistance use <strong>UserProfile</strong>
///</summary>
public class UserProfile : IUserProfileExtended
{
    #region Primitive Properties

    ///<summary>
    /// The unique ID of the profile
    ///</summary>
    public virtual int UserID { get; set; }

    ///<summary>
    /// The given name of the profile
    ///</summary>
    public virtual string GivenName { get; set; }

    ///<summary>
    /// The middle name of the profile
    ///</summary>
    public virtual string MiddleName { get; set; }

    ///<summary>
    /// The family name of the profile
    ///</summary>
    public virtual string FamilyName { get; set; }

    ///<summary>
    /// The user name of the profile
    ///</summary>
    public virtual string UserName { get; set; }

    ///<summary>
    /// The nickname of the profile
    ///</summary>
    public virtual string NickName { get; set; }

    ///<summary>
    /// The first address line of the profile
    ///</summary>
    public virtual string Address1 { get; set; }

    ///<summary>
    /// The second address line of the profile
    ///</summary>
    public virtual string Address2 { get; set; }

    ///<summary>
    /// The mailing address city of the profile
    ///</summary>
    public virtual string City { get; set; }

    ///<summary>
    /// The mailing address state of the profile
    ///</summary>
    public virtual string State { get; set; }

    ///<summary>
    /// The mailing address zip code of the profile
    ///</summary>
    public virtual string ZipCode { get; set; }

    ///<summary>
    /// The user's self description
    ///</summary>
    public virtual string Bio { get; set; }

    ///<summary>
    /// The user's birth date.
    ///</summary>
    public virtual Nullable<DateTime> BirthDate { get; set; }

    ///<summary>
    /// The user's photo URL
    ///</summary>
    public virtual string LinkToAvatar { get; set; }

    ///<summary>
    /// The user's website URL
    ///</summary>
    public virtual string LinkToWebpage { get; set; }
    #endregion

    #region Navigation Properties
    #endregion

    #region Navigation Collections

    ///<summary>
    /// The log entries of the user's actions
    ///</summary>
    public virtual IList<Log> ActivityLogs { get; set; }
    #endregion
} 

这些对象在数据库中的映射如下:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                    assembly="C3.DataModel.Generated"
                    namespace="C3.DataModel">
    <class name="Log" table="Logs">
        <id name="LogId">
            <generator class="identity" />
        </id>
        <property name="TimeStamp" column="TimeStamp" index="ixLogTimeStamp" not-null="true" />
        <property name="Thread" length="255" column="Thread" not-null="true" />
        <property name="Severity" column="Severity" not-null="true" />
        <property name="Source" length="255" not-null="false" column="Source" />
        <property name="Message" length="4000" not-null="true" column="Message"/>
        <property name="Exception" length="4000" column="Exception"/>
        <many-to-one name="UserProfile" column="UserID" cascade="none" not-found="ignore"/>
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                    assembly="C3.DataModel.Generated"
                    namespace="C3.DataModel">
    <class name="UserProfile" table="UserProfiles">
        <id name="UserID">
            <generator class="assigned" />
        </id>
        <property name="GivenName" length="40" column="GivenName" />
        <property name="MiddleName" length="40" column="MiddleName" not-null="false" />
        <property name="FamilyName" length="40" column="FamilyName" />
        <property name="UserName" length="250" index="ixUserName" unique="true" />
        <property name="NickName" length="40" column="NickName" />
        <property name="Address1" length="50" column="Address1" />
        <property name="Address2" length="50" column="Address2" />
        <property name="City" length="50" column="City" />
        <property name="State" length="2" column="State" />
        <property name="ZipCode" length="10" column="ZipCode" />
        <property name="Bio" length="2000" column="Bio" not-null="false" />
        <property name="BirthDate" not-null="false" column="BirthDate" />
        <property name="LinkToAvatar" length="250" column="LinkToAvatar" not-null="false" />
        <property name="LinkToWebpage" length="250" column="LinkToWebPage" not-null="false" />
    </class>
</hibernate-mapping>

我正在尝试构建一个 Log Searcher 函数,如下所示:

    /// <summary>
    /// Searches the logs for matching records
    /// </summary>
    /// <param name="fromUTC">Start point timestamp of the search</param>
    /// <param name="toUTC">End point timestamp of the search</param>
    /// <param name="ofSeverity">Severity level of the log entry</param>
    /// <param name="orHigher">Retrieve more severe log entries as well that match</param>
    /// <param name="sourceStartsWith">The source field starts with these characters</param>
    /// <param name="usernameStartsWith">The username field starts with these characters</param>
    /// <param name="maxRecords">The maximum nuber of records to return</param>
    /// <returns>A list of Log objects with attached UserProfile objects</returns>
    public IEnumerable<Log> SearchLogs(
        DateTime fromUTC, 
        DateTime toUTC, 
        string ofSeverity, 
        bool orHigher, 
        string sourceStartsWith, 
        string usernameStartsWith, 
        int maxRecords)
    {
        var result = _Session
            (SOMETHING goes here)
    }

作为 NHibernate 的一名 n00b,这种多方面的查询让我摸不着头脑,我希望能得到一些帮助。如果您能说明我将如何修改其他类似查询的语句,我将不胜感激。

注意:如果设置了参数,则 Severity 将为IEnumerable<string>一个或多个值orHigher

感谢您的关注。

4

1 回答 1

0

这是我最终得到的结果,如果我做错了什么或效率低下,请随时告诉我:

    /// <summary>
    /// Searches the logs for matching records
    /// </summary>
    /// <param name="fromUTC">Start point timestamp of the search</param>
    /// <param name="toUTC">End point timestamp of the search</param>
    /// <param name="ofSeverity">Severity level of the log entry</param>
    /// <param name="orHigher">Retrieve more severe log entries as well that match</param>
    /// <param name="sourceStartsWith">The source field starts with these characters</param>
    /// <param name="usernameStartsWith">The username field starts with these characters</param>
    /// <param name="maxRecords">The maximum number of records to return</param>
    /// <returns>A list of Log objects with attached UserProfile objects</returns>
    public IEnumerable<Log> SearchLogs(
        DateTime fromUTC,
        DateTime toUTC,
        string ofSeverity,
        bool orHigher,
        string sourceStartsWith,
        string usernameStartsWith,
        int maxRecords)
    {
        ofSeverity = ofSeverity ?? "INFO";

        var query = DetachedCriteria.For<Log>()
            .SetFetchMode("UserProfile", NHibernate.FetchMode.Eager)
            .Add(Restrictions.In("Severity", (orHigher ?
                Translator.SeverityOrHigher(ofSeverity) : Translator.Severity(ofSeverity)).ToArray()))
            .Add(Restrictions.Between("TimeStamp", fromUTC, toUTC))
            .AddOrder(Order.Desc("TimeStamp"))
            .SetMaxResults(maxRecords);

        if (!string.IsNullOrEmpty(usernameStartsWith))
        {
            query.CreateCriteria("UserProfile")
                .Add(Restrictions.InsensitiveLike("UserName",
                usernameStartsWith, MatchMode.Start));
        }

        if (!string.IsNullOrEmpty(sourceStartsWith))
        {
            query
                .Add(Restrictions.InsensitiveLike("Source", sourceStartsWith, MatchMode.Start));
        }

        return query.GetExecutableCriteria(_Session).List<Log>();

    }
于 2012-04-22T14:18:27.377 回答