<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Undisciplined Bytes</title>
	<atom:link href="http://www.undisciplinedbytes.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.undisciplinedbytes.com</link>
	<description>Web Application Development Tips, Tricks and Techniques</description>
	<lastBuildDate>Tue, 31 Jan 2012 09:07:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Entity Framework</title>
		<link>http://www.undisciplinedbytes.com/2012/01/entity-framework/</link>
		<comments>http://www.undisciplinedbytes.com/2012/01/entity-framework/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 09:07:24 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=640</guid>
		<description><![CDATA[Entity framework is the new kid on the block on the ORM realm. It’s Microsoft new technology for data access, and although it didn’t receive very good comments on its first version, things have been greatly improved in its latest release. It now looks really interesting, with some features which surpass the most seasoned ORM [...]]]></description>
			<content:encoded><![CDATA[<p>Entity framework is the new kid on the block on the ORM realm. It’s Microsoft new technology for data access, and although it didn’t receive very good comments on its first version, things have been greatly improved in its latest release. It now looks really interesting, with some features which surpass the most seasoned ORM frameworks that have been in the game for years.</p>
<p>Here’s what Entity Framework (EF) has to offer:</p>
<p><span id="more-640"></span></p>
<ul>
<li>Using EF with the <strong>Model First approach</strong>, you design a schema of the model you want to implement, and EF generates a database and the code to implement it. Now, that’s what I call saving time! Using this approach, you can get going really really quick.</li>
<li>If you already have the code for your model, then EF can generate the database to store this model as well as the rest of the code handling persistance to this database, using the<strong> Code First approach</strong>.</li>
<li>In case you already have the database, EF can generate the code using the <strong>Database First approach</strong>.</li>
<li><strong>EF uses </strong><strong>Plain .Net objects (POCOs)</strong> with navigational properties to implement your model. Very easy stuff. The way EF lets you interact with the data once it’s retrieved from the DB is piece of cake (not the fact of data retrieval itself, that can get tricky).</li>
<li><strong>Automatic code generation</strong> with T4 templates. Once you have EF set up in your project, there’s a bunch of templates to generate a lot of code for you. That doesn’t mean you must use it, but it’s there for your convenience, in case you think some templates can help you. Me myself I use some of the T4 templates, but not all of them. There’s some things I actually prefer doing manually.</li>
<li><strong>Self-tracking entities</strong> will greatly simplify the process of implementing an over-the-wire scenario, in which you have different layers spanning across different systems and you have to serialize and de-serialize your entities. EF makes this really easy.</li>
<li><strong>Local caching</strong>, to speed up your data access.</li>
<li><strong>Lazy loading</strong>, which can improve overall application performance.</li>
<li>Support for <strong>LINQ</strong> (both coming from Microsoft, you would expect this, right?).</li>
<li><strong>Convention over configuration.</strong> The hottest trend in software development. You wouldn’t think EF would miss it, would you? When starting up a new project with EF, the only thing you need to configure are the database server and its name. Nothing else. All other things are taken care of for you. Of course, if you want more control over what EF is doing, you can tweak it to meet your criteria. But that isn’t something you have do right at the beginning. This way, you can be productive much faster.</li>
<li><strong>Support for the major databases</strong>: MS SQL Server (of course), Oracle, MySQL, PostgreSQL, etc…</li>
</ul>
<p>And last but not least:</p>
<ul>
<li>Coming from a big player in the industry like Microsoft, Entity Framework has great resources and a great community behind it. If you ever have a problem with EF, or want to tweak its behavior, chances are you’ll be finding what you need really fast. If you’re a seasoned developer, you’ll find this is one of the most important features of EF.</li>
</ul>
<p>If you haven’t tried it out yet, don’t wait any longer and <a href="http://msdn.microsoft.com/en-us/library/bb399572.aspx">see for yourself what I’m talking about!</a></p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/' rel='bookmark' title='Permanent Link: Persistance layer: object-relational mapping tools'>Persistance layer: object-relational mapping tools</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/03/which-database-to-use-for-a-personal-project/' rel='bookmark' title='Permanent Link: Which database to use for a personal project?'>Which database to use for a personal project?</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/01/sluggish-database-view-materialize-it/' rel='bookmark' title='Permanent Link: Sluggish database view? Materialize it!'>Sluggish database view? Materialize it!</a></li>
<li><a href='http://www.undisciplinedbytes.com/2012/01/asp-net-4-5-and-visual-studio-11/' rel='bookmark' title='Permanent Link: ASP.NET 4.5 and Visual Studio 11'>ASP.NET 4.5 and Visual Studio 11</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2012/01/entity-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET 4.5 and Visual Studio 11</title>
		<link>http://www.undisciplinedbytes.com/2012/01/asp-net-4-5-and-visual-studio-11/</link>
		<comments>http://www.undisciplinedbytes.com/2012/01/asp-net-4-5-and-visual-studio-11/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 08:45:27 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=674</guid>
		<description><![CDATA[Back in September 2011 the next versions of ASP.NET and Visual Studio were announced. Microsoft has set up a page informing about all of the new features we can find in them. Check them out:


ASP.NET Core Runtime and Framework

Asynchronously Reading and Writing HTTP Requests and Responses.
Improvements to HttpRequest handling
Asynchronously flushing a response
Support for await and [...]]]></description>
			<content:encoded><![CDATA[<p>Back in September 2011 the next versions of ASP.NET and Visual Studio were announced. Microsoft has set up <a href="http://www.asp.net/vnext/overview/whitepapers/whats-new#_Toc303354469" target="_blank">a page informing about all of the new features</a> we can find in them. Check them out:</p>
<p><span id="more-674"></span></p>
<ul>
<li><strong>ASP.NET Core Runtime and Framework</strong>
<ul>
<li>Asynchronously Reading and Writing HTTP Requests and Responses.</li>
<li>Improvements to HttpRequest handling</li>
<li>Asynchronously flushing a response</li>
<li>Support for <em>await</em> and <em>Task</em>-Based Asynchronous Modules and Handlers</li>
<li>Asynchronous HTTP modules</li>
<li>Asynchronous HTTP handlers</li>
<li>New ASP.NET Request Validation Features</li>
<li>Deferred (&#8221;lazy&#8221;) request validation</li>
<li>Support for unvalidated requests</li>
<li>Anti-XSS Library</li>
<li>Support for WebSockets Protocol</li>
<li>Bundling and Minification</li>
<li>Performance Improvements for Web Hosting
<ul>
<li>Key Performance Factors</li>
<li>Requirements for New Performance Features</li>
<li>Sharing Common Assemblies</li>
<li>Using multi-Core JIT compilation for faster startup</li>
<li>Tuning garbage collection to optimize for memory</li>
<li>Prefetching for web applications</li>
</ul>
</li>
</ul>
</li>
<li><strong>ASP.NET Web Forms</strong>
<ul>
<li>Strongly Typed Data Controls</li>
<li>Model Binding
<ul>
<li>Selecting data</li>
<li>Value providers</li>
<li>Filtering by values from a control</li>
</ul>
</li>
<li>HTML Encoded Data-Binding Expressions</li>
<li>Unobtrusive Validation</li>
<li>HTML5 Updates</li>
</ul>
</li>
<li><strong>ASP.NET MVC 4 </strong></li>
<li><strong>ASP.NET Web Pages 2</strong>
<ul>
<li>New and Updated Site Templates</li>
<li>Improved Input Validation</li>
<li>Resource Management</li>
<li>Enhanced Membership and Authentication</li>
<li>Side-by-side Execution</li>
<li>Mobile Device Rendering</li>
<li>The Maps Helper</li>
</ul>
</li>
<li><strong>Visual Web Developer 11 Developer Preview.</strong>
<ul>
<li>HTML Editor
<ul>
<li>Smart Tasks</li>
<li>WAI-ARIA support</li>
<li>New HTML5 snippets</li>
<li>Extract to user control</li>
<li>IntelliSense for code nuggets in attributes</li>
<li>Automatic renaming of matching tag when you rename an opening or closing tag</li>
<li>Event handler generation</li>
<li>Smart indent</li>
<li>Auto-reduce statement completion</li>
</ul>
</li>
<li>JavaScript Editor
<ul>
<li>Code outlining</li>
<li>Brace matching</li>
<li>Go to Definition</li>
<li>ECMAScript5 support</li>
<li>DOM IntelliSense</li>
<li>VSDOC signature overloads</li>
<li>Implicit references</li>
</ul>
</li>
<li>CSS Editor
<ul>
<li>Auto-reduce statement completion</li>
<li>Hierarchical indentation</li>
<li>CSS hacks support</li>
<li>Vendor specific schemas (-moz-, -webkit)</li>
<li>Commenting and uncommenting support</li>
<li>Color picker</li>
<li>Snippets</li>
<li>Custom regions</li>
</ul>
</li>
<li>Page Inspector</li>
<li>Publishing
<ul>
<li>Publish profiles</li>
<li>ASP.NET precompilation and merge</li>
</ul>
</li>
<li>IIS Express</li>
</ul>
</li>
</ul>
<p>These are developer previews, which mean that they should be used for <strong>testing purposes only, and never in production environments</strong> – not yet, at least.</p>
<p>In case you’re interested, Microsoft’s central hub for ASP.NET’s next version articles is <a href="http://www.asp.net/vnext" target="_blank">ASP.NET vNext section</a>.</p>
<p>Via <a title="ASP.NET" href="http://www.asp.net/" target="_blank">Microsoft&#8217;s ASP.NET web site</a>.</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2010/02/how-to-make-an-ajax-call-using-jquery-and-asp-net-i/' rel='bookmark' title='Permanent Link: Different ways to use AJAX in ASP.NET'>Different ways to use AJAX in ASP.NET</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/02/php-or-asp-net/' rel='bookmark' title='Permanent Link: PHP or ASP.NET?'>PHP or ASP.NET?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2012/01/asp-net-4-5-and-visual-studio-11/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Persistance layer: object-relational mapping tools</title>
		<link>http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/</link>
		<comments>http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 10:36:16 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=242</guid>
		<description><![CDATA[Nowadays it&#8217;s very usual to work with object-oriented programming and relational data bases. Relational data bases are all about tables, relations and groups. However, the object-oriented programming paradigm consists of objects, attributes and inter-object relationships. When these objects need to be stored using a relational data base, it&#8217;s very obvious that these two models of [...]]]></description>
			<content:encoded><![CDATA[<p>Nowadays it&#8217;s very usual to work with object-oriented programming and relational data bases. Relational data bases are all about tables, relations and groups. However, the object-oriented programming paradigm consists of objects, attributes and inter-object relationships. When these objects need to be stored using a relational data base, it&#8217;s very obvious that these two models of representing information are very different from each other. We need some kind of translation to transfer data from one to the other.</p>
<p>If we&#8217;re using objects in our application and we want to save them, we&#8217;ll probably use some data base system. We&#8217;ll most likely establish a connection to the data base, create a SQL sentence with the values of the object (or call some kind of stored procedure), and save them to some table. Easy enough, right? Well, this may seem trivial for a small object with 4 or 5 properties. Just consider an object not so small, with 40 or 50 properties. But&#8230; what happens with associations? And&#8230; what if the object itself contains some other objects? We&#8217;ll store them in the data base as well? All of them or just some? What will we do with foreign keys? We can see that, as the application gets more complex, this translation is not so trivial anymore&#8230; As a matter of fact, storing objects in the database can become a very usual source of headaches. There are some studies which state that <strong>up to 35% of code is dedicated to the role of translating data between the application and the data warehouse.</strong></p>
<p><span id="more-242"></span></p>
<p><strong> </strong></p>
<p>And this is where <strong><em>Object-Relational Mapping tools</em></strong> come into play. ORM tools applied to databases transform a DB register to a code object and vice-versa, allowing us to abstract away from writing code to directly access the database. This will speed up development and will allow us to follow the mantra of<strong> focusing on business functionalities, not on application plumbing</strong>.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">But, what exactly can an ORM tool help me with? Well, they usually do some things like:</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Handle object persistance (to a database, for example). You won&#8217;t have to write code for this. And remember, less code means less bugs and less headaches.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Access different databases. Most of these tools support communicating with different database providers (Oracle, MS SQL Server, MySql, Postgre, etc.).</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Cache. Many of these tools offer some way of caching objects in memory, resulting in much faster data access.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Help with design. Some of these tools also provide some kind of wizar or graphical interface to aid in the design of the schema and/or database, which leads us to the following point:</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Create your database for you. If you have your data schema already modeled, some of these tools will not only handle how to get the data or how to write it. They can also create the database.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So now that you&#8217;ve decided to use an ORM, take a look at the most popular ORM tools available for .NET:</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Linq to SQL: not really a full-blown ORM, but it does help with data access.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- NHibernate: I would say NHibernate is one of the most mature ORM projects in .NET, and one of the most used as well.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">- Entity Framework: Microsoft&#8217;s new kid on the block. Its first version didn&#8217;t receive very positive comments, but its latest release looks very good indeed.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">In the next post I&#8217;ll be covering Entity Framework a bit. Stay tuned!</div>
<p>But, what exactly can an ORM tool helps us with? Well, they usually do some things like:</p>
<ul>
<li><strong>Handle object persistance </strong>(to a database, for example). You won&#8217;t have to write code for this. And remember, less code means less bugs and less headaches.</li>
<li><strong>Access different databases</strong>. Most of these tools support communicating with different database providers (Oracle, MS SQL Server, MySql, Postgre, etc.).</li>
<li><strong>Cache</strong>. Many of these tools offer some way of caching objects in memory, resulting in much faster data access.</li>
<li><strong>Help with design</strong>. Some of these tools also provide some kind of wizard or graphical interface to aid in the design of the schema and/or database, which leads us to the following point:</li>
<li><strong>Create your database for you</strong>. If you have your data schema already modeled, some of these tools will not only handle how to get the data or how to write it. They can also create the database.</li>
</ul>
<p>You&#8217;re already convinced to use an ORM for your project, right? Good. Now take a look at the most popular ORM tools available for .NET:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx" target="_blank">Linq to SQL</a>: not really a full-blown ORM, but it does help with data access.</li>
<li><a href="http://nhforge.org/" target="_blank">NHibernate</a>: I would say NHibernate is one of the most mature ORM projects in .NET, and one of the most used as well.</li>
<li><a href="http://msdn.microsoft.com/en-us/data/aa937723.aspx" target="_blank">Entity Framework</a>: Microsoft&#8217;s new kid on the block. Its first version didn&#8217;t receive very positive comments, but its latest release looks very good indeed.</li>
<li><a href="http://www.llblgen.com/defaultgeneric.aspx" target="_blank">LLBLGen Pro</a>: very mature project, although it&#8217;s not the simplest of all.</li>
<li><a href="http://dataobjects.net/" target="_blank">DataObjects.Net</a>: quite new, but very promising. It already has a lot of interesting features. Definetely worth taking a look at.</li>
</ul>
<p>In the next post I&#8217;ll be covering Entity Framework a bit. Stay tuned!</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2012/01/entity-framework/' rel='bookmark' title='Permanent Link: Entity Framework'>Entity Framework</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/04/some-open-source-projects-for-net-you-should-take-a-look-at/' rel='bookmark' title='Permanent Link: Some open source projects for .NET you should take a look at'>Some open source projects for .NET you should take a look at</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/03/which-database-to-use-for-a-personal-project/' rel='bookmark' title='Permanent Link: Which database to use for a personal project?'>Which database to use for a personal project?</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/01/sluggish-database-view-materialize-it/' rel='bookmark' title='Permanent Link: Sluggish database view? Materialize it!'>Sluggish database view? Materialize it!</a></li>
<li><a href='http://www.undisciplinedbytes.com/2009/12/software-design-patterns-ii-creational-patterns/' rel='bookmark' title='Permanent Link: Software design patterns (II): creational patterns'>Software design patterns (II): creational patterns</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallel programming with .NET</title>
		<link>http://www.undisciplinedbytes.com/2011/09/parallel-programming-with-net/</link>
		<comments>http://www.undisciplinedbytes.com/2011/09/parallel-programming-with-net/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 10:35:25 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[backend]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/2011/02/parallel-programming-with-net/</guid>
		<description><![CDATA[When I’m talking to people about the new things that Visual Studio 2010 and ASP.NET 4.0 bring, and the easy it is to write code to be executed in parallel, many people just don’t care. They see it as if it wasn’t related to them. It’s true that some kind of applications don’t really need [...]]]></description>
			<content:encoded><![CDATA[<p>When I’m talking to people about the new things that Visual Studio 2010 and ASP.NET 4.0 bring, and the easy it is to write code to be executed in parallel, many people just don’t care. They see it as if it wasn’t related to them. It’s true that some kind of applications don’t really need this type of optimization… but that’s not an excuse to try to take advantage of our hardware as much as possible.  From several years ago the most common thing in any computer is to have at least two processor cores. As a matter of fact, we’re starting to see now desktop computers with 8-core processors. The trend is to have an ever-increasing number of cores in our computers. This means that any application will have the chance to work in an environment where more than one thread can be executed simultaneously.  Besides, nowadays if you want your software to perform faster, you can no longer just move it to a machine with a faster processor and forget about it. We’re approaching the maximum speed a processor can get, so improvements in next CPUs will come from the side of more threads, not a higher clock-rate.  I think parallel programming  is a key concept all programmers should care about.  ASP.NET 4.0 and Visual Studio 2010 bring new libraries, types and tools to ease multi-core development. ASP.NET 4.0 includes the new “Parallel Extensions”, which are formed by:</p>
<p><span id="more-565"></span></p>
<ul>
<li>Task Parallel Library: this new library, included in the namespace System.Threading.Tasks.Parallel, has new elements to execute tasks in parallel automatically. It’s got, for example, a parallel <em>For</em> loop, and a parallel <em>ForEach</em> loop.</li>
<li>Parallel LINQ Execution Engine: as you can guess, it’s a parallelized version of Linq to Objects. It’s the same thing, except that it will run in parallel whenever possible.</li>
<li>Coordination Data Structures: it’s a set of primitives for sincronization and thread-safe collections. For example, we have thread-safe dictionaries, stacks and buffers, and special objects for thread sincronization, such as <em>SpinWait</em> or <em>SpinLock</em>.</li>
</ul>
<p>Now, the good part about these parallel extensions is that it gets extremely easy to do parallel programming. Just by using these new features, your code will escalate to use as many parallel threads as possible. For example, in a machine with just one processor all your code will execute sequentially, just as always. But, in an 8-core-processor computer, your code will be broken down into 8 different threads, and they all will execute your code in parallel –assuming all 8 cores are available, of course.  You can learn more <a href="http://msdn.microsoft.com/en-us/library/dd460693.aspx" target="_blank">this article</a> from MSDN.</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/' rel='bookmark' title='Permanent Link: Persistance layer: object-relational mapping tools'>Persistance layer: object-relational mapping tools</a></li>
<li><a href='http://www.undisciplinedbytes.com/2012/01/asp-net-4-5-and-visual-studio-11/' rel='bookmark' title='Permanent Link: ASP.NET 4.5 and Visual Studio 11'>ASP.NET 4.5 and Visual Studio 11</a></li>
<li><a href='http://www.undisciplinedbytes.com/2012/01/entity-framework/' rel='bookmark' title='Permanent Link: Entity Framework'>Entity Framework</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/09/parallel-programming-with-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test-driven development</title>
		<link>http://www.undisciplinedbytes.com/2011/07/test-driven-development/</link>
		<comments>http://www.undisciplinedbytes.com/2011/07/test-driven-development/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 09:09:20 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=575</guid>
		<description><![CDATA[Surprisingly, there’s many programmers who still don’t know what Test-driven development (TDD) is. I think it’s a very important practice that all of us should follow, since it produces software with many less bugs.
TDD is a programming methodology that involves two other practices: test first development, and refactoring. First a set of unit tests is [...]]]></description>
			<content:encoded><![CDATA[<p>Surprisingly, there’s many programmers who still don’t know what Test-driven development (TDD) is. I think it’s a very important practice that all of us should follow, since it produces software with many less bugs.</p>
<p>TDD is a programming methodology that involves two other practices: test first development, and refactoring. First a set of unit tests is written and verified to fail. Then, code to make them pass must be written, and lastly this code is refactored. The aim of TDD is to achieve clean code that works. The main idea around this methodology is that requirements must be translated into automated tests: thus, when these tests pass, all requirements are guaranteed to have been accomplished. The application to be developed must be flexible enough to allow automated tests to be run. Each test should be small enough to exactly determine which piece of code is failing.</p>
<p><span id="more-575"></span></p>
<p><strong>Test-driven development cycle</strong></p>
<p>Once a set of requirements has been defined, a new cycle starts:</p>
<ol>
<li>Choose a requirement: select a requirement you understand and about which you can write code both to test it and to implement it.</li>
<li>Write a test: the first code you must write is a test to determine whether the requirement is met, accessing the code through its interfaces. This step makes the programmer consider the problem from the client perspective.</li>
<li>Verify the test fails: if the test doesn’t fail it’s because this requirement is already implemented, or because the test is incorrect.</li>
<li>Write the implementation: Write the simplest code that will make the test pass. Follow the KISS philosophy (<em>Keep It Simple, Stupid!</em>).</li>
<li>Run the automated test: Verify whether the whole set of tests are passed.</li>
<li>Refactor: usually used to take out duplicate code. Each change should be made in small increments, and with each increment the tests should be re-run.</li>
<li>Update the requirements list: once the requirement is met mark it as done. Besides, add more requirements that may have been detected in this cycle or design requirements, like, for example, some functionality that has been decoupled from another one.</li>
</ol>
<p>Having a repository of tests makes another good practice easier: frequent integration. Integrating our work frequently with the rest of the team allows us to check whether our code is compatible with the rest of the application, and makes it easier to detect bugs in an early phase. It is much easier and cheaper to fix bugs every few hours than facing enormous problems close to a release date.</p>
<p>Generation of tests for each functionality means that programmers can trust the code. This means that we can make big modifications deep in the core of the system that we would otherwise avoid, since we can be certain that, as long as all the tests pass, the code works as intended and the requirements are still met.</p>
<p>Another thing to keep in mind is that programmers must make the unit tests fail. Unit tests must be tested. The idea is to be sure that unit tests really work, and that they’re able to pick up an error whenever it happens.</p>
<h4>Advantages</h4>
<p>Programmers that use test-driven development from the beginning of a project find that they seldom have the necessity to use a debugger. In the few occasions that a bug is discovered, it is very easy to fix.</p>
<p>Despite of the high initial requirements of this methodology, TDD may provide a great added value to the software development process, producing higher-quality applications in less time. TDD not only offers a simple requirements validation: it can also lead the design of a program. By writing unit tests, a programmer must imagine how customers will be using the functionality, which may avoid some misinterpretations or bad implementations of a functionality.</p>
<h4>Limitations</h4>
<p>In order to follow the test-driven development methodology, tests must be automated. This may turn out to be rather complex in the following situations:</p>
<ul>
<li>Graphical User Interfaces, although there are some partial solutions proposed.</li>
<li>Distributed Objects, even though Mock Objects may help.</li>
<li>Data bases: writing tests that work with data bases is quite complex, since you need to put some known data before, and you have to verify that the contents of the database is the expected once the test is done. This makes these kinds of tests costly to develop.</li>
</ul>
<p>Test-driven development is a programming methodology that should be in place in all organizations that develop software and consider themselves serious. Unfortunately, this is not the case. I hope things start to change in the near future!</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2010/07/improving-software-quality-automatic-testing/' rel='bookmark' title='Permanent Link: Improving software quality: automatic testing'>Improving software quality: automatic testing</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/10/gui-testing/' rel='bookmark' title='Permanent Link: GUI Testing'>GUI Testing</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/08/unit-testing/' rel='bookmark' title='Permanent Link: Unit testing'>Unit testing</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/07/test-driven-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Some open source projects for .NET you should take a look at</title>
		<link>http://www.undisciplinedbytes.com/2011/04/some-open-source-projects-for-net-you-should-take-a-look-at/</link>
		<comments>http://www.undisciplinedbytes.com/2011/04/some-open-source-projects-for-net-you-should-take-a-look-at/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 12:49:52 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=569</guid>
		<description><![CDATA[We all agree that ASP.NET and Visual Studio are amazing technologies with state-of-the-art tools. One requires almost nothing more to write any kind of software – except for a database. However, there are some tools that greatly simplify that task. It depends on what you’re trying to achieve, but I’m sure some of these projects [...]]]></description>
			<content:encoded><![CDATA[<p>We all agree that ASP.NET and Visual Studio are amazing technologies with state-of-the-art tools. One requires almost nothing more to write any kind of software – except for a database. However, there are some tools that greatly simplify that task. It depends on what you’re trying to achieve, but I’m sure some of these projects will help you get your tasks accomplished faster and easier:</p>
<ul>
<li><a href="http://nhforge.org/Default.aspx" target="_blank">NHibernate</a>: NHibernate is a mature, open source object-relational mapper for the .NET framework. It&#8217;s actively developed, fully featured and used in thousands of successful projects.</li>
<li><a href="http://www.nunit.org/index.php" target="_blank">NUnit</a>: NUnit is a unit-testing framework for all .Net languages, initially ported from  JUnit. It is written entirely in C# and has been completely redesigned to take advantage of many .NET language features, for example custom attributes and other reflection related capabilities. NUnit brings xUnit to all .NET languages</li>
<li><a href="http://jquery.com/" target="_blank">jQuery</a>: jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.</li>
<li><a href="http://ayende.com/projects/rhino-mocks.aspx" target="_blank">Rhino.Mocks</a>: A dynamic mock object framework for the .Net platform. It&#8217;s purpose is to ease testing by allowing the developer to create mock implementations of custom objects and verify the interactions using unit testing.</li>
<li><a href="http://www.codeplex.com/MVCContrib" target="_blank">MVC Contrib</a>: This is the contrib project for the ASP.NET MVC framework. This project adds additional functionality on top of the MVC Framework. These enhancements can increase your productivity using the MVC Framework. It is written in C#. Founded by Eric Hexter and Jeffrey Palermo.</li>
<li><a href="http://confluence.public.thoughtworks.org/display/CCNET/Welcome+to+CruiseControl.NET" target="_blank">CruiseControl.NET</a>: CruiseControl.NET is an Automated Continuous Integration server, implemented using the Microsoft .NET Framework.</li>
<li><a href="http://code.google.com/p/sharp-architecture/" target="_blank">S#arp Architecture</a>: Pronounced &#8220;Sharp Architecture,&#8221; this is a solid architectural foundation for rapidly building maintainable web applications leveraging the ASP.NET MVC framework with NHibernate. The primary advantage to be sought in using any architectural framework is to decrease the code one has to write while increasing the quality of the end product. A framework should enable developers to spend little time on infrastructure details while allowing them to focus their attentions on the domain and user experience.</li>
<li><a href="http://sparkviewengine.com/" target="_blank">Spark View Engine</a>: <strong></strong>Spark is a view engine for Asp.Net Mvc and Castle Project MonoRail frameworks. The idea is to allow the html to dominate the flow and the code to fit seamlessly.</li>
<li><a href="http://tortoisesvn.tigris.org/" target="_blank">TortoiseSVN</a>: <strong></strong>A Subversion client, implemented as a windows shell extension. TortoiseSVN is a really easy to use Revision control / version control / source control software for Windows. Since it&#8217;s not an integration for a specific IDE you can use it with whatever development tools you like.</li>
<li><a href="http://www.castleproject.org/container/index.html" target="_blank">Castle Windsor</a>: Castle Project offers two Inversion of Control Containers. The MicroKernel and the Windsor Container. Castle Windsor aggregates the MicroKernel  and exposes a powerful configuration support. It is suitable for common enterprise application needs. It is able to register facilities and components based on the configuration and adds support for interceptors.</li>
</ul>
<p>Take a look at these projects. I&#8217;m sure you’ll be able to find some good use for them in your projects!</p>
<p>Via <a href="http://dotnet.dzone.com/articles/t0p-10-open-source-projects" target="_blank">.NET Zone</a>.</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2009/10/open-source-software/' rel='bookmark' title='Permanent Link: Open Source Software'>Open Source Software</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/08/unit-testing/' rel='bookmark' title='Permanent Link: Unit testing'>Unit testing</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/' rel='bookmark' title='Permanent Link: Persistance layer: object-relational mapping tools'>Persistance layer: object-relational mapping tools</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/07/improving-software-quality-automatic-testing/' rel='bookmark' title='Permanent Link: Improving software quality: automatic testing'>Improving software quality: automatic testing</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/09/parallel-programming-with-net/' rel='bookmark' title='Permanent Link: Parallel programming with .NET'>Parallel programming with .NET</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/04/some-open-source-projects-for-net-you-should-take-a-look-at/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Which database to use for a personal project?</title>
		<link>http://www.undisciplinedbytes.com/2011/03/which-database-to-use-for-a-personal-project/</link>
		<comments>http://www.undisciplinedbytes.com/2011/03/which-database-to-use-for-a-personal-project/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 14:03:16 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[backend]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=458</guid>
		<description><![CDATA[When starting a new project, the choice of which database to use usually boils down to the one the company usually uses. The systems that are usually considered are just Oracle and Microsoft’s SQL server (MS-SQL). There’s no doubt that big bucks companies have the resources to make the best-of-its-breed products, and almost no other [...]]]></description>
			<content:encoded><![CDATA[<p>When starting a new project, the choice of which database to use usually boils down to the one the company usually uses. The systems that are usually considered are just Oracle and Microsoft’s SQL server (MS-SQL). There’s no doubt that big bucks companies have the resources to make the best-of-its-breed products, and almost no other database system comes close to what Oracle and MS-SQL can offer in terms of quality, features, and highly skilled professionals around their products. Yes, all this is true, but there’s a price. And usually this price tag can only be afforded by a company, not by a sole individual.</p>
<p>So, what if I want to develop my own personal project and I require a RDBMS (<em>Relational DataBase Management System</em>)? Oracle and MS-SQL are out of the question because of their high cost. MS-SQL Express has great limitations, so, although it is free, we won’t be considering it for our purpose. Which is the best open source RDBMS?</p>
<p><span id="more-458"></span></p>
<p>Today’s most known open source RDBMS are MySQL and PostgreSQL. PostgreSQL is considered to beat MySQL in the features war. This used to be true some time ago, but it’s no longer the case anymore. 99% of the needs you will have with your RDBMS will be covered by both MySQL and PostgreSQL. Still, there’s many people who think that MySQL has improved a lot, but it’s just not PostgreSQL.</p>
<p>If you want to compare performance, fans of both systems will tell you their RDBMS is faster than the other one. On one hand, PostgreSQL has been recently paying attention to speed, and has been making its product faster and faster with every release. On the other hand, MySQL proponents say that, if configured properly, MySQL’s MyISAM tables are indeed lightweight and make for a faster database.</p>
<p>But probably, the most important thing to consider when choosing which RDBMS you should use, is which one is easier to use. I can tell you that previous Oracle experience will make you feel at home and comfortable with PostgreSQL. Still, the MySQL community seems to be much bigger than PostgreSQL’s, so, should you require some assistance when developing your project, if you’re using MySQL chances are that you will get quick help from them.</p>
<p>MySQL is licensed under the GNU GPL, but offers an alternative commercial license for those who don’t want to be restricted by that license. PostgreSQL uses the BSD license, which stipulates that the credits have to be maintained, but beyond that you can do whatever you want with it.</p>
<p>But, after all, it just all comes down to personal tastes. Just choose whatever you like and whichever you think might fit your needs better. What might be the best for one person, might be a bad choice for another.</p>
<p>Check these links for more info:</p>
<ul>
<li><a href="http://www.devx.com/dbzone/Article/29480" target="_blank">Open Source Database Feature Comparison Matrix</a></li>
<li><a href="http://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems" target="_blank">Comparison of RDBMS</a></li>
</ul>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2010/01/sluggish-database-view-materialize-it/' rel='bookmark' title='Permanent Link: Sluggish database view? Materialize it!'>Sluggish database view? Materialize it!</a></li>
<li><a href='http://www.undisciplinedbytes.com/2012/01/entity-framework/' rel='bookmark' title='Permanent Link: Entity Framework'>Entity Framework</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/12/persistence-layer-object-relational-mapping-tools/' rel='bookmark' title='Permanent Link: Persistance layer: object-relational mapping tools'>Persistance layer: object-relational mapping tools</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/03/which-database-to-use-for-a-personal-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript best practices II</title>
		<link>http://www.undisciplinedbytes.com/2011/02/javascript-best-practices-ii/</link>
		<comments>http://www.undisciplinedbytes.com/2011/02/javascript-best-practices-ii/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 10:35:08 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[browser]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=552</guid>
		<description><![CDATA[We’ve seen some basic recommendations we should all follow when writing  JavaScript. Now let’s go through some more:


Comment as much as needed, but no more: comment what you consider needed,  but don’t tell others your life story. Too many comments may make some other  developer get distracted from the actual code.
Javascript is [...]]]></description>
			<content:encoded><![CDATA[<p>We’ve seen some basic recommendations we should all follow when writing  JavaScript. Now let’s go through some more:</p>
<p><span id="more-552"></span></p>
<ul>
<li>Comment as much as needed, but no more: comment what you consider needed,  but don’t tell others your life story. Too many comments may make some other  developer get distracted from the actual code.</li>
<li>Javascript is used to define the behaviour, not the look &amp; feel: even  though there’s the possibility to set the appearance of your application through  JavaScript, you should leave that for CSS. Always follow the “separation of  concerns” mantra. A change in the icons or the color of borders shouldn’t  require dealing with JavaScript code.</li>
<li>Modularize: keep your code modularized and specialized. Make sure to write  smaller, generic helper functions that fulfill one specific task, rather than  catch-all methods.</li>
<li>Allow for configuration and translation: everything that is likely to change  in your code should not be scattered throughout the code, for example, labels,  CSS classes and IDs. By putting these into a configuration object and making it  public, we make maintenance very easy and allow for customization.</li>
<li>Access the DOM as little as possible: accessing the DOM in browsers is an  expensive thing to do. The DOM is a very complex API and rendering in browsers  can take up a lot of time. To make sure that your code is fast and doesn’t slow  down the browser to a halt try to keep DOM access to a bare minimum.</li>
<li>Build on the shoulders of giants: JavaScript libraries are specifically  built to make browsers behave as you would expect by plugging browser holes.  Therefore, if you want to write code that works without keeping the maintenance  overhead of supporting current browsers and those to come, start with a good  library.</li>
<li>Development code is not live code: live code is done for machines,  development code is done for humans. Make your code as readable and  understandable as possible. Then, before deploying your web application,  collate, minify and optimize your code in a build process.</li>
</ul>
<p>If you follow all these recommendations, you can be sure you’ll have produced  a high quality code that will be easy to maintain.</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2011/01/javascript-best-practices-i/' rel='bookmark' title='Permanent Link: JavaScript best practices I'>JavaScript best practices I</a></li>
<li><a href='http://www.undisciplinedbytes.com/2009/10/javascript-frameworks-overview/' rel='bookmark' title='Permanent Link: Javascript frameworks overview'>Javascript frameworks overview</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-i-the-basics/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript I: the basics'>Object Oriented JavaScript I: the basics</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/02/javascript-best-practices-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript best practices I</title>
		<link>http://www.undisciplinedbytes.com/2011/01/javascript-best-practices-i/</link>
		<comments>http://www.undisciplinedbytes.com/2011/01/javascript-best-practices-i/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 12:43:41 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[browser]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/2011/01/javascript-best-practices-i/</guid>
		<description><![CDATA[Now that we’ve seen the basic JavaScript concepts, we’ll go through some recommendations to make a good code. These are some practices that should be followed if you want to make a readable and maintainable code. It is based on my knowledge and experience of JavaScript over several years of practical experience:


Always use ‘var’: although [...]]]></description>
			<content:encoded><![CDATA[<p>Now that we’ve seen the basic JavaScript concepts, we’ll go through some recommendations to make a good code. These are some practices that should be followed if you want to make a readable and maintainable code. It is based on my knowledge and experience of JavaScript over several years of practical experience:</p>
<p><span id="more-548"></span></p>
<ul>
<li>Always use ‘var’: although it is not compulsory, the use of the ‘var’ keyword is always recommended. By using it, your code will execute faster, and you will avoid some potential problems.</li>
<li>Feature-detect rather than browser-detect: before using any advanced feature that an older browser may not support, check to see if the function or property exists first, instead of writing code to detect browser versions.</li>
<li>Avoid ‘eval’: the eval() function in JavaScript allows a programmer to run arbitrary code at run-time, by interpreting a string as if it was code. If it exists in your page, there is almost always a more correct way to accomplish what you are doing. “Eval is evil.&#8221; Don&#8217;t use it unless you are an experienced developer and know that your case is an exception.</li>
<li>Use unobstrusive JavaScript: now, this is an important one: HTML and JavaScript code should not be mixed together. You should always follow the “separation of concerns” mantra: HTML defines the structure and content of a web page, and JavaScript defines the behavior. Therefore, do not use inline event handlers. Attach your event-handling code by JavaScript. This way, you’ll improve readability and maintainability.</li>
<li>Avoid cluttering the global namespace: global variables and functions are rarely required. Using globals may cause naming conflicts between JavaScript source files and cause code to break. You should always use object oriented programming and write all your code in classes when writing JavaScript code.</li>
<li>Use JSON: when storing data structures as plain text or sending/retrieving data structures via Ajax, use JSON instead of XML when possible. JSON (JavaScript Object Notation) is a more compact and efficient data format, and is language-neutral.</li>
<li>Make you code understandable: choose easy to understand and short names for variables and functions. Think of your code as a story – a story that should be easy to read and understand, the names you choose shouldn’t make it more complicated.</li>
</ul>
<p>In my next post I’ll discuss some more recommendations. Stay tuned!</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2011/02/javascript-best-practices-ii/' rel='bookmark' title='Permanent Link: JavaScript best practices II'>JavaScript best practices II</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/01/object-oriented-javascript-iii-some-tips/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript III: some tips'>Object Oriented JavaScript III: some tips</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-i-the-basics/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript I: the basics'>Object Oriented JavaScript I: the basics</a></li>
<li><a href='http://www.undisciplinedbytes.com/2009/10/javascript-frameworks-overview/' rel='bookmark' title='Permanent Link: Javascript frameworks overview'>Javascript frameworks overview</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-ii-the-pattern/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript II: the pattern'>Object Oriented JavaScript II: the pattern</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/01/javascript-best-practices-i/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Object Oriented JavaScript III: some tips</title>
		<link>http://www.undisciplinedbytes.com/2011/01/object-oriented-javascript-iii-some-tips/</link>
		<comments>http://www.undisciplinedbytes.com/2011/01/object-oriented-javascript-iii-some-tips/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 12:47:38 +0000</pubDate>
		<dc:creator>Oliver Mezquita</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.undisciplinedbytes.com/?p=531</guid>
		<description><![CDATA[We’ve already gone through the basic JavaScript concepts we need to write object oriented code, and we’ve already seen the basic pattern. But here’s a couple of tips that has proven useful many times when writing long and complex JavaScript code.
The curious case of the mutating scope
Ok, now imagine we have some fancy widget in [...]]]></description>
			<content:encoded><![CDATA[<p>We’ve already gone through the basic JavaScript concepts we need to write object oriented code, and we’ve already seen the basic pattern. But here’s a couple of tips that has proven useful many times when writing long and complex JavaScript code.</p>
<h4>The curious case of the mutating scope</h4>
<p>Ok, now imagine we have some fancy widget in our class and we want to interact with it. There are many widgets which redefine the <code>this</code> keyword to its instance instead of ours. For this case, we might want to be able to access our instance instead of the widget instance. We’ll need to redefine the scope in which the function is being executed. To do so, we can use the following function:</p>
<pre class="brush:javascript">// Creates a new function and returns a reference. This new function has its scope set to 'newScope'
this.setScope = function (originalFn, newScope) {
   return function () { return originalFn.apply(newScope, arguments); };
};</pre>
<p>We could just use the apply method on our original function, but I prefer this way since its more comfortable to work with parameters like this.</p>
<p>Be careful when using this, since this is a double-edged sword. By accessing our class scope we have all our data available. But we won’t have the widget’s scope available anymore. So make sure you don’t need to access the widget’s scope before changing the scope of the event handler.</p>
<p><span id="more-531"></span></p>
<h4>Accessing your instance from the global scope</h4>
<p>When you do something like</p>
<p><code>var Peter = new Person();</code></p>
<p>you&#8217;re creating a new instance of the class <code>Person</code>. And the variable <code>Peter</code> is just a pointer to the instance, not the instance itself. <code>Peter</code> is the way we have to access that instance from global scope: it’s just a pointer. But we have no way to know the name the pointer to the instance was given from within the instance itself. This means, we have no way to know that <code>Peter</code> is the way we have to access the instance from global scope.</p>
<p>So, if we’re ever in the need of calling a method from a global scope, we’ll need to keep an array of instances ourselves. We could be doing something like:</p>
<pre class="brush:javascript">var Person2 = function (name, surname, age) {
	var _name, _surname, _age;

	this.showData = function () {
		var str = new String();
		str = "Name: " + _name + " - ";
		str += "Surname: " + _surname + " - ";
		str += "Age: " + _age;
		alert(str);
	};	

	// ---- Constructor -----------------------------------------------
	_name = (typeof name === "undefined" ? "John" : name);
	_surname = (typeof surname === "undefined" ? "Smith" : surname);
	_age = (typeof age === "undefined" ? 20 : age);

	// Store pointers to all instances of this class, so we can access their
	// context from global code
	var _instanceIndex = Person2._instances.length;
	Person2._instances[_instanceIndex] = this;
};
Person2._instances = new Array();</pre>
<p>So now we have an array containing all the different instances of the object:</p>
<pre class="brush:javascript">var Peter = new Person2();  // Equivalent to Person2._instances[0]
var John = new Person2('John', 'Smith');  // Equivalent to Person2._instances[1]</pre>
<p>This is useful if you’re declaring some event handlers in the HTML code. Something like:</p>
<pre>&lt;div id="myDiv" onclick="Person2._instances[1].showData();"&gt;Hello world!&lt;/div&gt;</pre>
<p><strong>CAUTION!! This is not something I would recommend. This is not a good practice in JavaScript. Behaviour should be separated from the content, and with this you’re just mixing both together.</strong></p>
<p>I’m just showing this as a possibility, in case you’re in the need of something similar. But be aware that by using <code>onclick</code> event handlers like this, you’re making your code less readable and less maintainable. <strong>Be careful!</strong></p>
<p>Well, now you have to start practicing all these concepts you’ve just read. Start doing some JavaScript programming. The more you do it, the better programmer you’ll become.</p>


<h4>Related posts:</h4><p><ol><li><a href='http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-ii-the-pattern/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript II: the pattern'>Object Oriented JavaScript II: the pattern</a></li>
<li><a href='http://www.undisciplinedbytes.com/2010/12/object-oriented-javascript-i-the-basics/' rel='bookmark' title='Permanent Link: Object Oriented JavaScript I: the basics'>Object Oriented JavaScript I: the basics</a></li>
<li><a href='http://www.undisciplinedbytes.com/2011/01/javascript-best-practices-i/' rel='bookmark' title='Permanent Link: JavaScript best practices I'>JavaScript best practices I</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.undisciplinedbytes.com/2011/01/object-oriented-javascript-iii-some-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

