Friday, January 30, 2015

C# Reference Value

I often run into situations in C# where my variables don't behave as I expect them to.  For example, I pass an instance of a custom object into another method and the attributes of that instance are changed even though I didn't specify the ref keyword.  That's because C# has value and reference types and which one you get depends on what you're declaring.  This also affects where the memory is stored (the two concepts are tied together to a degree).

In short, when you're using an instance of a custom object, your resulting variable will be a reference type.  Every time.  I know this article is a bit outdated, but it explained it very clearly to me (for the first time) so I'm passing it along (and remember, this blog is all about keeping track of my own answers).

Tuesday, January 13, 2015

RDP With Multiple Monitors

I love working from home, but I hate that I lose my dual monitors when I have to remotely access my work computer.  Apparently, you can totally do that with Windows 7 and I just gave up after being unable to do it with Windows XP.  If you need to remotely access another computer and you want that RDP session to span multiple monitors (I only have two, so I'm not sure whether this works with more than two) you can launch the RDP application with the /span switch, like this: mstsc /span.

Thursday, January 8, 2015

Code Comments

I came across an old email from a coworker wherein he described his preference for commenting code.  To paraphrase: "Comments should explain why you did something, not how you did it.  If they want to know how you did it, they'll just look at your code."

That sums it up nicely.  If you're one of the many, many, many, many, many, many developers who thinks commenting code is pointless, consider that nugget next time and see if it helps.

Thursday, December 11, 2014

Entity-ORM Hybrid

Sometimes you end up working on a project that shifts away from Entity Framework in favor of something else, ADO.NET, Linq to Sql, an available ORM, or maybe even a homegrown ORM.  That's what happened at one client I've worked with.  There were a few reasons to move away from EF, mostly based on misunderstandings, database design, and lack of knowledge of the product.  But there are still some things EF does better than some other methods.  Instead of writing a new stored procedure in SQL Server every time we need to update one field, we set out to use the features of EF to make that happen a little more easily.  After all, isn't it better when you can pass two variables to your repository and just wait for the true/false response?  That's right, it is better.

Here's the code:

   1:  public bool Save<T>(T domainModel, string idFieldName) where T : class
   2:          {
   3:              var type = typeof(T);
   4:              var idProperty = type.GetProperty(idFieldName);
   5:              var domainModelIdValue = (int)idProperty.GetValue(domainModel, null);
   6:   
   7:              var match =
   8:                  Context.Set<T>().Where(PropertyEquals<T, int>(idProperty, domainModelIdValue)).ToList();
   9:   
  10:              // if there's more than one record with a matching ID field (which shouldn't be possible as long as our field is specified correctly) we can't do this update
  11:              // also, if there's no match from the database on the ID field, we can't do this update
  12:              if (!match.Any() || match.Count() > 1)
  13:              {
  14:                  return false;
  15:              }
  16:   
  17:              var firstMatch = match.First();
  18:   
  19:              // set all properties from the domain model passed to the match found in the database
  20:              Context.Entry(firstMatch).CurrentValues.SetValues(domainModel);
  21:   
  22:              var numberOfObjectsWrittenToUnderlyingDatabase = Context.SaveChanges();
  23:   
  24:              return numberOfObjectsWrittenToUnderlyingDatabase == 1;
  25:          }
  26:   
  27:          public Expression<Func<TItem, bool>> PropertyEquals<TItem, TValue>(PropertyInfo property, TValue value)
  28:          {
  29:              var param = Expression.Parameter(typeof(TItem));
  30:              var expressionProperty = Expression.Property(param, property);
  31:   
  32:              BinaryExpression body;
  33:              if (Nullable.GetUnderlyingType(expressionProperty.Type) != null)
  34:              {
  35:                  var comparisonValue = Expression.Convert(Expression.Constant(value), typeof(int?));
  36:                  body = Expression.Equal(expressionProperty, comparisonValue);
  37:              }
  38:              else
  39:              {
  40:                  body = Expression.Equal(Expression.Property(param, property), Expression.Constant(value));
  41:              }
  42:   
  43:              return Expression.Lambda<Func<TItem, bool>>(body, param);
  44:          }

Tuesday, December 2, 2014

Angular ng-include

If you need to bring one HTML file into another using Angular, you may want to take advantage of the ng-include directive (documentation can be found here).  Using ng-include will bring in the specified HTML file.

<div ng-include="'/Pages/SomeApp/Ordering.html'">

Be careful, though!  I ran into a problem with this directive that was driving me crazy.  I had used ng-include in a file that also had an ng-include in it and it caused me no small headache.  Others have done the nested ng-include so it may have just been my specific usage that was causing problems, but it's worth noting.

In case you're seeing something similar, I was forced to use IE 8 (you can peruse my hatred of that browser in other posts) and whenever the ng-hide class was applied, the entire screen went blank.  The DOM was technically still there, but you couldn't see anything.  Good times.  Or not.

Plain HTML as a Partial View

Sometimes you just need to include a plain HTML file in another MVC view.  It's easy enough.  Just use the code below and you should be all set.

@Html.Raw(File.ReadAllText(Server.MapPath("~/Pages/SomeApp/Ordering.html")))

Friday, November 7, 2014

Scope Objects in the Console

When you need to quickly print a scope value in the console (I use Chrome), you can use this helpful little guy:

angular.element("#elementId").scope()

Just a little bit of code that can help immensely.