How to Learn New Technology (as in Language / Framework / Tools)

  1. Watch videos (google it!).
  2. Read white paper (to understand the architecture).
  3. Buy, or borrow, and read the books.
  4. Attend the user groups.
  5. Use it! – create use cases and apply your learning.
  6. Write about it.

Updates are coming…

Troubleshooting Principles

  1. Understand the system workflow.
  2. Learn the basic design (pattern).
  3. Read the error message thoroughly.

Updates are coming…

Getting to Know James Altucher

I came across this guy named James Altucher today. Apparently, he was a programmer in his early career life. Now, he is a stock trader, entrepreneur, writer and fund manager. Heck, he even plays chess and won a tournament!

Spending few minutes on his blogs I quickly find that most people disagree with his perspective. But, all I have to say about this guy is, he is truly inspiring! Straight forward in his writing (to which I mostly agree) and he always stand on his own principles. And no, I am not one of his relatives nor did I relate to him in anyway. So writing this post about him will not cause Claudia-incident.

I am personally inspired with his idea of writing down any ideas about problems, better-living, anything. Most of these ideas will NOT work, trust me. But, that’s not the point. The point is to stimulate our thinking and ability to generate more ideas, better ideas. The hardest part is to come out with first few ideas, after that, it flows like waterfall. Bottom line is, to exercise the brain and practice problem solving. It’s true when they said we only use 7% (not-so-accurate-number, but you get the idea) of our brain.

Having said that, I just want ping him back in my blog as form of my support.

Here’s some of his articles I read:
10 more reasons why parents should not send their kids to college
8 alternatives to college
how to be the luckiest guy on the planet in 4 easy steps
what it feels like to be rich

ps: one thing I notice though, his first blog post was in feb 2001. But he didn’t post anything until feb 2010. weird, huh?

Use ViewModel (Model Binder) in LINQ to Entity Framework – LINQ Doesn’t Return All Joined Data

When using Entity Framework, I’ve found that the join LINQ query that I specified will not return all the data I needed.

Although the query includes OpmaDB.Property entity, the entity is not included in the result object. So, statement like:

_propertyList.FirstOrDefault().Property.PropertyId

will return the famous-‘Object reference not set to an object of an instance’-error:

To resolve this issue, we can use ViewModel, or Model Binder, instead of the entity itself. This should be the better practice anyway. The query looks like:

And the ViewModel looks like this:

Alternatively, we can also store the whole entity model in the View Model instead of property members of the entity. This is how I would do it, the ViewModel:

And the query:

Select Many to Many Relationship in LINQ to Entity Framework

Linq to Entity Framework query looks little bit different when it comes to selecting database records from two tables in which the relationship is many-to-many.

The database diagram looks like:

In regular T-SQL query, I would normally do this:

SELECT Departments.*, Rooms.*
FROM Departments INNER JOIN DepartmentRoomJunction ON Departments.Id = DepartmentRoomJunction.DepartmentId
	INNER JOIN Rooms ON DepartmentRoomJunction.RoomId = Rooms.Id
	WHERE Departments.Id = 2

The result is:

However, when try to do ‘join’ in Linq, the Intellisense doesn’t show Entity object that I want.

Linq is little bit different when it comes to querying many-to-many relationship. Here’s the code:

var _departments = from d in _context.Departments
                           from r in _context.Rooms
                           where d.Id == 2
                           select d;

This is because in Entity Framework, many-to-many relationship are automatically joined. So, there is no need to join them again in the query. As you can see the Entity data model designer below, both Departments and Rooms are not connected with a junction table, as it would with the database diagram.

What is Dependency Injection?

Martin Fowler original coined the term in his article, “Inversion of Control Containers and the Dependency Injection pattern”. In it, he explains in great details his approach in creating high cohesive application with weak coupling modules. It’s a design pattern that allows a separate assembler module to inject the implementation into a class. The author derived Dependency Injection from Inversion of Controller for a more specific name for this pattern.

In my simple definition, dependency injection is a concept to move dependency between classes / controls / methods / interfaces by place this dependency in a configuration files (such as xml-based config file).

Before the dependency injection time, developers achieve weak coupling by manually injected dependency in a new class. This new class is responsible for bridging the other two classes. This approach introduces new problem such as, when modifications need to be done in the code, the code will need to be recompiled.

Scott Hanselman has a good list of .Net Containers that can be found here.

Another good read for Dependency Injection term is from Wikipedia.

Select First and Second Highest Record in Database

The table is:

So far, I’ve found two ways to do this

TOP()

SELECT TOP(1) *
FROM (SELECT TOP(2) *
    FROM Salary
    ORDER BY Amount DESC) AS TOP2
ORDER BY Amount

MAX()

SELECT MAX(Amount)
FROM Salary
WHERE Amount Not In (SELECT MAX(Amount) FROM Salary)

I personally like the “TOP()” approach just because of its flexibility. If I want to select third, fourth, or fifth highest record, all I need to do is change the number in the second TOP() statement.

Getting Data Across (Pass Values) Between View, Controller and External Source in ASP.NET MVC 2 and MVC 3

Here’s the list of my post related to getting data across / pass values between View, Controller and external source:

  1. with Query String (No Modification to Route Table)
  2. with Parameters
  3. with System.Web.Mvc.FormCollection
  4. with Domain Model and / or View Model
  5. (MVC 2) with JSON + ViewModel
  6. (MVC 3) with JSON + VIewModel
  7. (MVC 3) with JSON + ViewModel (Multiple Objects)

JSON Binding Support to Post Array Object with ViewModel in ASP.NET MVC 3

In JSON Binding Support to Post Action Method with ViewModel in ASP.NET MVC 3, I explained how to take advantage of JSON Binding Support in ASP.NET MVC 3 to post ViewModel object to the Controller. This post will extend the original post to allow posting of multiple objects.

I will use the same ViewModel as in the original post with the addition of the following object, which basically contains list of Person object:

public class Persons
{
    public List<Person> PersonList { get; set; }
}

The Controller will be pretty much the same as the original post, with the exception of the parameter:

[HttpPost]
public ActionResult SubmitMultiplePostObjectsInJsonWithViewModel(Development.MvcApp.Models.Persons objectsModel)
{
    return View(objectsModel);
}

This will allow the Controller to receive multiple Person objects as Persons ViewModel.

Here’s the JSON object I sent to the Controller:

{ "PersonList": [{ "Firstname": "testFirstname1", "Lastname": "testLastname1", "Email": "testEmail1", "PhoneNumber": "testNumber1" }, { "Firstname": "testFirstname2", "Lastname": "testLastname2", "Email": "testEmail2", "PhoneNumber": "testNumber2"}] }

The request:

The response:

Few important notes:

  1. The ViewModel object’s properties (“PersonList”) must match JSON object’s string.
  2. The ViewModel object’s properties must have “{ get; set; }”.
  3. The request’s Content Type must be “application/json”.

JSON Binding Support to Post Action Method with ViewModel in ASP.NET MVC 2

JSON Binding Support is only available in ASP.NET MVC 3. However, there is a workaround we can do to make this work in ASP.NET MVC 2.

I will use the same ViewModel in ASP.NET MVC 3 Post.

Here’s the Controller:

public ActionResult SubmitPostObjectInJsonWithViewModel(string objectModel)
{
    System.Web.Script.Serialization.JavaScriptSerializer _jsonDeserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var _person = _jsonDeserializer.Deserialize(objectModel, new Development.MvcApp.Models.Person().GetType());

    return Json(_person);
}

For simple demonstration purpose, the Controller only returns Json object pass to the Controller.

As mentioned in ASP.NET MVC 3 Post, I use Poster, a Mozilla Firefox add-on to test posting to the Controller.

Here’s the request:

Here’s the response:

Basically, this approach is the same as what I described here. We take string input (which essentially is a JSON string) in our Controller and deserialize it to our ViewModel object.

Few things to note:

  1. The request must NOT be “application/json”. The request is a regular post action request.
  2. Parameter used in Controller must match the request parameter’s name.
  3. System.Web.Script.Serialization.JavaScriptSerializer is obsoleting according to Scott Gu’s post, but will still be available in few .Net versions to come.