Brian's Blog

items I see across my tribes

Climb the Right Ladder

March 25
by briancarter 25. March 2013 13:12

It's better to be at the bottom of the ladder you want to climb than the top of the one you don't.

Categories:

Html.Partial and HTML.Action

March 24
by briancarter 24. March 2013 10:39

Terminology and how different groups/companies give names to coding elements increase the time required to contribute for new team members. I was helping someone out today – and they changed the MVC terms for Partial and Action.

Html.Partial
Render a Partial View without hitting a controller action first.

  1. Use Html.Partial when you are rendering static content or,
  2. If you are going to pass data from the ViewModel that is being sent to the main view

Html.Action
Html.Action Call a Controller Action, which may return a view/partial view (or may not, it could return JSON, or other things).

  1. Use Html.Action when you actually need to retrieve additional data from the server to populate the partial view

Basically, if is static, use Html.Partial. If dynamic, model independent data, use Html.RenderAction(). There are probably more scenarios, but this will give you a good idea of where/how to go.

Also, I would use RenderPartial and RenderAction in most cases, just because these methods output their content into the same TextWriter object as used in the current template. In contrast to this, @Html.Partial and @Html.Action methods create their own TextWriter instances each time and buffer all their content into memory.

@Html.Action() – Invokes the specified child action method and returns the result as an HTML string.

@{ Html.RenderAction(); } – Invokes the specified child action method and renders the result inline in the parent view.

Many terms to clarify in MVC Smile

Categories:

You've probably heard this one......but it cracked me up!

March 23
by briancarter 23. March 2013 09:24

Five cannibals get appointed as programmers in an IT company. During the welcoming ceremony the boss says: "You`re all part of our team now. You can earn good money here, and you can go to the company canteen for something to eat. So don`t trouble the other employees. The cannibals promise not to trouble the other employees.

A month later the boss returns and says: "You`re all working very hard, and I`m very satisfied with all of you. However, one of our cleaners has disappeared. Do any of you know what happened to her?" The cannibals disavowed all knowledge of the missing cleaner.

After the boss left, the leader of the cannibals says to the others: "Which of you idiots ate the cleaner?" A hand raises hesitantly, to which the leader of the cannibals says: "You fool! For four weeks we`ve been eating Architects, Team Leads, and Scrum Masters so no one would notice anything, and you have to go and eat the cleaner!"

Categories:

Git–details on branching

March 13
by briancarter 13. March 2013 23:30

One way to add a new branch to the remote repository is to first add the branch to your local repository and then push that local branch to the remote repository.

Let’s see what branches we have now:

git branch

We have just one branch. Not much to shake a stick at. So create a new branch named v0:

git branch v0


Then push the new branch named v0 to the remote repository named origin. The git push syntax is: git push [remote-repository-name] [branch-or-commit-name]:

git push origin v0

Currently the master and v0 branches are identical, but they will diverge (the whole point of branches is to diverge) as users make different commits to each branch.

The person who created the branch has more configuration to do to make their local v0 branch configured correctly.  This, configures git to automatically pull/fetch from the new remote v0 branch, without having to specify the v0 repository and branch name every time using git pull or git fetch.

git branch --set-upstream v0 origin/v0

Branch v0 set up to track remote branch v0 from origin.  That does the job, and from now on, all creator has to type is:

git pull
-------------------------------------

Tracking The New Branch: Other Devs

When other devs git clone the shared repository, the git clone command will automatically:

  • Create the a new local branch named v0
  • Configure their local repository to correctly track changes in the new v0 branch. For example, when the users are on the new local v0 branch, The user can type git fetch, git pull, and git push without specifying the origin remote andv0 branch with every command.

The next time devs retrieves the latest commits from the shared repository, they will see the new v0 branch automatically created locally:

git pull

Devs can then switch to the new branch:

git checkout v0
Devs can then commit to the new branch:

git commit -a –m “added a new line to”

Devs can then commit to the remote branch:

git push

Categories:

Font icons not working on IE

March 13
by briancarter 13. March 2013 22:21

After some searching, found the fix for web font icons not showing up on IE when deployed to IIS.  Add this to web.config:

  <system.webServer>
   <staticContent>
      <mimeMap fileExtension=".woff" mimeType="application/octet-stream" />
     </staticContent>

Categories:

Major GIT commands with examples

March 05
by briancarter 5. March 2013 07:19

Here you will find a list with the major commands, their short descriptions and exemplary usage.  After using Git with Bitbucket for awhile, the distributed version control is showing how beneficial it is over SVN.

image

Bitbucket Examples:

1) Setup Git on your machine if you haven't already done so.

mkdir /path/to/your/project
cd /path/to/your/project
git init
git remote add origin https://briancarter@bitbucket.org/briancarter/mrs.git

2) Let's publish it to Bitbucket

cd /path/to/my/repo
git remote add origin https://briancarter@bitbucket.org/briancarter/mrs.git
git push -u origin --all   # to push changes for the first time

 

Major GIT commands:

  • git config

    Sets configuration values for your user name, email, gpg key, preferred diff algorithm, file formats and more.
    Example: git config --global user.name "My Name"
                     git config --global user.email "user@domain.com"
    cat ~/.gitconfig
    [user]
        name = My Name
        email = user@domain.com

  • git init

    Initializes a git repository – creates the initial ‘.git’ directory in a new or in an existing project.
    Example: cd /home/user/my_new_git_folder/
    git init

  • git clone

    Makes a Git repository copy from a remote source. Also adds the original location as a remote so you can fetch from it again and push to it if you have permissions.
    Example: git clone git@github.com:user/test.git

  • git add

    Adds files changes in your working directory to your index.
    Example: git add .

  • git rm

    Removes files from your index and your working directory so they will not be tracked.
    Example: git rm filename

  • git commit

    Takes all of the changes written in the index, creates a new commit object pointing to it and sets the branch to point to that new commit.
    Examples: git commit -m ‘committing added changes’
    git commit -a -m "committing all changes, equals to git add and git commit’"

  • git status

    Shows you the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).
    Example: git status
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #    README
    nothing added to commit but untracked files present (use "git add" to track)

  • git branch

    Lists existing branches, including remote branches if ‘-a’ is provided. Creates a new branch if a branch name is provided.
    Example: git branch -a
    * master
      remotes/origin/master 

     


  • git checkout

    Checks out a different branch – switches branches by updating the index, working tree, and HEAD to reflect the chosen branch.
    Example: git checkout newbranch

  • git merge

    Merges one or more branches into your current branch and automatically creates a new commit if there are no conflicts.
    Example: git merge newbranchversion

  • git reset

    Resets your index and working directory to the state of your last commit.
    Example: git reset --hard HEAD

  • git stash

    Temporarily saves changes that you don’t want to commit immediately. You can apply the changes later.
    Example: git stash
    Saved working directory and index state "WIP on master: 84f241e first commit"
    HEAD is now at 84f241e first commit
    (To restore them type "git stash apply")

  • git tag

    Tags a specific commit with a simple, human readable handle that never moves.
    Example: git tag -a v1.0 -m 'this is version 1.0 tag'

  • git fetch

    Fetches all the objects from the remote repository that are not present in the local one.
    Example: git fetch origin

  • git pull

    Fetches the files from the remote repository and merges it with your local one. This command is equal to the git fetch and the git merge sequence.
    Example: git pull origin

  • git push

    Pushes all the modified local objects to the remote repository and advances its branches.
    Example: git push origin master

  • git remote

    Shows all the remote versions of your repository.
    Example: git remote
    origin

  • git log

    Shows a listing of commits on a branch including the corresponding details.
    Example: git log
    commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe
    Author: User <user@domain.com>
    Date:   Mon May 3 09:24:05 2010 +0300
        first commit

  • git show

    Shows information about a git object.
    Example: git show
    commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe
    Author: User <user@domain.com>
    Date:   Mon May 3 09:24:05 2010 +0300
        first commit
    diff --git a/README b/README
    new file mode 100644
    index 0000000..e69de29

  • git ls-tree

    Shows a tree object, including the mode and the name of each item and the SHA-1 value of the blob or the tree that it points to.
    Example: git ls-tree master^{tree}
    100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    README

  • git cat-file

    Used to view the type of an object through the SHA-1 value.
    Example: git cat-file -t e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
    blob

  • git grep

    Lets you search through your trees of content for words and phrases.
    Example: git grep "www.siteground.com" -- *.php

  • git diff

    Generates patch files or statistics of differences between paths or files in your git repository, or your index or your working directory.
    Example: git diff

  • gitk

    Graphical Tcl/Tk based interface to a local Git repository.
    Example: gitk

    Gitk

  • git instaweb

    Runs a web server with an interface into your local repository and automatically directs a web browser to it.
    Example: git instaweb --httpd=webrick
    git instaweb --stop

    Git Instaweb

  • git archive

    Creates a tar or zip file including the contents of a single tree from your repository.
    Example: git archive --format=zip master^ README >file.zip

  • git gc

    Garbage collector for your repository. Optimizes your repository. Should be run occasionally.
    Example: git gc
    Counting objects: 7, done.
    Delta compression using up to 2 threads.
    Compressing objects: 100% (5/5), done.
    Writing objects: 100% (7/7), done.
    Total 7 (delta 1), reused 0 (delta 0)

  • git fsck

    Does an integrity check of the Git file system, identifying corrupted objects.
    Example: git fsck

  • git prune

    Removes objects that are no longer pointed to by any object in any reachable branch.
    Example: git prune

Categories:

MVC ActionResult subtypes

February 23
by briancarter 23. February 2013 10:22

Yesterday, I was asked what is the difference between ViewResult() and ActionResult() in ASP.NET MVC?

ActionResult is an abstract class that can have several subtypes.  Typically most just use ActionResult.  ViewResult to render a specific view to the response tree.  I use JsonResult to return JSON to the client.

 

ActionResult Subtypes

  • ViewResult - Renders a specified view to the response stream

  • PartialViewResult - Renders a specified partial view to the response stream

  • EmptyResult - An empty response is returned

  • RedirectResult - Performs an HTTP redirection to a specified URL

  • RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data

  • JsonResult - Serializes a given ViewData object to JSON format

  • JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client

  • ContentResult - Writes content to the response stream without requiring a view

  • FileContentResult - Returns a file to the client

  • FileStreamResult - Returns a file to the client, which is provided by a Stream

  • FilePathResult - Returns a file to the client

Categories:

IQueryable Doesn’t Get

February 14
by briancarter 14. February 2013 08:31

IQueryable<Customer> GetCustomers() doesn’t actually “get” anything. And that’s at the heart of any IQueryable result: until you iterate over the items in the IQueryable list, your IQueryable list is nothing more than anexpression of the records that you want. When you filter the IQueryable list down, e.g.:

<IQueryable<Customer> youngCustomers = repo.GetCustomers().Where(m => m.Age > 18);

you still haven't gotten anything. All you've done is modify the expression of the records that, at some point, you might want to retrieve! That lazy ol' IQueryable...

If it's easier, think of IQueryable as a Stored Proc.  It sits in your database until someone calls it.  It doesn’t get the results when you create it… it gets them when you ask for it.

The point: IQueryable is first and foremost an expression builder, not a list. When you finally iterate over the "records" in the IQueryable list, IQueryable converts its expression into a SQL statement, fires the SQL statement to retrieve records from the database, and populates (finally) the IQueryable list with the Linq-mapped objects that you've asked for.

Categories:

Steve Jobs Taught Me

October 05
by briancarter 5. October 2012 20:24

He was fired..and came back to make the company better.  He taught me that failures teach you to become stronger and smarter.  There will be lots of bumps in your way, try to overcome them in any way possible and to follow what drives you.

Aim for Simplicity – fine tuning an idea pays in the long run.   Simplicity over complexityl

Categories:

CSV Export

September 25
by briancarter 25. September 2012 08:17

I’ve had to do CSV exports many times.  Decided to document my findings:

CSV is a delimited data format that has fields/columns separated by the comma character and records/rows terminated by newlines. Fields that contain a special character (comma, newline, or double quote), must be enclosed in double quotes. If a line contains a single entry which is the empty string, it may be enclosed in double quotes. If a field's value contains a double quote character it is escaped by placing another double quote character next to it. The CSV file format does not require a specific character encoding, byte order, or line terminator format.

I suggest you read about the basic rules of CSV:

 

 

case "csv":
results = FishingManager.Serialize(resultsDr);
results.statuscode = "200";

//Add the column headers
StringBuilder builder = new StringBuilder();
bool firstColumn = true;
foreach (var value in results.results[0])
{
    // Add separator if this isn't the first value
    if (!firstColumn)
        builder.Append(',');
    // Implement special handling for values that contain comma or quote
    // Enclose in quotes and double up any double quotes
    if (value.Key.IndexOfAny(new char[] { '"', ',' }) != -1)
        builder.AppendFormat("\"{0}\"", value.Key.Replace("\"", "\"\""));
    else
        builder.Append(value.Key);
    firstColumn = false;
}

builder.Append("\r\n");

//Add the rows
foreach (Dictionary<string, object> row in results.results)
{
    firstColumn = true;

    foreach (var x in row)
    {
        // Add separator if this isn't the first value
        if (!firstColumn)
            builder.Append(',');
        // Implement special handling for values that contain comma or quote
        // Enclose in quotes and double up any double quotes
        if (x.Value.ToString().IndexOfAny(new char[] { '"', ',' }) != -1)
            builder.AppendFormat("\"{0}\"", x.Value.ToString().Replace("\"", "\"\""));
        else
            builder.Append(x.Value.ToString());
        firstColumn = false;
    }

    builder.Append("\r\n");
}

returnValue = builder.ToString();

Categories: Development


 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.