views:

216

answers:

1

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

+1  A: 

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.

svinto
I want to know how to add it to project. Do I create new folder "Helpers", then click on it and add new class and then just copy pasty my class there...? Will it be available or there's something else to do?
ile
"And it works if you have a Using to the namespace where the helper method lives" I see, this is crucial
ile
Yeah, crucial is the word. (Note: In ASP.NET you can add default namespaces in web.config, and that way you don't have to include them in your views, yay for less code.)
svinto