About Projection Code

Topics: Writing modules
Jul 15, 2012 at 12:43 PM

Hi All,

I'm using the Projection module as a basis for writing a new module. It's doing similar things to what I want to do, but not quite the same.

Anyway, I'm interested to know how this works:

public ActionResult Index(AdminIndexOptions options, PagerParameters pagerParameters) {
            if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to list queries")))
                return new HttpUnauthorizedResult();

            var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);

            // default options
            if (options == null)
                options = new AdminIndexOptions();

            var queries = Services.ContentManager.Query("Query");

            switch (options.Filter) {
                case QueriesFilter.All:
                    throw new ArgumentOutOfRangeException();

            if (!String.IsNullOrWhiteSpace(options.Search)) {
                queries = queries.Join<TitlePartRecord>().Where(r => r.Title.Contains(options.Search));

            var pagerShape = Shape.Pager(pager).TotalItemCount(queries.Count());

            switch (options.Order) {
                case QueriesOrder.Name:
                    queries = queries.Join<TitlePartRecord>().OrderBy(u => u.Title);

            var results = queries
                .Slice(pager.GetStartIndex(), pager.PageSize)

            var model = new AdminIndexViewModel {
                Queries = results.Select(x => new QueryEntry {
                    Query = x.As<QueryPart>().Record, 
                    QueryId = x.Id,
                    Name = x.As<QueryPart>().Name
                Options = options,
                Pager = pagerShape

            // maintain previous route data when generating page links
            var routeData = new RouteData();
            routeData.Values.Add("Options.Filter", options.Filter);
            routeData.Values.Add("Options.Search", options.Search);
            routeData.Values.Add("Options.Order", options.Order);


            return View(model);

In this code, AdminIndexOptions class is passed in, and if null, a new one is created. But then the options values are used, assuming that the passed in class is not null. I say this because in AdminIndexOptions class, there is no default values set during instantiation, and I can see nowhere where any default values are set. If there are no values to reference in the switch statements that reference them, we're going to get exceptions.

I must say I'm not really that familiar with MVC in general yet, having hung onto WebForms for as long as I can. Maybe this issue is resolved with dependency injection, or some other means I'm not aware of. An explination of what's going on here would be greatly appreciated.

Cheers in advance.

Jul 15, 2012 at 4:24 PM

Nevermind, I get it now, the function takes from the view.. newbie mistake lol..