Fluent Nhibernate:如何以最佳方式使搜索 Class 通用

Fluent Nhibernate : How to make Search Class Generic in best possible way

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

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

实体 ActiveInvoices 的映射 class 是

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

现在,我使用此实体在数据库中搜索活动发票,内容如下 class

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;
    }
}

为了搜索活动发票,我从如下所示的工作流 class 中调用搜索 Class 方法 'GetActiveInvoices()' :

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

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

很抱歉问得非常详细和冗长,但我想尽可能多地包含细节。希望大家一切顺利。

你不能做一个像这样的通用存储库吗? -

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 class -

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();

哪个适合你。