Joseph Bulger IV

Author Archive

Using Contract By Design over the wire and a possible solution

by Joseph on Apr.29, 2010, under Programming

I’ve been plagued with an issue that I have yet to find a solution for.

This is really more for my benefit so I can research this later, but basically when I create classes I tend to decouple them by using interfaces, especially between layers.

The problem is when you use a service layer, such as WCF, that the class that is generated on the client side doesn’t carry the implementations that the server side class has. 

Then I found this article which might be my saving grace.  It lefts you actually reference the same class on the client side.  This might be solution I’m looking for.

Once I get a chance to test this out I’ll post the results with some code.

Leave a Comment : more...

Creating an PagedList<T> that uses AJAX

by Joseph on Apr.14, 2010, under Consulting, Programming, Technology

I’ve been using this PagedList functionality that i found from a blog article Rob Conery put up, and a control I found by Robert Muehsig which I’ve really enjoyed using so far.

One of the things that was missing from the functional set that I ended up needing was the ability to page the list, but through issuing AJAX requests instead of the typical post back.

So I went off and extended the existing model to support AJAX requests, and thought I would share it in case anyone else needed to do the same thing.

I guess the best place to start would be the use case.  So to start I created a control that encapsulates the Paging UI layout and calls I need.  The use of the original control looks like this:


Html.RenderPartial("AjaxPagination",
    new AjaxPaginationViewData
        {
            PageIndex = Model.PageIndex,
            Action = "CondoPage",
            Controller = "Home",
            AjaxOptions =
                new AjaxOptions { UpdateTargetId = "updatedContent" },
            TotalCount = Model.TotalCount,
            PageSize = Model.PageSize,
            NumberOfPagesToEachSide = 2
        }
);

The new AJAX functionality is called similarly:


<% using (Ajax.BeginForm("SomePage",
        "SomeController",
        new AjaxOptions { UpdateTargetId = "updatedContent" })) { %>

        <% Html.RenderPartial("AjaxPagination",
                new AjaxPaginationViewData {
                        PageIndex = Model.PageIndex,
                        Action = "SomeAction",
                        Controller = "SomeController",
                        AjaxOptions = new AjaxOptions
                                { UpdateTargetId = "updatedContent" },
                        TotalCount = Model.TotalCount,
                        PageSize = Model.PageSize,
                        NumberOfPagesToEachSide = 2
                });%>

<% } %>

A couple things to note. You’ll notice that the AJAX control is rendered inside a Ajax.BeginForm. This is because I’m using the Microsoft.Ajax way of making AJAX calls.  This could also be done using jQuery or something else that can process AJAX calls. I just went this way because the scripts are already included in asp.net mvc app when you first create the project.  The result of the AJAX call will be a partial view, and we’ll need to put that somewhere.  That’s where the UpdatedTargetId comes into play. Other things we include in the AJAX control that are not in the original are the Action and the Controller, and some AjaxOptions. PageActionLink doesn’t work with the AJAX control, because we’ll be using Ajax.ActionLink to build the link, which is why I broke it up into Action, and Controller. For the AjaxOptions, we need those to specify the target of the call.

So now that’s been explained, let’s look at the controls themselves.  Here’s a comparison of the original control and the ajax control.

The original is one this way:


<% if (Model.HasPreviousPage) { %>
    <a href="<%=Model.PageActionLink.Replace("%7Bpage%7D", (Model.PageIndex - 1).ToString())%>">Previous</a>
<% } %>

<% if (Model.GetFirstPageToLink() != 1) { %>...<% } %>

<%for (var page = Model.GetFirstPageToLink(); page <= Model.GetLastPageToLink(); page++) {
    if (page == Model.PageIndex) { %>
        <%=page.ToString()%>
<% } else { %>
    <a href="<%=Model.PageActionLink.Replace("%7Bpage%7D", page.ToString())%>"><%=page.ToString()%></a>
<% } 

    if (page != Model.GetLastPageToLink()) { %>|<% } } %>

<% if (Model.GetLastPageToLink() != Model.PageCount) { %>...<% } %>

<% if (Model.HasNextPage) { %>
    <a href="<%=Model.PageActionLink.Replace("%7Bpage%7D", (Model.PageIndex + 1).ToString())%>">Next</a>
<% } %>

And the AJAX control is done this way:


<% if (Model.HasPreviousPage) { %>

<%= Ajax.ActionLink("Previous", Model.Action, Model.Controller, new { page = (Model.PageIndex - 1).ToString() }, Model.AjaxOptions)%>

<% } %>

<% if (Model.GetFirstPageToLink() != 1) { %>...<% } %>

<%for (var page = Model.GetFirstPageToLink(); page <= Model.GetLastPageToLink(); page++) {
    if (page == Model.PageIndex) { %>
        <%=page.ToString()%>
    <% } else { %>

<%= Ajax.ActionLink(page.ToString(), Model.Action, Model.Controller, new { page = page.ToString() }, Model.AjaxOptions)%>

<% } if (page != Model.GetLastPageToLink()) { %> | <% } } %>

<% if (Model.GetLastPageToLink() != Model.PageCount) { %>...<% } %>

<% if (Model.HasNextPage) { %>

<%= Ajax.ActionLink("Next", Model.Action, Model.Controller, new { page = (Model.PageIndex + 1).ToString() }, Model.AjaxOptions)%>

<% } %>

The big difference here is the way that the links are generated. The original control simply creates an anchor tag and passes in the url generated by the Model. The AJAX control uses AJAX.ActionLink() instead, so we can have the link support AJAX.

So knowing how the control looks, this is the Model for the AJAX control itself:


public class AjaxPaginationViewData
{
    public int NumberOfPagesToEachSide { get; set; }
    public int PageIndex { get; set; }
    public int PageSize { get; set; }
    public int TotalCount { get; set; }

    public string Action { get; set; }
    public string Controller { get; set; }

    public AjaxOptions AjaxOptions { get; set; }

    public int PageCount
    {
        get
        {
            return (int)Math.Ceiling((double)TotalCount / PageSize);
        }
    }
    public bool HasPreviousPage
    {
        get
        {
            return (PageIndex > 1);
        }
    }

    public bool HasNextPage
    {
        get
        {
            return (PageIndex * PageSize) <= TotalCount;
        }
    }

    public int GetFirstPageToLink()
    {
        return (PageIndex - NumberOfPagesToEachSide > 1 ? PageIndex - NumberOfPagesToEachSide : 1);
    }

    public int GetLastPageToLink()
    {
        return (PageIndex + NumberOfPagesToEachSide < PageCount ? PageIndex + NumberOfPagesToEachSide : PageCount);
    }
}

That pretty much explains how the control is built.

The only thing left is how the interaction with PagedList happens.  For that we look at the action that the control calls.  In this example, we’re calling SomeAction in SomeController, and it would look something like this:


public ActionResult SomeAction(int page)
{
    CachedPage = page;
    var query = GetSearchQuery(CachedSearchParameters);
    var model = query.ToPagedList(page, DefaultPageSize);
    return PartialView("AjaxResults", model);
}

The ToPagedList performs the functionality that is included with the PagedList classes which you can find here.

Let me know what you think, and if you’d like some demo source to see this in action I can happily provide, just let me know.

Leave a Comment :, , , more...

Are you a Nerd, Geek, or Dork?

by Joseph on Mar.30, 2010, under General

I just got an email from a friend of mine with this and I couldn’t pass up throwing this on my blog.

Nerd_Dork_Geek_Venn_Diagram

Here’s the original article that this came from.

Enjoy!

1 Comment : more...

First Impressions of Droid Eris

by Joseph on Feb.22, 2010, under Technology

I just recently got a new phone, a Droid Eris. So far I’m liking it a lot. The keyboard is surprisingly easy to use. I haven’t gotten around to tethereing the phone yet, but thats my next step. Then I’ll be able to use my computer from the road, something the iPhone didn’t offer me, which is why I went with an Android phone. So here’s hoping!

Leave a Comment : more...

An example of why Health Care in the US has gotten out of control

by Joseph on Feb.08, 2010, under General

I was just watching a television show that’s a drama about the medical field.  The particular episode I was watching perfectly illustrated the reason why Health Care in our country has completely gotten out of control.

The scenario: A patient has lost one of his digits (his thumb to be exact), and requested that the minimal amount of surgery be done on him as to avoid costs from his medical insurance.  His insurance only covers 60% of what they deem major surgery. He also indicated that if it was too much he wouldn’t sign the consent forms and would rather not get the surgery at all and just lose his thumb.

The doctor, in response to this patient, decides to get the patient’s consent for the least amount of surgery possible (basically to just attach the skin, but keep in mind this is fictitious), and after getting the consent and putting the patient under he proceeds to fully attach the mans thumb even after being explicitly told not to do so.  Now this isn’t what I’m saying the problem is, because I don’t really think a doctor would do this in real life.  The problem is what is to come, and shows the mentality that a lot of people with power seem to have.

The hospital ends up getting sued by both the patient and the insurance company, because both parties are unwilling to foot the bill (imagine that), and so the manager of the hospital goes to the doctor to confront him about the consent issue and the surgery.

Here’s the problem: The doctor, at some point, responds, “I wasn’t going to let the guy throw away his thumb just to save a few bucks.”

It may sound reasonable at the front of it, but consider something else.  The manager said the cost of the procedure was $80,000.  Taking into account for the fact that the patient would be responsible for a minimum of 40% of that amount, you’re talking $32,000.  That is not a few bucks.

Now the crux of this whole thing comes falling down.  Ultimately who’s responsibility/right is it to decide whether this procedure is done or not? It’s the patient’s. He decided he didn’t want it done; respect the decision. Our mentality about Health Care in this country needs to change from the mentality of this doctor. We need to give respect back to the patients. They have the right to choose.  They have the right to negotiate price. They have the right to refuse. They have the right for a second opinion. They have a right to their own opinion.

Leave a Comment :, more...

  • Link with me

    Joseph Bulger
  • Call me

  • What I'm Doing...

    Powered by Twitter Tools

  • Looking for something?

    Use the form below to search the site:

    Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

    Visit our friends!

    A few highly recommended friends...

    Archives

    All entries, chronologically...