Monday, May 20, 2013

AppFactor US Events - Compete for a Surface Pro!!

I'm very excited to announce that our team at Microsoft is putting on a series of events where you can complete and win cool prizes.

If you CAN attend one in person, that would be AWESOME and you'll have us a FREE RESOURCE during this time. Ex. http://ohours.org/adamtuliper
Our entire West Region team will be available to schedule free time via in-person, skype, or phone. Check out the oHours links over at http://aka.ms/wrteam

If you cannot attend in person, you can still compete online. I know, pretty darn cool. Here's are the details

Day

Date

City, State

Event Title

Fri

5/31/2013

Bellevue, WA (LS)

Windows 8 App Factor Learn

Fri

5/31/2013

San Diego, CA

Windows 8 App Factor Learn

Fri

5/31/2013

San Francisco, CA

Windows 8 App Factor Learn

Fri

6/7/2013

Irvine, CA

Windows 8 App Factor Learn

Fri

6/7/2013

Mountain View, CA

Windows 8 App Factor Learn

Fri

6/7/2013

Portland, OR

Windows 8 App Factor Learn

Thurs

6/13/2013

Tempe, AZ

Windows 8 App Factor Learn

Sat

6/22/2013

Irvine, CA

Windows 8 App Factor Compete

Sat

6/22/2013

San Francisco, CA

Windows 8 App Factor Compete

Have app ideas? Have software development skills? If so, that’s all you need to learn, compete and win! 10 Cities, 100’s of developers and 1000’s of great ideas. Take your skills and ideas into a new economy and register for the Windows 8 App Factor series: http://www.windows8appfactor.com Join us at Windows 8 App Factor Learn, a fun day of “training and auditions”: • Learn what you need to know from app idea to submission. • Form a team where you can use your skills and compete to win. • Create an application from scratch or use a beautiful sample to get started. • Free consulting from the local evangelist team (in person or online). Once you discover your app potential, your team can hack for a couple of weeks and participate in Windows 8 App Factor Compete (part 2 of the series). Windows 8 App Factor Compete will provide you with an opportunity to show off your app and hard work and potentially win (not offered in all cities). If you don’t have time to attend in person, no big deal, just submit your work of art online to be *eligible to win fabulous prizes. Watch and see which developers truly have the App Factor, register today! #win8appfactor *More information to be provided on rules and eligibility for online or in-person submission at the first event ‘Windows 8 App Factor Learn’ in your city.

Thursday, April 25, 2013

Windows 8 ListView Data Binding - Dynamically adding and removing items from a WinJS ListView

A common theme in your app may be the dynamic additions to a WinJS ListView. The ListView control, while HTMl/JavaScript based acts like you would expect a XAML List with dynamic binding to act. What I mean is that you can dynamically add and remove items and have them automatically appear. There are no tricks to make this happen, you just have to ensure you do it correctly. The binding steps are simply 1. Create a WinJS.Binding.List wrapper around your collection. 2. Specify a template and bind the above list to your ListView
Now let's look at two ways to bind data 
1. Using a typical JSON array
2. Using a defined object
DO ensure the item you are binding is a WinJS.Binding.List ie
var customerList = new WinJS.Binding.List(customers);

A word on closing tags

DO ensure that your elements have CLOSING TAGS.
I cannot stress this enough. You will bang your head on the table trying to debug your issue.
Mark my words - ensure your non-void elements have separate closing tags.

Example - DO
<div id='content'></div>

Example - DO NOT
<div id='content' />

Most elements in HTML5 need closing tags.

There are only about two 'self closing' tags and the 'void' tags (like img) simply ignore the closing slash, so it's not really required in the case of void tags anyways. If you want more details on that see:
  • Start Tags (see point 6)
  • Elements from the MathML namespace and the SVG namespace are the only ones that can use the self closing '/', see here
  • And if you really want to know the 16 void elements that don't need a closing tag they are: area, base, br, col, embed, hr, img, input, keygen, link, menuitem, meta, param, source, track, wbr defined Here


Ok, let's get to it!

Using a typical JSON array for binding

Here's your HTML
<div id="simpleBinding" data-win-control="WinJS.UI.ListView"></div>

<div id="moreBinding" class="bindingSample" data-win-control="WinJS.UI.ListView"></div>

<div id="simpleBindingTemplate" data-win-control="WinJS.Binding.Template">
    <h3 data-win-bind="innerText:name" />
    <span>Name :</span><span data-win-bind="textContent: name"></span>
    <img data-win-bind="src:imageUrl" />
</div>
And your JavaScript
//your data to bind
var people = [{ name: 'mary', birthday: '1/1/2010' }, 
              { name: 'fred', birthday: '1/1/2012' }];

//create a bindable list from the data
var simpleFriends = new WinJS.Binding.List(people);

//get the ref to your list view, note we call winControl to get 
//the 'functional' windows control from it to do cool things with
var simpleListView = document.getElementById('simpleBinding').winControl;

//get your div that is the template to render each item as
var simpleTemplate = document.getElementById('simpleBindingTemplate');
//can _could_ also set in the markup for this element where you declare your list
//
simpleListView.itemTemplate = simpleTemplate;

//and bind it!!
simpleListView.itemDataSource = simpleFriends.dataSource;

//add a new item. the binding update happens automatically
//Could use a Person object we defined
//simpleFriends.push(new Person({ name: "Dynamically added", birthday: "2/2/2002"}));

//or JSON
simpleFriends.push({ name: "Dynamically added", birthday: "2/2/2002"})

And now for using an Object for binding

The code is is almost exact except instead of creating an array we are creating a customer object that we can then 'new up'.
The HTML is the same block as above, and the JavaScript is as follows:
//Create a customer object. You can also do this via WinJS.Class.define as well 
//to create a 'class'. The syntax is very similar.
var Customer = WinJS.Binding.define({
    customerId: "",
    name: ""
});

var customers = [
    //note - if you add a field here that wasn't defined in the binding above it is just ignored and not bound to anything
    new Customer({ customerId: "1", name: "Mary Jones" }), 
    new Customer({ customerId: "2", name: "Sally Smith" }),
    new Customer({ customerId: "2", name: "Zadie Zebrazeus" })
];
            
var customerList = new WinJS.Binding.List(customers);
var moreBindingLv = document.getElementById('moreBinding').winControl;

//Assign our data and rendering template
moreBindingLv.itemDataSource = customerList.dataSource;
moreBindingLv.itemTemplate = simpleTemplate; //can also set in the markup for this element
            

customerList.push(new Customer({ customerId: "4", name: 'Fourth Awesome Person' }));
customerList.push(new Customer({ customerId: "5", name: 'Fifth Awesome Person' }));
            
//now delete item 5 (zero based)
customerList.splice(4, 1);
            

Thursday, March 21, 2013

HTML5 Developers LA - Windows 8 HTML Apps

I had a great time with the HTML5 Developers group on Tuesday night. Here are the slides from that session, looking forward to seeing you all at a future date!
Slides online here

Sunday, December 16, 2012

Read Only Entities in the Entity Framework

Recently I had a need in a project to have a few read-only entities because the tables didn't belong to my application. I didn't want to create views into those tables, I just wanted a read-only entity. If we set the entity's state such that it isn't tracked in the ObjectContext, then no changes would ever be persisted for it.

The following allows usage of context.States (ie states of the USA) but doesn't track any changes.

/// Using a dbquery since this is readonly.
/// </summary>
public DbQuery<State> States
{
  get
  {
    // Don't track changes to query results
    return Set<State>().AsNoTracking();
 }
}

Tuesday, December 4, 2012

Logging Complete Objects and Properties with Log4net

I came across a code sample that gave me an idea.

The problem

At times we need to log objects into a debug or error log - the full object. object.ToString() doesn't give us a log of all the properties.


The Solutions

1. Create an extension method for .ToString() for all objects but the impact of that is far reaching. Let's scratch that method.
2. Create an object dumper and tell log4net to use it. We can use the ObjectDumper code from Microsoft

I found this code at: http://codingcockerel.co.uk/2008/12/28/add-logging-to-your-application-using-log4net-part-two/
This doesn't quite get us there, but almost.

We'll need the code ObjectDumper.cs at various locations - here's one that will work:
ObjectDumper.cs


public class Log4NetObjectLogger : IObjectRenderer
    {
        public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
        {
            var ex = obj as Exception;

            //if its not an exception, dump it. If it's an exception, log extended details.
            if (ex == null)
            {
                //by default log up to 10 levels deep.
                ObjectDumper.Write(obj,10,writer);
            }
            else
            {
                while (ex != null)
                {
                    RenderException(ex, writer);
                    ex = ex.InnerException;
                }
            }
        }

        private void RenderException(Exception ex, TextWriter writer)
        {
            writer.WriteLine(string.Format("Type: {0}", ex.GetType().FullName));
            writer.WriteLine(string.Format("Message: {0}", ex.Message));
            writer.WriteLine(string.Format("Source: {0}", ex.Source));
            writer.WriteLine(string.Format("TargetSite: {0}", ex.TargetSite));
            RenderExceptionData(ex, writer);
            writer.WriteLine(string.Format("StackTrace: {0}", ex.StackTrace));
        }

        private void RenderExceptionData(Exception ex, TextWriter writer)
        {
            foreach (DictionaryEntry entry in ex.Data)
            {
                writer.WriteLine(string.Format("{0}: {1}", entry.Key, entry.Value));
            }
        }
    }


Now we can log our FULL object details up to 10 levels deep by doing simply the following:

log.Debug(customer);
In order to configure the custom logger, edit your app.config or web.config and find the <log4net> element. Add your custom logger as follows:
<log4net>

    <renderer renderedclass="System.Object" renderingclass="YourProjectName .NameSpace.ToYourClass.Log4NetObjectLogger, YourProjectName"></renderer>

    <root>.....</root>



Note the format is YourClassFullNameSpace.YourLogger, YourProjectName

That's all there is to it - now you can view the state of your object at any time by a simple log call.


Thursday, November 22, 2012

What's new in ASP.NET 4.5

For those that attended my recent talks on this here is a link to the slides.
What's new in ASP.NET 4.5.pptx
Code will be up on github shortly
Thanks!

Wednesday, September 19, 2012

30 tips and Tricks for the ASP.NET Developer

I had a great time this week giving this talk a couple times.

Code is available at:
 https://github.com/adamtuliper/30TipsAndTricksForASPNET
Thanks for attending!!
Keep an eye out on Twitter if you don't follow me already there, I post tidbits there as well.