我需要能夠通過多個搜索字段搜索客戶帳戶。現在,我的存儲庫中有我的搜索邏輯。搜索邏輯包括一些過濾,感覺更像是它屬於域層,但這意味着使用IQueryable之類的東西,我也不確定我喜歡那種。哪個更好?在存儲庫或域級服務中有複雜的搜索邏輯(通過IQueryable或其他)?
例如,現在我有了所有字段搜索類利用它用戶可以搜索:
public class AccountSearch
{
public decimal Amount { get; set; }
public string CustomerId { get; set; }
public string Address { get; set; }
public string CustomerName { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string State { get; set; }
}
然後,我有一個簡單地將搜索類關閉的一個域級別的服務庫。我不喜歡它:
public class AccountsService : IAccountsService
{
private readonly IAccountRepository _accountRepository;
public AccountsService(IAccountRepository accountRepository)
{
_accountRepository = accountRepository;
}
public IEnumerable<Account> Search(AccountSearch accountSearch)
{
return _accountRepository.Search(accountSearch);
}
}
然後,我把所有的篩選邏輯在我的倉庫實現:
public class AccountRepository : IAccountRepository
{
private AccountDataContext _dataContext;
public AccountRepository(AccountDataContext entityFrameworkDataContext)
{
_dataContext = entityFrameworkDataContext;
}
public IEnumerable<Account> Search(AccountSearch accountSearch)
{
// My datacontext contains database entities, not domain entities.
// This method must query the data context, then map the database
// entities to domain entities.
return _dataContext.Accounts
.Where(TheyMeetSearchCriteria)
.Select(MappedAccounts);
}
// implement expressions here:
// 1. TheyMeetSearchCriteria filters the accounts by the given criteria
// 2. MappedAccounts maps from database to domain entities
}
不知道我是否應該感到好這個,或者我應該另找這樣實現搜索的方式。在這個情況下,你會怎麼做?
請問有什麼AccountsService`這裏的`點全部似乎要做的是包裝`IAccountRepository`看似毫無用處 – 2011-02-05 15:35:26
正確與往常一樣,爲了解決問題,我已經刪除了其他方法,而且你的問題和我的一樣,除了我寧願將搜索邏輯放在域名服務,我只是不知道如何最好地實現它。 – 2011-02-05 15:37:28