开发者

NerdDinner tutorial - missing explanation for Helper methods?

I've started reading NerdDinner tutorial from scratch. While reading and coding application 开发者_高级运维I came to part about some Helper methods and there was one example of some class (AddRuleViolations) but there was no any explanation WHERE to add this class. So I skipped this one and continued with tutorial without using this class later in code.

Now, I am stuck at "Adding page navigation UI" section where this helper method is in use again. So, I downloaded their final code and I see that there is folder "Helpers" and these classes that I need to implement in my code. Thing is that I don't want to do copy/paste and I want to understand how to add this helper methods.

Specifically, I want to add this class as helper method:

    public class PaginatedList<T> : List<T>
    {
        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }
        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();

            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }
        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 0);
            }
        }
        public bool HasNextPage
        {
            get
            {
                return (PageIndex + 1 < TotalPages);
            }
        }
    }

Problem is that I don't have any experience with .net or C# and I'm not familiar with developing applications in VS. (I know only some basics of C#)

Thanks,

Ile


Helper methods is a .net way to monkey patch!

Say you have class Foo that someone somewhere wrote and you can't change it. Now you want to:

var foo = new Foo();
var something = foo.NotThere();

The NotThere function isn't in foo, what to do, what to do? Why not a helper method:

static class FooHelperThingy{
  static string NotThere(this Foo foo){
    return "Bar!!!";
  }
}

And voila you can call foo.NotThere(). (And it works if you have a Using pointing to the namespace where the helper method lives.)

The parameter marked this will be the object you're monkey patching.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜