我正在为流利的nhibernate实现repository model。
我的项目具有以下结构
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.
}
}
}
虽然我也有类似Bonus1Repository的课程: BonusRepository,IRepository Bonus2Repository: BonusRepository,IRepository Bonus3Repository: BonusRepository,IRepository Bonus4Repository: BonusRepository,IRepository但作为所需的功能 在我想要在BonusRepositry中实现的所有entites中都是一样的 类iteslf。对此最好的方法是什么?谁能建议 对此的解决方案?
提前致谢。
答案 0 :(得分:0)
如果我正确理解您的问题,您可以为奖金实体创建一个指定公共成员的界面。 E.g。
public interface IBonus
{
string GetBonusValue();
DateTime DateUpdated{ get; set; }
}
然后使用IBonus
界面约束您的BonusRepository。例如。
public class BonusRepository<T> : IRepository<T> where T : class, IBonus
这样,您就可以访问IBonus
通用课程中的BonusRepository<T>
个成员。