流利的Nhibernate:如何以最佳方式制作Search Class Generic

时间:2015-09-13 22:15:37

标签: c# fluent-nhibernate

我有一个班级说'AllInvoices',其结构如下:

public class ActiveInvoices
{
    public string InvoiceId { get; set; }
    public string InvoiceIssueDate { get; set; }
    public string InvoiceTransactionDate { get; set; }
    public string InvoiceExpiryDate { get; set; }
}

Entity ActiveInvoices的映射类是

public class ActiveInvoicesMap : ClassMap<ActiveInvoices>
{
    public ActiveInvoicesMap()
    {
        Id(x => x.InvoiceId);
        Map(x => x.InvoiceIssueDate);
        Map(x => x.InvoiceTransactionDate);
        Map(x => x.InvoiceExpiryDate);
    }
}

现在使用此实体,我使用以下类

搜索数据库中的活动发票
public class SearchInvoices
{
    public readonly IRepository<ActiveInvoices> latestActiveInvoicesRepository;

    public SearchInvoices(IRepository<ActiveInvoices> activeInvoicesRepository)
    {
        latestActiveInvoicesRepository = activeInvoicesRepository;
    }

    public List<ActiveInvoices> GetActiveInvoices()
    {
        var listOfActiveInvoices = latestActiveInvoicesRepository.GetAll();
        return listOfActiveInvoices;
    }
}

搜索活动发票我从工作流程类调用搜索类方法'GetActiveInvoices()',如下所示:

public class CurrentWorkFlow
{
    public void GetActiveInvoices()
    {
        var invoiceSearch = new SearchInvoices(IRepository <ActiveInvoices> repository);
    }
}

现在的问题是,我需要使“SearchInvoices”类通用,以支持我将创建的所有其他可能类型,如“ExpiredInvoices”,“ArchivedInvoices”,“FutureInvoices”等,而不仅仅是'ActiveInvoices类型”。 这些新类型可能与“ActiveInvoices”具有相同或不同的结构。 我试图使用动态,但想在这里询问专家是否有更好的想法来实现所需的功能 以最优化的通用方式。

对于在询问时非常详细和冗长感到遗憾,但我想尽可能多地包含细节。希望你的人很顺利。

1 个答案:

答案 0 :(得分:2)

你不能制作这样的通用存储库吗? -

interface IDomain{
}

class ExpiredInvoices: IDomain{
}

class ActiveInvoices: IDomain{
}

interface IRepository{
}

class Repsoitory: IRepository  {
      public static IList<T> Get<T>() where T: IDomain //default one
      {
          using (ISession session = OpenEngineSession())
          {
              return session.Query<T>().ToList();
          }
      }
      public static IList<T> Get<T>(Expression<Func<T, bool>> expression) where T: IDomain // overloaded get with linq predicate
      {
          using (ISession session = OpenEngineSession())
          {
              return session.Query<T>().Where(expression).ToList();
          }
      }
}

然后像 -

一样使用它
var repo = // get  IRepository

var activeInvoices = repo.Get<ActiveInvoices>();
var expiredInvoices = repo.Get<ExpiredInvoices>();

编辑:由于OP

建议无法更改存储库

如果您无法更改存储库,那么我建议您使搜索服务interface依赖,而不是concrete类 -

interface IInvoice{
}

class ExpiredInvoices: IInvoice{
}

class ActiveInvoices: IInvoice{
}


public class SearchInvoices
{
    public readonly IRepository<IInvoice> latestActiveInvoicesRepository;

    public SearchInvoices(IRepository<IInvoice> activeInvoicesRepository)
    {
        latestInvoicesRepository = activeInvoicesRepository;
    }

    public List<T> GetActiveInvoices<T>() where T: IInvoice
    {
        var listOfActiveInvoices = latestActiveInvoicesRepository.GetAll();
        return listOfActiveInvoices;
    }
}

然后打电话给 -

var ss = new SearchService(IRepository <ActiveInvoices> repository);
var items = ss.GetActiveInvoices<ActiveInvoices>();

或者,

public class SearchInvoices<T> where T: IInvoice
{
    public readonly IRepository<T> latestActiveInvoicesRepository;

    public SearchInvoices(IRepository<T> activeInvoicesRepository)
    {
        latestInvoicesRepository = activeInvoicesRepository;
    }

    public List<T> GetActiveInvoices() 
    {
        var listOfActiveInvoices = latestActiveInvoicesRepository.GetAll();
        return listOfActiveInvoices;
    }
}
然后打电话给 -

var ss = new SearchService<ActiveInvoices>(IRepository <ActiveInvoices> repository);
var items = ss.GetActiveInvoices();

无论哪个适合你。