-2

I am implementing repository model for fluent nhibernate.

My project has the following structure

    public interface IRepository<T>
    {

    #region Create Methods 1

        /// <summary>
        /// This method Submits the Bonus Offer to the database.
        /// </summary>
        /// <param name="Value">Type of Bonus Subject Eg. Bonus1, Bonus2, Bonus3, Bonus4 </param>
        void SubmitPost(T Value);

        #endregion

        #region Read Methods 4

        /// <summary>
        /// This method retrieves the Bonus offers not older than a month.
        /// </summary>
        /// <returns>Returns a List of Type T which is collection of particular type of Bonus Subject Eg. Bonus1, Bonus2, Bonus3, Bonus4</returns>
        IList<T> GetPostsNotOlderThan30Days();

        /// <summary>
        /// This method returns the most recent 10 posts.
        /// </summary>
        /// <returns>Returns a List of Type T which contains a collection of Bonus offers which are the top 10 recently posted ones.</returns>
        IList<T> GetRecent10Posts();

        /// <summary>
        /// This method returns a Bonus Subject given its matching Id Provided.
        /// </summary>
        /// <param name="Id">Id of the Bonus Subject of Type T. Eg. If of any of the Bonus1, Bonus2, Bonus3, Bonus4 Subjects.</param>
        /// <returns></returns>
        T GetPostById(object Id);

        /// <summary>
        /// This method returns all the Bonus offers of any Particular Bonus Subject posted by a particular user.
        /// </summary>
        /// <param name="UserId">Logged In UserId</param>
        /// <returns></returns>
        IList<T> GetUserPosts(object UserId);
        #endregion
    }
 and I have 4 Bonus Types
    namely Bonus1, Bonus2, Bonus3, Bonus4
    All these have some common properties like DateUpdated, DateExpires.

    And when I wanted to implement the class for IRepository which is BonusRepository
    public class BonusRepository<T>:IRepository<T> where T:class
        {
            protected Configuration config;
            protected ISessionFactory sessionFactory;

            public BonusRepository(string conStr)
            {
                config=Fluently.Configure()
                    .Database(
                        MsSqlConfiguration
                        .MsSql2008
                        .ConnectionString(conStr))
                        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<BonusRepository<T>>())
                    .BuildConfiguration();
                sessionFactory = config.BuildSessionFactory();
            }

            #region Create Methods 1

            /// <summary>
            /// This method Submits the Bonus Offer to the database.
            /// </summary>
            /// <param name="value">Type of Bonus Subject Eg. Bonus1, Bonus2, Bonus3, Bonus4</param>
            public void SubmitPost(T value)
            {
                using (var session = sessionFactory.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(value);
                    transaction.Commit();
                }
            }
            #endregion

            #region Read Methods 4

            /// <summary>
            /// This method retrieves the Bonus offers not older than a month.
            /// </summary>
            /// <returns>Returns a List of Type T which is collection of particular type of Bonus Subject Eg. Bonus1, Bonus2, Bonus3, Bonus4</returns>
            public IList<T> GetPostsNotOlderThan30Days()
            {
                using (var session = sessionFactory.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                  //Here I'm unable to get DateUpated, DateExpires
                  IList<T> returnVal=session.QueryOver<T>()
                       .Where(c=>c.

                }
            }
    }

while I'm also having classes like below Bonus1Repository: BonusRepository, IRepository Bonus2Repository: BonusRepository, IRepository Bonus3Repository: BonusRepository, IRepository Bonus4Repository: BonusRepository, IRepository but as the desired functionality is same across all the entites I wanted to implement in BonusRepositry class iteslf. What is the best approach for this ? Can anyone suggest the solution for this?

Thanks in advance.

4

1 回答 1

0

如果我正确理解您的问题,您可以为您的奖励实体创建一个界面,指定共同成员。例如

public interface IBonus
{
    string GetBonusValue();
    DateTime DateUpdated{ get; set; }
}

然后使用IBonus界面约束您的 BonusRepository。例如

public class BonusRepository<T> : IRepository<T> where T : class, IBonus

这将使您可以访问泛型类中的IBonus成员。BonusRepository<T>

于 2012-06-23T12:23:02.250 回答