tag:blogger.com,1999:blog-54610167785390276972024-02-21T01:07:07.249+00:00Movable #Brinquedos, Engenhocas e SoftwareNuno Lopeshttp://www.blogger.com/profile/00141863230937433912noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-5461016778539027697.post-44349244138843671022010-01-15T18:01:00.006+00:002010-01-19T14:55:41.093+00:00O meu Avatar em Silver Light: Ajax Bolt<div style="text-align: left;">Quem quiser me convidar para umas jogatanas na XBOX 360 fica aqui o meu Gamecard em Silverlight. Experimentem interagir com a imagem abaixo ....<br />
</div><br />
<div style="text-align: center;"><div style="text-align: left;"><object data="data:application/x-silverlight-2," height="135" type="application/x-silverlight-2" width="300"> <param name="source" value="http://card.mygamercard.net/default.xap" /><param name="enableHtmlAccess" value="true" /><param name="enableGPUAcceleration" value="true" /><param name="minRuntimeVersion" value="3.0.40624.0" /><param name="autoUpgrade" value="true" /><param name="initParams" value="gamertag=Ajax Bolt,language=" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none" /> </a> </object><br />
</div></div>Nuno Lopeshttp://www.blogger.com/profile/00141863230937433912noreply@blogger.com0tag:blogger.com,1999:blog-5461016778539027697.post-43563926643109988522010-01-15T16:02:00.004+00:002010-01-15T16:04:58.247+00:00Adesão massiva aos partidos políticosPara todos aqueles que não se revêem em partidos políticos uma amigo meu, João Nogueira, apresenta um desafio que pode ser interessante. Se não vejamos:<br />
<blockquote>Queremos despertar a geração mais educada que o nosso país alguma vez teve, nós todos, para urgência de tomarmos em mãos a responsabilidade de elevar a qualidade dos nossos partidos, aderindo em massa a estes, e com a nossa participação e voto, iniciar uma mudança radical na qualidade dos partidos, da nossa democracia e da nossa governação. Se não o fizermos, esta geração arrisca-se a entregar aos seus filhos, um país sem esperança, sem oportunidades e provavelmente um dos mais pobres da Europa.<br />
</blockquote>Fonte: <a href="http://www.facebook.com/group.php?gid=246936858817&ref=nf">Facebook</a><br />
<br />
Parece-me interessante.<br />
<br />
Stay cool.Nuno Lopeshttp://www.blogger.com/profile/00141863230937433912noreply@blogger.com0tag:blogger.com,1999:blog-5461016778539027697.post-28181354542274845032010-01-15T15:33:00.003+00:002010-01-19T12:20:15.148+00:00This Blog is MutatingHello, <br />
<br />
When I decided to create this blog I was bit too ambitious. <br />
<br />
The fact is that I had little to no experience in creating Blogs, and no sense of how much time is needed to keep it interesting. <br />
<br />
Unfortunately time is a scarse resource. Furthermore I was never very good in writing down my thoughts so I should have known better. I can't remember how many diaries I left filled with blank pages.<br />
<br />
So I'm changing the style. <br />
<br />
First my posts will be quite short with the understanding that I will be a compromise between valuable content and sintetizes.<br />
<br />
Second it will no longer be only about software development but also my own tastes in tech and culture.<br />
<br />
Third, the posts will be by default written in my native language, Portuguese. There will be posts in English for sure but only when I feel that the concept is so new and interesting that deserves global exposure.<br />
<br />
Thank you all for the support, check back soon.<br />
<br />
Stay cool,<br />
Nuno LopesNuno Lopeshttp://www.blogger.com/profile/00141863230937433912noreply@blogger.com0tag:blogger.com,1999:blog-5461016778539027697.post-64586597156067328892009-02-06T15:37:00.002+00:002010-01-15T15:37:02.910+00:00Inversion of Control Made SimpleInversion of control is an architectural technique where instead of directly calling a function, you hand this action to something else.<br />
Sounds confusing? <br />
<br />
<strong>A simple example:</strong><br />
<div class="csharpcode"><pre class="alteven">1: List<int> l = new List<int>();</pre><pre class="alteven">2: l.Add(5);</pre><pre class="alteven">3: l.Add(30);</pre><pre class="alteven">4: l.Add(3);</pre><pre class="alteven">5: l.Add(41);</pre><pre class="alteven">6: l.Sort(delegate(int n1, int n2) { return n1.CompareTo(n2); });</pre></div><br />
<br />
The function Sort accepts another function for a parameter. This function is used by the sort algorithm to find which of two numbers is smaller and is implemented explicitly in the code above.<br />
Nevertheless the code above has no idea of how the sorting algorithm is implemented and as such when the function will be called. In other words the code above has no control about when and by whom the function will be called.<br />
This is in contrast with the Add method that is used to populate the list with values. The programmer explicitly controls when the values are added to the list.<br />
<strong>Another example:</strong><br />
Most of us already have coded a simple Web Page with ASP.NET. All pages in ASP.NET inherit from the System.Web.UI.Page class. <br />
We are used to overload methods such as OnInit and OnPrerender or attach delegates to events. Nothing calls these functions but the ASP.NET framework, so we have no control over when and how will be it will be done.<br />
Indeed, the main difference between a class library and a framework is that the second used IoC right into its heart. While the first is usually a collection of classes (Web Control Library for instance) that may or be part of a framework.<br />
If you haven’t done already you should read what <a href="http://martinfowler.com/bliki/InversionOfControl.html">Martin Fowler</a> says about it.<br />
Stay cool.Nuno Lopeshttp://www.blogger.com/profile/00141863230937433912noreply@blogger.com0tag:blogger.com,1999:blog-5461016778539027697.post-1215740286200593062009-02-02T19:12:00.002+00:002010-01-15T15:37:39.468+00:00#1 Principle of Object Modeling & Design of Domain Models<strong>In OO if you want to execute an action over an object you don’t explicitly execute that action, but you ask an object to execute that action for you. This in contrast with procedural languages.</strong><br />
Well it may not be number one, but I see so many people confused about where to put a business method when building a Domain Model that this post is just a reminder of basic OO principles. <br />
Say we have the following business activity:<br />
<em>“Pay an Order by Credit Card”</em><br />
Seams easy. So the programmer start coding immediately in C# the activity using practices that I see so often (I’m sure you do to).<br />
<br />
Example of a procedure implementing this activity using non OO methods:<br />
<div><div class="csharpcode"><pre class="alteven">1: public void PayOrder(OrderData aOrder, CreditCardData aCreditCard)</pre><pre class="alteven">2: {</pre><pre class="alteven">3: try</pre><pre class="alteven">4: {</pre><pre class="alteven">5: // start transaction</pre><pre class="alteven">6: double ammount = this.CalculateOrderAmmount();</pre><pre class="alteven">7: PaymentData aPaymentData = this.MakePayment(ammout, creditCardData);</pre><pre class="alteven">8: // commit transaction</pre><pre class="alteven">9: }</pre><pre class="alteven">10: catch (Exception ex);</pre><pre class="alteven">11: {</pre><pre class="alteven">12: // perform clean up</pre><pre class="alteven">13: }</pre><pre class="alteven">14: }</pre></div></div>The problem here is that you have no objects (OO way) apart from the current object (this), that might be a singleton class exposing business services to an upper layer. Let’s say it is the <strong>OrderBC</strong>. BC from business component. <br />
Both <strong>orderData, </strong><strong>creditCardData</strong> and PaymentData are just data structures that hold data (a class with properties only and little to now behavior is nothing but a structure like a record was in <strong>Pascal</strong>. We now call them DTO’s). Some of us implement this data structures using DataTables other using structured Classes. Furthermore business rules for payment are entirely coded both in the function <strong>CalculateOrderAmmount</strong> and the procedure <strong>Pay</strong>.<br />
Now take for instance the OO version of this:<br />
<div><div class="csharpcode"><pre class="alteven">1: public void PayOrder(Order aOrder, CreditCard aCreditCard)</pre><pre class="alteven">2: {</pre><pre class="alteven">3: try</pre><pre class="alteven">4: {</pre><pre class="alteven">5: // start transaction</pre><pre class="alteven">6: aOrder.MakePayment(aCreditCard);</pre><pre class="alteven">7: // commit transaction</pre><pre class="alteven">8: }</pre><pre class="alteven">9: catch (Exception ex);</pre><pre class="alteven">10: {</pre><pre class="alteven">11: // perform clean up</pre><pre class="alteven">12: }</pre><pre class="alteven">13: }</pre></div></div>So here we have two first class objects, aOrder and aCreditCard. Remember the definition of this activity?<br />
<em>“Pay an Order by Credit Card”</em><br />
So instead of having functions calling functions or procedures of some mega component we ask the Order to make the Payment.<br />
I understand that this principle has been forgotten by many not because of them, but because of how we have been evolving around enterprise architectures built to cope with data access and storage in relational databases. Not only that, but with external services also.<br />
But now you lucky ones that are using Object Relational Mappers and can apply OO principles do design the Business Domain are reminded.<br />
Stay cool.Nuno Lopeshttp://www.blogger.com/profile/00141863230937433912noreply@blogger.com0tag:blogger.com,1999:blog-5461016778539027697.post-88810039477882990852009-01-29T19:59:00.002+00:002010-01-15T15:37:46.265+00:00Dynamic Queries using LINQ – Part 1A Dynamic SQL Query is one built at runtime. This is often used to generalize a simple query to cope with parameters that change the way data is filtered and ordered while some rules are fixed.<br />
Take for instance this method:<br />
<div class="csharpcode"><pre class="alteven">1: public IList<Entry> Load(long blogId, int lastN, string sortBy, string sortOrder)</pre></div>It returns a list of entries in a Blog where:<br />
<ul><li>lastN – list the last N entries in the Blog. If 0, then return all values. </li>
<li>sortBy – sorts the entries by some field. If null return values unsorted. </li>
<li>sortOrder – sets the order by which the list returned is sorted (ascending/descending) </li>
<li>None of these arguments are mandatory. That is one can simply want a list of the last 10 entries without any kind of sorting or order. </li>
</ul>Clearly a static query cannot cope with this, so we need to dynamically generate one.<br />
<br />
With LINQ we can do the following:<br />
<div class="csharpcode"><pre class="alteven">1: public IList<Entry> Load(long blogId, int lastN, string sortBy, string sortOrder)</pre><pre class="alteven">2: {</pre><pre class="alteven">3: </pre><pre class="alteven">4: var q = from entry in OB.Entities<Entry>() where entry.Blog.ID == blogId select entry;</pre><pre class="alteven">5: </pre><pre class="alteven">6: if (!StringUtils.IsNullOrEmpty(sortBy))</pre><pre class="alteven">7: {</pre><pre class="alteven">8: switch (sortBy)</pre><pre class="alteven">9: {</pre><pre class="alteven">10: case "Date":</pre><pre class="alteven">11: if (StringUtils.IsNullOrEmpty(sortOrder))</pre><pre class="alteven">12: {</pre><pre class="alteven">13: q = q.OrderBy(entry => entry.Date); break;</pre><pre class="alteven">14: }</pre><pre class="alteven">15: else</pre><pre class="alteven">16: {</pre><pre class="alteven">17: switch (sortOrder)</pre><pre class="alteven">18: {</pre><pre class="alteven">19: case "asc": q = q.OrderBy(entry => entry.Date); break;</pre><pre class="alteven">20: case "desc": q = q.OrderByDescending(entry => entry.Date); break;</pre><pre class="alteven">21: }</pre><pre class="alteven">22: }</pre><pre class="alteven">23: break;</pre><pre class="alteven">24: // more cases ...</pre><pre class="alteven">25: }</pre><pre class="alteven">26: }</pre><pre class="alteven">27: </pre><pre class="alteven">28: if (lastN >= 0) q = q.Take(lastN);</pre><pre class="alteven">29: </pre><pre class="alteven">30: return OB.Execute<IList<Entry>>(() =></pre><pre class="alteven">31: {</pre><pre class="alteven">32: return q.ToList();</pre><pre class="alteven">33: });</pre><pre class="alteven">34: }</pre></div>Bellow is SQL generated by NHibernate when we call it with: blogId=1, sorBy=null and sortOrder=null.<br />
<div><div class="csharpcode"><pre class="alteven">1: SELECT top 10 this_._ID as column1_1_1_,</pre><pre class="alteven">2: this_._Title as column2_1_1_,</pre><pre class="alteven">3: this_._Body as column3_1_1_,</pre><pre class="alteven">4: this_._PostDate as column4_1_1_,</pre><pre class="alteven">5: this_._PublicationDate as column5_1_1_,</pre><pre class="alteven">6: this_._BlogID as column6_1_1_,</pre><pre class="alteven">7: blog1_._ID as column1_0_0_,</pre><pre class="alteven">8: blog1_._Name as column2_0_0_,</pre><pre class="alteven">9: blog1_._Owner as column3_0_0_,</pre><pre class="alteven">10: blog1_._PostDate as column4_0_0_</pre><pre class="alteven">11: FROM BlogPost this_</pre><pre class="alteven">12: left outer join Blog blog1_</pre><pre class="alteven">13: on this_._BlogID = blog1_._ID</pre><pre class="alteven">14: WHERE blog1_._ID = 1 /* @p0 */</pre></div></div>Naturally ORDER BY SQL keyword is not included in the query, neither is the sort order. <br />
Bellow is SQL generated by NHibernate when we call it with: blogId=1, sorBy=”Date” and sortOrder=“asc”.<br />
<div><div class="csharpcode"><pre class="alteven">1: SELECT top 10 this_._ID as column1_1_1_,</pre><pre class="alteven">2: this_._Title as column2_1_1_,</pre><pre class="alteven">3: this_._Body as column3_1_1_,</pre><pre class="alteven">4: this_._PostDate as column4_1_1_,</pre><pre class="alteven">5: this_._PublicationDate as column5_1_1_,</pre><pre class="alteven">6: this_._BlogID as column6_1_1_,</pre><pre class="alteven">7: blog1_._ID as column1_0_0_,</pre><pre class="alteven">8: blog1_._Name as column2_0_0_,</pre><pre class="alteven">9: blog1_._Owner as column3_0_0_,</pre><pre class="alteven">10: blog1_._PostDate as column4_0_0_</pre><pre class="alteven">11: FROM BlogPost this_</pre><pre class="alteven">12: left outer join Blog blog1_</pre><pre class="alteven">13: on this_._BlogID = blog1_._ID</pre><pre class="alteven">14: WHERE blog1_._ID = 1 /* @p0 */</pre><pre class="alteven">15: ORDER BY this_._PostDate asc</pre></div></div>Now the ORDER BY and the sort order keywords are there.<br />
Next time I’ll explore ways build dynamically the where clause. Until then …<br />
Stay cool.Nuno Lopeshttp://www.blogger.com/profile/00141863230937433912noreply@blogger.com0