小编Bri*_*ler的帖子

ASP.NET WebApi和部分响应

我有一个我正在研究的ASP.NET WebApi项目.老板希望返回支持"部分响应",这意味着尽管数据模型可能包含50个字段,但客户端应该能够为响应请求特定字段.原因是如果他们实现例如列表他们根本不需要所有50个字段的开销,他们可能只想要名字,姓氏和Id来生成列表.到目前为止,我已经使用自定义合约解析器(DynamicContractResolver)实现了一个解决方案,这样当请求进入时,我通过OnActionExecuting方法中的过滤器(FieldListFilter)窥视它,并确定是否有一个名为"FieldList"的字段

一些示例代码

DynamicContractResolver.cs

protected override IList<JsonProperty> CreateProperties(Type type, Newtonsoft.Json.MemberSerialization memberSerialization)
    {
        List<String> fieldList = ConvertFieldStringToList();

        IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
        if (fieldList.Count == 0)
        {
            return properties;
        }
        // If we have fields, check that FieldList is one of them.
        if (!fieldList.Contains("FieldList"))
            // If not then add it, FieldList must ALWAYS be a part of any non null field list.
            fieldList.Add("FieldList");
        if (!fieldList.Contains("Data"))
            fieldList.Add("Data");
        if (!fieldList.Contains("FilterText"))
            fieldList.Add("FilterText");
        if (!fieldList.Contains("PageNumber"))
            fieldList.Add("PageNumber");
        if (!fieldList.Contains("RecordsReturned"))
            fieldList.Add("RecordsReturned");
        if (!fieldList.Contains("RecordsFound"))
            fieldList.Add("RecordsFound");
        for (int ctr …
Run Code Online (Sandbox Code Playgroud)

model-view-controller json asp.net-web-api

5
推荐指数
1
解决办法
3239
查看次数

在c#中返回给定接口的泛型类型

假设我有以下设置.

Interface IDALModel {}
Run Code Online (Sandbox Code Playgroud)

各种类实现此接口,因此......

class AddressModel : IDALModel 
{
    public int AddressId {get;set;}
    public string Address {get;set;}
}

class AccountModel : IDALModel
{
    public int AccountId {get;set;}
    public string Name {get;set;}
}

class TenantModel : IDALModel
{
    public int TenantId {get;set;}
    public KeyedCollection<int, AddressModel> Addresses {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

在另一个类中,我有一个方法应该以通用方式返回类的实例.

class SomeClass
{
    internal DataFieldBuilder<IDALModel> Embed<T>(Expression<Func<T, object>> embed, string alias = "")
    {
        PropertyInfo propertyInfo = GetPropertyInfo(embed);
        Type type = propertyInfo.PropertyType.GenericTypeArguments[1];
        DataFieldBuilder<IDALModel> returnObject = new DataFieldBuilder<type>();
    }

    private PropertyInfo GetPropertyInfo<T>(Expression<Func<T, object>> …
Run Code Online (Sandbox Code Playgroud)

c# generics interface

3
推荐指数
1
解决办法
81
查看次数