これは同じ質問(AutoMapperを使用)に対応するものです。私はValueInjecterを使用しており、解決策があるかどうかに興味があります。
簡略化されたコード例:
// get a list of viewModels for the grid.
// NOTE: sort parameter is flattened property of the model (e.g. "CustomerInvoiceAmount" -> "Customer.Invoice.Amount"
// QUESTION: how do I get original property path so I can pass it to my repository for use with Dynamic LINQ?
public HttpResponseMessage Get(string sort)
{
var models = _repo.GetAll(sort) // get a list of domain models
var dto = _mapper.MapToDto(models); // get a list of view models that are flattened via dto.InjectFrom<FlatLoopValueInjection>(models);
var response = new HttpResponseMessage();
response.CreateContent(vehicles);
return response;
}
まあ、私は何かを考え出すことができましたが、コミュニティから(そしておそらくValueInjecterを書いた@Chuck Norrisからの)いくつかの入力を本当に望んでいます...
だから問題を言い直して...
その場合はCompany
呼ばれる性質があるPresident
タイプのEmployee
、そしてEmployee
呼ばれる性質があるHomeAddress
タイプのAddress
、そしてAddress
呼ばれる文字列プロパティがあるLine1
次のようにします。
「 President.HomeAddress.Line1
」は、ビューモデルの「 PresidentHomeAddressLine1
」と呼ばれるプロパティにフラット化されます。
ユーザー側のサーバー側の並べ替えには、Dynamic Linqがフラット化されたパスではなく、点線のプロパティパスを必要とします。 ValueInjecterを使用して、フラットプロパティのみを展開解除する必要があります。クラス全体ではありません。
これが私が思いついたものです(他のことをするメソッドから抽出された関連ロジック:
// type is type of your unflattened domain model.
// flatProperty is property name in your flattened view model
public string GetSortExpressionFor(Type type, string flatPropertyPath)
{
if (type == null || String.IsNullOrWhiteSpace(flatPropertyPath))
{
return String.Empty;
}
// use ValueInjecter to find unflattened property
var trails = TrailFinder.GetTrails(flatPropertyPath, type.GetInfos(), typesMatch => true).FirstOrDefault();
var unflatPropertyPath = (trails != null && trails.Any()) ? String.Join(".", trails) : String.Empty;
return unflatPropertyPath;
}
// sample usage
var unflatPropertyPath = GetSortExpressionFor(typeof(Company), "PresidentHomeAddressLine1");
// unflatPropertyPath == "President.HomeAddress.Line1"
これを行う別の方法は、 TrailFinder
を使用することTrailFinder
IEnumerable<IList<string>> GetTrails(string upn, IEnumerable<PropertyInfo> all, Func<Type, bool> f)
UberFlatter
はUberFlatter
ように使用します。
var trails = TrailFinder.GetTrails(flatPropertyName, target.GetType().GetInfos(), f);
f endプロパティのタイプが特定の条件に一致するかどうかをチェックします(おそらく、あなたの場合はtrueを返すことができます)