SQL Saturday 2013 – San Diego
I hope everyone in southern California is planning to attend this years SQL Saturday on September 21st in San Diego. It’s always a great time and lots of free training! I will be presenting the following session and I hope you will attend.
Wear your favorite concert t-shirt to my sessions and be in the running to win free software!
Rock Your Technical Interview
Room: TBA
Have you ever not gotten a job due to not being prepared for the technical interview? I’ve have interviewed 100′s of software developers and will share my knowledge on how to survive, what we look for and even divulge some of the secrets we use during the process. This session will include advice from engineers just like you that have recently landed a new job!
Here is what people say about this session:
- Chris S. – I wanted to thank you for your DVD on Technical Interviews. I attended your presentation at Desert Code Camp in 2012 and purchased the DVD. Your guidance on how to handle technical and even non-technical questions gave me great support and confidence going into a round of interviews I had. That confidence translated into performance, as I was able to receive attractive offers from multiple companies, and I was able to make an important move that has been a life-altering, exciting change for me.
- Bill W. – Good information! I had forgotten to study the technical questions, did that this morning and I felt I aced my technical interview just now
Thanks Dave!
- Daniel I. – Great presentation because you made me realize that I’m not doing enough to better my career! You struck a chord when you said we should keep our resume up-to-date and on-hand at all times. Thanks for helping me realize something I should have been doing a long time ago. Your DVD is very helpful. The fact that you have hiring managers and recruiters giving interviewing tips is great. I’m glad I bought it!
- Ken – Thanks for the great presentations – really enjoyed all your presentations at the code camp, learned a lot about interviewing and gained a lot of insight into how recruiting process works.

Please rate this session by going to SpeakerRate.
All slides and videos can be found here: http://www.slideshare.net/dotNetDave and on YouTube.
Anyone seen with any item from my Code Camp store, will win some cool swag!
What’s the Purpose of the “using” Statement?
I recently was asked by one of my clients “What’s the Purpose of the “using statements”? ” The developer that asked me this title is Senior Engineer. He went on to state “Looking at the <SOME OTHER> project, I see they didn’t use “using”“. When a senior developer actually asks me this I cringe. What I mean by “actually” is the most developers, no matter at what level, have no idea what this is for or know the “why” either.
Microsoft states that using “provides a convenient syntax that ensures the correct use of IDisposable objects”. Well next you might ask is what are IDisposable objects? They are types that need to release unmanaged resources. These resources are usually (but not always) system resources such as a file, window, or network connections and many more. While .NET (Garbage Collector) maintains and releases managed (.NET type) it does not for other resources.
If you develop a type that needs to clean up these unmanaged resources it’s very important that you implement IDisposible properly. Even more important is as a developer that uses one of these types, you must use it properly! If you don’t, you can cause memory leaks… ok they are not memory leaks you might have heard of in C++ applications (like Internet Explorer) they are “virtual memory leaks” that can eat up all the memory on the system until it stops, not release connections to SQL Server and even worse!
When I’m speaking on this subject at one of my coding standard conference sessions, I ask the audience if anyone has a server that for some unexplained reason they have to reboot it every day or week etc. Unfortunately hands always go up, then I tell them they have a memory leak and lights start going off in their heads. I have personally seen in production projects that were many hundreds of thousands of lines of code that this was not being done correctly at all!
This post is directed towards a developer using one of these types. How do you know if a type implements IDisposible? It’s easy, just type the variable name for the object then . then D to see if the Dispose method comes up in IntelliSense like this:
StreamedImage.Dispose
If you see .Dispose call it as soon as you are done with the object, not at the end of the method. You want to release these resources sooner than later for better memory optimization. The biggest problem with this (besides you remembering to do this) is that if an exception is thrown before Dispose is called then the object might never be properly be released from memory… causing a virtual memory leak. So the safest way to do this is like this:
try { StreamedImage image = new StreamedImage(); } finally { image.Dispose(); }
Using the finally block guarantees that Dispose will be called if an exception was thrown or not. This will prevent any memory issues. In later versions of .NET the using statement or pattern was introduced. Now, you only need to do this:
using(StreamedImage image = new StreamedImage())
{
//Your code goes here
}
That’s it! Very simple. What actually happens when you compile your code the compiler writes the try – finally block for you. The using pattern is just a way to get away with less coding. I also like the readability of the code better too.
I will soon write a post on the proper way to implement the IDisposible pattern for your types.
Entity Framework 6 Adding Lots of Great Features
After barely over a year after Entity Framework 5 was released, version 6 is adding a lot of new and awesome features. Here is what is being shipped:
Runtime
The following features work for models created with Code First or the EF Designer:
- Async Query and Save adds support for the task-based asynchronous patterns that were introduced in .NET 4.5. We’ve created a walkthrough and a feature specification for this feature.
- Connection Resiliency enables automatic recovery from transient connection failures. The feature specification shows how to enable this feature and how to create your own retry policies.
- Code-Based Configuration gives you the option of performing configuration – that was traditionally performed in a config file – in code. We’ve created an overview with some examples and a feature specification.
- Dependency Resolution introduces support for the Service Locator pattern and we’ve factored out some pieces of functionality that can be replaced with custom implementations. We’ve created a feature specification and a list of services that can be injected.
- Interception/SQL logging provides low-level building blocks for interception of EF operations with simple SQL logging built on top. We’ve created a feature specification for this feature and Arthur Vickers has created a multi-part blog series covering this feature.
- Testability improvements make it easier to create test doubles for DbContext and DbSet. We’ve created walkthroughs showing how to take advantage of these changes using a mocking framework orwriting your own test doubles.
- Enums, Spatial and Better Performance on .NET 4.0 - By moving the core components that used to be in the .NET Framework into the EF NuGet package we are now able to offer enum support, spatial data types and the performance improvements from EF5 on .NET 4.0.
- DbContext can now be created with a DbConnection that is already opened which enables scenarios where it would be helpful if the connection could be open when creating the context (such as sharing a connection between components where you can not guarantee the state of the connection).
- Default transaction isolation level is changed to READ_COMMITTED_SNAPSHOT for databases created using Code First, potentially allowing for more scalability and fewer deadlocks.
- DbContext.Database.UseTransaction and DbContext.Database.BeginTransaction are new APIs that enable scenarios where you need to manage your own transactions.
- Improved performance of Enumerable.Contains in LINQ queries.
- Significantly improved warm up time (view generation) – especially for large models – as the result of a contributions from AlirezaHaghshenas and VSavenkov.
- Pluggable Pluralization & Singularization Service was contributed by UnaiZorrilla.
- Improved Transaction Support updates the Entity Framework to provide support for a transaction external to the framework as well as improved ways of creating a transaction within the Framework. See this feature specification for details.
- Entity and complex types can now be nested inside classes.
- Custom implementations of Equals or GetHashCode on entity classes are now supported. See thefeature specification for more details.
- DbSet.AddRange/RemoveRange were contributed by UnaiZorrilla and provides an optimized way to add or remove multiple entities from a set.
- DbChangeTracker.HasChanges was contributed by UnaiZorrilla and provides an easy and efficient way to see if there are any pending changes to be saved to the database.
- SqlCeFunctions was contributed by ErikEJ and provides a SQL Compact equivalent to the SqlFunctions.
The following features apply to Code First only:
- Custom Code First Conventions allow write your own conventions to help avoid repetitive configuration. We provide a simple API for lightweight conventions as well as some more complex building blocks to allow you to author more complicated conventions. We’ve created a walkthough and afeature specification for this feature.
- Code First Mapping to Insert/Update/Delete Stored Procedures is now supported. We’ve created afeature specification for this feature.
- Idempotent migrations scripts allow you to generate a SQL script that can upgrade a database at any version up to the latest version. The generated script includes logic to check the __MigrationsHistory table and only apply changes that haven’t been previously applied. Use the following command to generate an idempotent script.
Update-Database -Script -SourceMigration $InitialDatabase
- Configurable Migrations History Table allows you to customize the definition of the migrations history table. This is particularly useful for database providers that require the appropriate data types etc. to be specified for the Migrations History table to work correctly. We’ve created a feature specification for this feature.
- Multiple Contexts per Database removes the previous limitation of one Code First model per database when using Migrations or when Code First automatically created the database for you. We’ve created afeature specification for this feature.
- DbModelBuilder.HasDefaultSchema is a new Code First API that allows the default database schema for a Code First model to be configured in one place. Previously the Code First default schema was hard-coded to “dbo” and the only way to configure the schema to which a table belonged was via the ToTable API.
- DbModelBuilder.Configurations.AddFromAssembly method was contributed by UnaiZorrilla. If you are using configuration classes with the Code First Fluent API, this method allows you to easily add all configuration classes defined in an assembly.
- Custom Migrations Operations were enabled by a contribution from iceclow and this blog post provides an example of using this new feature.
For more info, go to this link: http://blogs.msdn.com/b/adonet/archive/2013/08/21/ef6-release-candidate-available.aspx
What’s Wrong With This Code: Type Design
Here is something I see all the time. Maybe done by lazy programmers. This is real in production code. What is wrong with this type design and why?
{
public int videoId = 0;
public string Filename = string.Empty;
public long Filesize = 0;
public double Speed = 0;
public long Time = 0;
public double Altitude = 0;
public double Longitude = 0;
public double Latitude = 0;
public double Accuracy = 0;
public double Bearing = 0;
public bool HasBearing = false;
public bool HasSpeed = false;
public bool HasAltitude/* = false*/;
public bool HasAccuracy/* = false*/;
public string deviceId = string.Empty;
public string dateReceived = string.Empty;
public string dateTransmitted = string.Empty;
//Rest of code removed for brevity
}
Submit your answers by using comments below. I see at least three issues, one being major.
What’s Wrong With This Code: Class Design
Here is the second post in this series. Proper class/ type design is very important for your app for many reasons. Unfortunately I see issues all the time! Even with supposed senior programmers (like in the example below). So lets see if you can spot all the issues with this class below… there are multiple of them (most of the code has been removed from brevity).
- public class DataAccess
- {
- #region constants
- //default connection string
- private string CONN_STRING = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
- private SqlConnection connection;
- #endregion
- public DataAccess()
- {
- connection = new SqlConnection(CONN_STRING);
- }
- private void OpenConnection()
- {
- if (connection.State != System.Data.ConnectionState.Open)
- {
- connection.Open();
- }
- }
- private void CloseConnection()
- {
- if (connection.State != System.Data.ConnectionState.Closed)
- {
- connection.Close();
- }
- }
- }
Submit your answer using comments.
What’s Wrong With This Code: Exceptions
This is my first post on a series of posts that are going to be devoted to seeing if you can spot the problem with this code. All code is REAL code in production or close to going live. Here is the first one:
Not properly handling Exceptions is .NET is very important part of coding your application. Not doing so can bring your app to a crashing halt! So lets see if you can spot the problem with the code below:
try { ctrl = (Control)Parent.FindControl("SomeControl"); if (ctrl != null) { PrepareControlForExport(ctrl); ExportControl(); } } catch (Exception ex) { }
Send in your response by using comments. Will post answer later.
Visual Studio Live!
Hey Developers — get your backstage pass to the Microsoft Platform at the Visual Studio Live event!
Visual Studio Live!
August 19, 2013 thru August 23rd, 2013
Celebrating 20 years of education and training for the developer community, Visual Studio Live! is coming back to Microsoft headquarters!
This is the event in Redmond Washington where developers, software architects and designers will connect for five days of unbiased and cutting-edge education on the Microsoft platform. With 60+ sessions led by industry experts and Microsoft insiders, you will walk away from this event having expanded your .NET skills and the ability to build better applications.
Visual Studio Live! Redmond WA
08/19/2013 -08/23/2013
Microsoft Conference Center (MSCC) Redmond, WA
I was able to obtain an exclusive discount code which provides my readers a $500 savings on any of the 5 day conference pacakages. Use code: UGRD9
For more details see:
www.vslive.com/redmond