2014-09-24

More MVC and JSON.Net - Exceptions

In my last episode with MVC, I was trying to reconcile MVC 5.0 with WebAPI 5.1 and the fact that the former used a broken JSON library, while the latter used the better Newtonsoft JSON library. I implemented a model binder and a value provider factory to get the two projects in sync, and all was right with the world.

As the project has gone on, we've come across instances where we've needed to call MVC controllers and get JSON responses back. We have created WebAPI controllers in some cases, but the way the project is structured, we've needed to post MVC models as JSON via AJAX back to MVC methods where we cannot reference them in the WebAPI project (otherwise we'd have a circular reference).

There are probably a few ways to restructure this to work. Ripping the view models out to a library that can be referenced by both MVC and WebAPI was one idea, for instance. But that would've been a lot of work and caused bigger deployment headaches that I don't want to get into here. (A smaller project might've gotten away with it, though.)

Returning JSON to the client is almost as easy as having your MVC controller return a JsonResult — the MVC Controller class has its own built-in Json() method to do just that. Except, of course, it uses the JSON serializer we never want to see again. In this case, I created my own JsonNetResult class (based on a StackOverflow question, naturally), and my controller just calls this.JsonNetResult(resultObj); to do the magic.

The remaining issue, though, is when it came to exceptions. While WebAPI, on a JSON request, returns an error response formatted in JSON (that jQuery's ajax method parses easily), MVC gives you that nice, big, friendly "yellow screen of death" with the error formatted in HTML (or a generic message when you get to production). Not too useful when you want your JavaScript to report back some detail about what went wrong.

The solution, here, was to use an exception filter. The JsonNetExceptionFilter class checks to see if the incoming request was specified as JSON (using very similar code to the model binder), and if so, it handles the error on its own. We were throwing HTTP error messages using two different types of objects, depending on whether the code was copied from WebAPI or MVC (since they each have their own namespaces for this kind of thing). I check for these types so that I can set the response code to something besides the default 500 (400 Bad Request is used quite a bit), and I set the content to a JsonNetResult object with the data being a message formatted in something similar to the WebAPI format (and thus parsed by the same JavaScript code).

I considered leaving it here, so that we would have to decorate every JSON method with this handler as well as specifying it returned a JsonNetResult (unless there's a way to find out programmatically in the filter whether the current controller method returns a JsonNetResult, but I failed to find that); but ultimately, I decided to just override the default error handler site-wide (since it falls back to the base class for non-JSON requests, it shouldn't be an issue). That was done by editing the MVC app's RegisterGlobalFilters method to read:

filters.add(new JsonNetExceptionFilter());

Now, whenever we want to return an exception back to a JSON request, all we have to do is throw an exception:

throw new System.Web.HttpException((int)System.Net. HttpStatusCode.BadRequest, "Bad request data.");

Processing the return message in jQuery is left as an exercise to the reader (though if you use WebAPI, you probably already know). ;)

2014-08-11

How is escrow not a scam?

The concept of "escrow" already annoys me. As a requirement for my mortgage, I have to put aside money to pay for my homeowners insurance and property taxes, into a special account managed by the mortgage company, plus some extra depending on the mortgage company's demands. I can understand why they have a vested interest in making sure these things get paid, since technically they do own the house; but it seems it operates under the assumption that I would not take responsibility for these payments, and it gives me no opportunity to prove otherwise. Instead, the mortgage company gets to take the money and earn the interest, or dividends on the investments, and I have to just trust that they will make the tax and insurance payments (that I am responsible for) on time.

(To their credit, I have not yet had a mortgage company fail to pay either of these things on time.)

But recent events make me even more ticked off at the whole racket. I have a homeowners insurance claim in progress for storm damage (nothing too serious, mostly hail damage on the roof; we had the same thing five years ago). If the check is over a certain amount, the insurance company makes the check out to us and the mortgage company, requiring endorsement from all parties before the money can be used. I don't fully understand this. The insurance policy is a contract I have entered into with the insurance company. The mortgage company did not co-sign or endorse this contract, outside of requiring me to have a contract that meets whatever minimum requirements they had to allow me to get the mortgage. The mortgage company does not put any money into this contract — while they do make the payment of funds every year, they do so out of money they took from me. So why should they have a claim to the money on the insurance policy I paid for?

Still, before now, it was just a hurdle I had to jump before I could start repairing my home.

This time around, it's worse. Because the insurance payment was over a specific amount, my mortgage company demands I endorse the check and send it to them, and they will deposit it in a separate escrow account, and only give me money from it when I turn in receipts for repairs.

This is ridiculous. For one thing, it means I will be paying money out of pocket first. I don't have thousands of dollars sitting around that I can "float", paying for the repairs and waiting for the mortgage company to validate the receipts and give me my money back. That's the reason I have an insurance policy, so I don't have to pay out of pocket for catastrophes. For another, if my insurance company overestimated the damage amount, or if I am able to find a contractor willing to make the repairs for much less than the amount the insurance company gave me, how do I get that money when I don't have a big receipt to cover it? (The fact that the insurance company may have overpaid me is a discussion between me and them alone, since we are the ones that hold that contract.)

And this is just on top of my initial annoyance with escrow, that I'm not able to so much as put that money in an interest-bearing account to make a lousy 1‰ until I get the repairs done.

Of course, the answer is going to be so that they know that damage to their property is repaired, because otherwise the value of their property will be lessened by my negligence, while I could pocket the money and run. Still, this should be something I should prove first, let them sue me for negligence later. I get weary fast of having to pay for someone else's mistakes, and it seems like that's what is happing now: because irresponsible people exist, I cannot be trusted to do the right thing.

2014-07-27

Attraction doesn't always mean action

So here's a really interesting story. It's a hard one to read or listen to, because it involves something that, I'd wager, most of us would rather not think about. It's about a young man who discovered he was sexually attracted to children, and how he's been struggling with this attraction all his life. A big part of his struggle is that pedophilia is something for which it is very difficult to get help. As the article states: "…they have no way to talk about these urges or how to prevent them from taking control of their lives without being considered a threat. Talk to a shrink? You risk being reported to the authorities. The scientific community is so afraid of the stigma attached to even researching pedophilia that it's barely been studied it at all." Which is why this young man has risked tarring and feathering to set up support groups, to help people who need help taking control of their lives instead of letting their urges define and control them. It's actually a pretty inspirational story about taking control and standing up for what's right, even when the wrongness is coming from your own self.

Now, here's where I'm about to get very unpopular.

Take that same story, but replace all references to sexual attraction to children, with sexual attraction to members of the same sex.

Suddenly, it goes from a story of inspiration and self-control to one worthy of derision because of its support of self-denial and giving in to homophobia.

I've seen many arguments that saying "anti-gay" is wrong because people can't change who they are, and they should be free to act on their feelings. It's the whole crux of the argument for allowing same-sex marriage: preventing discrimination based on what a person is. Yet if we were to apply the same logic to other sexual attractions that are (still) considered bad — pedophilia, bestiality, necrophilia, take your pick — we would be immediately shot down for trying to support the "sick" or "depraved" (if we weren't immediately shot for merely insinuating any kind of relationship between homosexuality and pedophilia).

So what's the difference? Attraction to children is something that, as a society, we all seem to agree is wrong — so it's ok to have laws against it and to not even think about granting legal protections and rights to those forms of relationships. And yet, it wasn't until just the past few years that the same could be said for same-sex attraction. Debate or blame whomever you wish for the existence of California's famous Proposition 8, but the fact that it passed means there was a significant number of people who, less than a decade ago, supported the idea that condoning same-sex attraction was wrong.

So, why? Why is this form of sexual attraction ok, and others aren't? What makes same-sex attraction part of a person's being, but underage attraction something that a person can and should suppress and live their lives in denial of? And what form of sexual attraction that we think is wrong today, will we be forced to bake cakes in celebration of ten years from now?

2014-07-17

MVC, JSON, and DateTime (oh my)

It's been a while since I've had a programming issue that warrants a blog post, but here's an interesting one.
First, let me set up the situation. I have an MVC project (MVC version 5.0) that communicates to RESTful services using WebAPI (version 5.1). In one particular view, I write out an object in JSON with the intent that the client can make changes to it and POST it back to an MVC action to get an HTML table back. Here's the model:

public class PaymentScheduleRequestModel {
[JsonProperty(PropertyName = "effDt", NullValueHandling = NullValueHandling.Ignore)]
public DateTime EffectiveDate { get; set; }

[JsonProperty(PropertyName = "termId", NullValueHandling = NullValueHandling.Ignore)]
public Nullable<int> ContractTermId { get; set; }

[JsonProperty(PropertyName = "billDay", NullValueHandling = NullValueHandling.Ignore)]
public int BillingDayOfMonth { get; set; }

[JsonProperty(PropertyName = "freq", NullValueHandling = NullValueHandling.Ignore)]
public PaymentScheduleFrequencyId PaymentScheduleFrequencyId { get; set; }

[JsonProperty(PropertyName = "prem", NullValueHandling = NullValueHandling.Ignore)]
public decimal TotalPremium { get; set; }
}

(Note that the JsonProperty decorators exist because the same model is used to communicate to the WebAPI service -- we are using shorter property names to lighten the payload.)

And the MVC action that is set up to process it looks like this:

[HttpPost] public async Task<ActionResult> NewPaymentSchedule(PaymentScheduleRequestModel request) { … }

To put it in JavaScript, the view has this block of code:

window.ScriptModel = @Html.Raw(Json.Encode(new {
PaymentScheduleUrl = Url.Action("NewPaymentSchedule", "Contract"),
PaymentScheduleRequestModel = new PaymentScheduleRequestModel {
ContractTermId = Model.ContractTermId,
EffectiveDate = Model.StartDate ?? DateTime.Now,
TotalPremium = 200m
}
}));

Unfortunately, Json.Encode doesn't work well with dates. This is the output that the browser sees (with line breaks added for legibility):

window.ScriptModel={
"PaymentScheduleUrl":"/Contract/NewPaymentSchedule",
"PaymentScheduleRequestModel":{
"EffectiveDate":"\/Date(1078729200000)\/",
"ContractTermId":null,
"BillingDayOfMonth":0,
"PaymentScheduleFrequencyId":0,
"TotalPremium":200
}
};

Notice two things about System.Web.Helpers.Json.Encode's output:

  1. The property names are the .Net property names. The same code could not be used to post what should be the same model to MVC or to WebAPI.
  2. The EffectiveDate field has been converted to a Date function, enclosed in a string. Even if the Date function would reveal the correct value, the fact that it's in a string means JavaScript will not see it as a Date.

If I don't do anything with this object and just post it back to the MVC app, the resulting object does not have a valid date — all other values carry over, but the EffectiveDate property is 01/01/0001. So not only does it look odd, not only is it inconvenient in that JavaScript can't use it as-is (without picking apart the string), but it doesn't even work for round-tripping data to and from the client.

In doing some research on this topic, I came across Scott Hanselman's blog post describing the problem, and stating that the release of WebAPI won't have the issue since it will use JSON.Net (a.k.a. Newtonsoft.Json). Since I'm using the same model in an WebAPI call further downstream, I can verify that it does work as intended. It uses the JsonProperty decorators to rename the properties, and it serializes and deserializes like magic.

To solve this problem in MVC, you have to alter how it deals with JSON on the way out and on the way in.

On the way out is easy in my case, since I am manually spitting out JSON into the HTML. I just exchanged Json.Encode with Newtonsoft's serializer:

window.ScriptModel = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(new {
PaymentScheduleUrl = Url.Action("NewPaymentSchedule", "Contract"),
PaymentScheduleRequestModel = new PaymentScheduleRequestModel {
ContractTermId = Model.ContractTermId,
EffectiveDate = Model.StartDate ?? DateTime.Now,
TotalPremium = 200m
}
}));

And the browser sees:

window.ScriptModel={
"PaymentScheduleUrl":"/Contract/NewPaymentSchedule",
"PaymentScheduleRequestModel":{
"effDt":"2004-03-08T00:00:00",
"billDay":0,
"freq":0,
"prem":200
}
};

This is better. Of course, the controller action doesn't understand this. It's still looking for the .Net property names, and, since they don't exist on the incoming object, all values come back empty. (In this simple example, not only is EffectiveDate 01/01/0001, but TotalPremium is 0.0.)

The trick here is to override MVC's default model binder, so that it, too, uses the Newtonsoft.Json library. It is also consistent to have MVC use a value provider factory that also uses JSON.Net.

Fortunately, people smarter than I figured out these two steps. I found a value provider factory on this blog: http://www.dalsoft.co.uk/blog/index.php/2012/01/10/asp-net-mvc-3-improved-jsonvalueproviderfactory-using-json-net/
and the important piece, the model binder, that will translate the JSON property names to their real .Net names, is detailed here: http://stackoverflow.com/questions/4164114/posting-json-data-to-asp-net-mvc

My implementation looks like this:

And this code gets called from Application_Start (I actually added it to my WebApiConfig class (which, for some reason, exists in my MVC app even though it's obviously not the same as WebAPI), since other configuration-type things were being done here):

ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());

ModelBinders.Binders.DefaultBinder = new JsonDotNetDefaultModelBinder();

All this because, although Microsoft updated one of their web interfaces (WebAPI) to use the JSON.Net library that works, another one of their interfaces (MVC) uses their own, broken, JSON serialization library.

2014-04-30

Consequences of free speech

A lot has been made recently of Los Angeles Clippers' owner Donald Sterling, being caught on tape saying some pretty racist things, bad enough for the NBA to ban him for life, fine him $2½ million, and force him to sell his team. From what I've heard, this result seems like a net positive for society as a whole, but how this whole thing has come about makes me very worried for the direction this country is headed.

According to the reports I've been reading, Sterling is not a very nice guy. Some say he has a long history of being racist. By the sound of things, the NBA will be better off without him. And yet, the NAACP gave him an award back in 2009 because (at least in part) he gave away a lot of tickets. The more cynical reports suggest he was given this award, with his racism overlooked, because he gave money to the right people. He was, in fact, due to receive another award this year from the NAACP, before recent events made the organization reconsider.

But what were the "recent events"? Did he use his power as an owner in real estate to deny housing to blacks or hispanics? Did he make an employment decision based on race? No, that was years ago (before, during, and after the NAACP was giving him his first lifetime achievement award). It happened because his girlfriend recorded a private conversation, coaxed him to say what he did, and sold the tape to the online tabloid TMZ.

I've read conflicting comments as to whether or not Sterling knew he was being recorded, but those that care to mention the girlfriend seem to agree that she really worked on Sterling to get him to make his damaging statements, leading him to say what he did and really dragging it out of him. Of course that brings a lot of rumor and speculation about how much, and from whom, she was getting paid to do this, and whether or not it was illegal (most likely if Sterling didn't know the tape was rolling).

Kareem Abdul-Jabbar wrote an excellent opinion of the issue for Time, which echoes a lot of the comments I've heard that led me to my conclusion above. It also comes to pretty much the same end — Sterling's eviction from the NBA is a good thing and long overdue, but how it happened is very, very wrong.

The big question, of course, is, did Sterling deserve his NBA exile, fine, and loss of ownership of his team for what he said? Maybe his speech just served to bring about awareness of what he was actually doing, but because he only got punished when he expressed his views in the privacy of his own home, it certainly looks like that's his sin. He said something that was unpopular, and now he must pay.

It is true that "freedom of speech" does not make you free of consequences. If you say something that someone doesn't like, that person is just as entitled to use their own freedom of speech to speak out against you, exercise their freedom of association to refuse to do business with you, and even freely encourage others to avoid you as well. But why does it seem like it's only the speech — or even the perceived interpretation of speech — that deserves to be punished, when actions can go unnoticed or be forgiven?

We've been sliding down this slope for a while. Paula Deen was condemned for using a racial slur in the past (including in a description of someone who had a gun to her head), despite having apparently changed her ways since. Phil Robertson lost his place on the show Duck Dynasty for daring to honestly answer an interview question asking what he considered a sin (only to be reinstated after enough public support). Brendan Eich found he was unable to do his new job as Netscape CEO due to all the protests that, six years prior, he gave a paltry (for him) sum for California Proposition 8 — a view that, even though is becoming less so now, was the popular view at the time (Prop 8 passed) and, at the time, was even supported by presidential candidate Barack Obama. Cliven Bundy was supported by many in the (mostly right-wing) media, before he expressed his political opinion that came off as racist (though that might be less actual racism and more poor word choice and selective editing by the media, depending on your point of view).

So now, it's not just what you say in public. It's not even what you said in public in the past, whether or not you've recanted since. But what you say in private, behind closed doors, can and will be used against you in the court of public opinion; and if your opinion goes against the prevailing wisdom, whichever way it happens to be blowing at the time, then God help you.

2014-03-13

My New Year's Resolutions

I try to avoid making New Year's Resolutions. They tend to be some big life-changing or life-enhancing promises that start with really good intentions, and then fall flat within a month or two. Personally, I just can't see myself deciding to completely change my habits or lifestyle overnight. But this year, I decided there were a couple things I could do better. They aren't major issues, but at least it's something I can do and accomplish and feel like I've done something to improve.

The first is a bit of housekeeping. Our HVAC system has stainless steel air filters (rather than cheap cardboard-framed filters), and they are supposed to be cleaned out periodically. Historically, I've been very bad at cleaning those filters, going way back to when we first had the system installed. I remember cleaning them one day and then being very surprised that I actually saw curtains moving in the breeze from the air vents. Our house also tends to be very dusty, which I don't doubt is at least partially the fault of the HVAC's inability to move air through the completely clogged filters. So, I resolved this year to incorporate into my Sunday routine, cleaning out those air filters.

For the month of January, I did manage to clean the filters out every week. At least in those first couple weeks, it really needed it, as the filters were pretty well coated when I pulled them out. But by the time I got to February, the filters were still pretty clear when I went to clean them. So, I've backed off a bit on my initial resolution, instead cleaning the filters out every other week. Even then, they're not catching nearly as much dust as they were, so I feel that I'm reaching my ultimate goal of keeping them clear so the heater & A/C will work efficiently. I'm not certain it's cut down on the dust that gathers in the rest of the house, as that's pretty hard to quantify (especially when we just simply don't dust around here), but I like to think so. I was hoping to see my energy bill go down a bit as a result of the better air flow, but that, too, failed to come to pass. Still, I can at least pretend that I'm doing the right thing.

The second has to do with my church. I believe in the Law of Tithing, where I give 10% of everything I earn back to the Lord. The way I've kept track of what I should pay, on every increase, I enter a transaction in my financial software with a future date with the 10% amount. It's always there at the bottom of my register, so I always know how much I owe; and I can always make sure I don't spend too much such that there isn't enough for the tithing amount. If I forget to take a check to church one week, I just keep adding to that transaction in my register, until I get around to writing the check and giving it a real date.

I thought it was a pretty good system, but a couple things bothered me. One, I noticed in months where money was tight, I would find myself "borrowing" from that total to pay the bills. Oh, I wouldn't change the amount, but I would ignore the fact that the tithing transaction was actually making my balance negative, because it would be fine when the next paycheck showed up. But I found that it seemed to happen more frequently and with greater amounts the longer I left that transaction in there unpaid (not to mention the tithing amount was growing over time as well).

The other thing occurred to me as we discussed tithing in church one Sunday. While it's relatively easy for us to pay tithing now by just writing a check, in the past it involved real items — for example, agricultural farmers would bring food from their harvest, and livestock farmers would bring animals. They always referred to it as bringing the "firstfruits" of the harvest or the flock, because it was a sign of devotion and respect to God that we give Him the best portion of what He gave us. When I applied this to my system of reserving money in my register, I realized that what I was doing was closer to making sure everything else was paid first, and then giving God what was left over. In practice, I had it backwards.

My resolution for tithing is to pay as soon as possible. That means, on Saturday night, I check to see if I need to pay anything for tithing (since I don't really think about which weeks are the pay weeks and which aren't), and write out the check, so it's ready to turn in the very next day. It just feels like the right thing to do, and I feel better about doing it this way.

So yeah, that's it. Those are my New Year's resolutions. Nothing earth-shattering or anything, but I feel like they are realistic, attainable goals that I can actually keep. And now, here they are in writing, so I can hold myself accountable.