开发者

using a Custom Attribute in C# for decorating method result

  public IQueryable<T> All()
    {
        var session = _sessionFactory.GetCurrentSession();
        return FilterByClientId(from r in session.Query<T>() select r);
    }

    public IQueryable<T> FilterByClientId(IQueryable<T> queryable)
    {
        return queryable.Where(row => _clientIds.ClientIds.Contains<long>(row.ClientId) );
    }

Can I make use of Custom Attribute on the method to handle the decoration? the resulting code would look something like this. call to All method with the ClientFilter开发者_开发问答 would automatically decorate the result.

[ClientFilter]
    public IQueryable<T> All()
    {
        var session = _sessionFactory.GetCurrentSession();
        return from r in session.Query<T>() select r;
    }


You're looking for PostSharp, which allows you to modify method behavior using attributes.

However, it will add tremendous complexity and probably isn't worth it for something this simple.


If I understand what you are asking, then the answer is probably yes, but the complication of using attributes isn't worth it. Wouldn't it be simpler to make your second code sample simply be as follows?

// Edited to make more sense, but see below...
public IQueryable<T> FilterByClientId()
{
    return All().Where(row => _clientIds.ClientIds.Contains<long>(row.ClientId) );
}

EDIT: Based on your comment, try defining FilterByClientId as an extension method with a generic constraint:

public static IQueryable<T> FilterByClientId(this IQueryable<T> queryable) where T : IHasClientId
{
    return queryable.Where(row => _clientIds.ClientIds.Contains<long>(row.ClientId) );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜