<?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>The Inquisitive Coder - Davy Brion&#039;s Blog &#187; Architecture</title> <atom:link href="http://davybrion.com/blog/category/architecture/feed/" rel="self" type="application/rss+xml" /><link>http://davybrion.com/blog</link> <description>inquisitive: adjective. given to inquiry, research, or asking questions; eager for knowledge; intellectually curious</description> <lastBuildDate>Mon, 14 May 2012 21:08:36 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>What&#8217;s The Point Of Using WCF In A Web App?</title><link>http://davybrion.com/blog/2012/03/whats-the-point-of-using-wcf-in-a-web-app/</link> <comments>http://davybrion.com/blog/2012/03/whats-the-point-of-using-wcf-in-a-web-app/#comments</comments> <pubDate>Sun, 18 Mar 2012 16:28:28 +0000</pubDate> <dc:creator>Davy Brion</dc:creator> <category><![CDATA[Architecture]]></category> <category><![CDATA[Code Quality]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[WCF]]></category><guid
isPermaLink="false">http://davybrion.com/blog/?p=3938</guid> <description><![CDATA[A very common approach of building web applications in .NET is to put most of the non-UI related code behind an internal WCF service layer. I used to be a fan of this approach as well, but these days I just don't see the benefit of that internal service layer anymore. The overhead that an [...]]]></description> <content:encoded><![CDATA[<p>A very common approach of building web applications in .NET is to put most of the non-UI related code behind an internal WCF service layer. I used to be a fan of this approach as well, but these days I just don't see the benefit of that internal service layer anymore. The overhead that an internal WCF service layer adds to development, deployment and runtime performance just doesn't stack up favorably to the supposed benefits IMO. To be clear: I'm talking about WCF services that will <em>only</em> be used by the front end of your web application.</p><p>Let's talk about the overhead on development first. If you're using WCF services in your web app, you need proxies to access those services. Some people prefer to generate the proxies based on the WSDL of the services that will be used. In the worst case, this leads to regenerating proxies and all of the types that are defined in the WSDL every time you change a service contract or one of the types that are used by the services. If multiple people need to make changes to any of these concurrently, this easily leads to merging problems when people need to commit their changes. Another way is to share the same types on both sides (client &amp; server), and implement your service proxies by inheriting from ClientBase and manually keeping the implementation of the proxies up to date with the definitions of their service contracts. This is better than regenerating a bunch of code all the time, but you're still writing a lot of redirection code for the purpose of, well, what exactly? Another possibility is to use dynamic proxies which automatically implement the service contracts but this increases the amount of infrastructure code you need to put in place and it's not always clear to everyone how exactly communication with the services happens. There's also a lot of WCF configuration for each service that you need to maintain, and it can quickly grow unwieldy.</p><p>Then there's the overhead on performance. I hope we can all agree that any operation that goes out of process is at least an order of magnitude slower than a similar operation that can be executed in process. First of all, there's the networking overhead (even if your services are hosted on the same machine as the web app) that you have to keep into account. Secondly, there is the cost of serializing and deserializing <em>everything</em> that is transferred between the client and the server. Even with the most efficient bindings and serializers, the cost of all of this quickly adds up on high-traffic web apps. That's not to say that WCF services are inherently slow. They can be very fast and efficient, but they'll never be as fast and efficient as executing that logic in process within the web app.</p><p>Finally, there's the extra overhead it introduces to the deployment phase:</p><ul><li>more endpoints to set up and transfer artifacts to</li><li>more configuration</li><li>more monitoring of endpoints</li><li>more servers if you're not hosting the services and the web app on the same machine</li></ul><p>Of course, people will argue that there a plenty of benefits to using a WCF service layer in a web app. The ones I hear about most often are the forced separation of business logic and UI logic and improved scalability and reliability. I really disagree that you need a physical separation of business and UI logic. I much prefer approaches where the separation is based on abstractions. A good example was recently posted by Ayende (<a
href="http://ayende.com/blog/154241/limit-your-abstractions-the-key-is-in-the-infrastructurehellip">here</a> and <a
href="http://ayende.com/blog/154273/limit-your-abstractions-and-how-do-you-handle-testing">here</a>). And when it comes to scalability/reliability, a web app that isn't dependent on a WCF service layer is as easy (or even easier depending on your setup) to scale than one that is entirely dependent on WCF services. First of all, if you care about scalability/reliability your web app should already be prepared to run behind a load balancer. If you already have a load balancer in place, you can just add more web servers to your setup when needed. If you'd host the WCF services on the same machines that are hosting the web front end, you'd get less total throughput from one server than you would if that one server could just host a web app that fully executes in process (not including the database obviously). If you're hosting the WCF services on separate machines, you'd end up with more servers to handle the load and to achieve the reliability you need than you would with just being able to add more web servers to your setup. That also increases your licensing costs. And of course, it also means increased networking overhead on every service call, which also implies that the threads on your web servers will be blocked for longer periods while they wait for those service calls to return. Unless you're calling those services asynchronously, but most people simply don't. Also, if you have serious scalability and reliability requirements you're probably better off with asynchronous messaging solutions than with SOAP services.</p><p>WCF has its benefits (though I prefer Web API's or asynchronous messaging over SOAP services these days) and it has its use cases. I just don't think internal service layers for web apps is one of them.</p><p>What are the benefits that you think an internal WCF service layer brings to your web app? And what's your opinion on how they stack up versus the downsides?</p><div
class="bottomcontainerBox" style=""><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <iframe
src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavybrion.com%2Fblog%2F2012%2F03%2Fwhats-the-point-of-using-wcf-in-a-web-app%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:85px; height:21px;"></iframe></div><div
style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <g:plusone size="medium" href="http://davybrion.com/blog/2012/03/whats-the-point-of-using-wcf-in-a-web-app/"></g:plusone></div><div
style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://davybrion.com/blog/2012/03/whats-the-point-of-using-wcf-in-a-web-app/"  data-text="What&#8217;s The Point Of Using WCF In A Web App?" data-count="horizontal" data-via="davybrion"></a></div><div
style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://davybrion.com/blog/2012/03/whats-the-point-of-using-wcf-in-a-web-app/" data-counter="right"></script></div><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://davybrion.com/blog/2012/03/whats-the-point-of-using-wcf-in-a-web-app/"></script></div></div><div
style="clear:both"></div><div
style="padding-bottom:4px;"></div>]]></content:encoded> <wfw:commentRss>http://davybrion.com/blog/2012/03/whats-the-point-of-using-wcf-in-a-web-app/feed/</wfw:commentRss> <slash:comments>38</slash:comments> </item> <item><title>Architectural Drivers</title><link>http://davybrion.com/blog/2012/02/architectural-drivers/</link> <comments>http://davybrion.com/blog/2012/02/architectural-drivers/#comments</comments> <pubDate>Sun, 26 Feb 2012 17:10:50 +0000</pubDate> <dc:creator>Davy Brion</dc:creator> <category><![CDATA[Architecture]]></category><guid
isPermaLink="false">http://davybrion.com/blog/?p=3912</guid> <description><![CDATA[There are many different opinions and preferences when it comes to how we should deal with software architecture. My personal preference is to try not to make things more complex than they should be, while striving to maintain enough flexibility to be able to deal with future changes in both functional and non-functional requirements in [...]]]></description> <content:encoded><![CDATA[<p>There are many different opinions and preferences when it comes to how we should deal with software architecture. My personal preference is to try not to make things more complex than they should be, while striving to maintain enough flexibility to be able to deal with future changes in both functional and non-functional requirements in a way that hopefully doesn't require extensive effort. I've always thought of good architecture essentially being something that enables developers to keep implementing new features over time at a similar cost than what it took to implement features in the early stages of development. Thus I prefer to try to find a balance between simplicity of the codebase, and making sure that the architecture scales nicely (from a code quality point of view) as the application grows in functionality. I also like to make sure that we can scale out (from a performance/throughput point of view) relatively easily if that need to scale comes up. These are just my general preferences though, and aren't always relevant to every project or suitable for every situation. I'm now in the 10th year of my career and have seen a variety of architectural styles in the wild, and of course I've read and heard about many more since the subject has always interested me a lot. I've learned that it's important to keep <em>architectural drivers</em> of a project in mind when thinking about what kind of architecture is suitable for a certain project or situation. Those drivers are often more important than whatever your personal preferences are. I've come up with a list of drivers that I think should influence the architecture used by teams or organizations.</p><h3>Simplicity/Complexity of the application</h3><p>A common mistake is to decide to use a fashionable architectural style in a project that doesn't really have enough complexity to warrant the use of said architectural style. For instance, there's no reason whatsoever to go the DDD route if your application is mostly a forms-over-data CRUD app. You won't really get any benefit from it, and you'll only increase the total cost of the required development effort. On the other hand, an application which will feature a lot of <em>behavior</em> in a complex domain will certainly benefit from using a DDD approach versus a data-driven approach that implements the behavior in a more procedural, transaction-script based approach. Be honest to your customer/employer and <em>yourself</em> and make an objective decision based on the simplicity or the complexity of the required solution that you're being paid to build. Don't let enthusiasm for a fashionable architectural style influence your decision.</p><h3>Expected lifetime of the application</h3><p>Some applications live for years and years, and some are only temporary solutions that will be phased out a year or two after having been put in production. Of course, some of those temporary solutions end up being used much longer than what was originally expected due to a variety of reasons but that certainly isn't always a given. Unless you're familiar enough with how things typically go at your client or your company to predict these things, your best bet is to just go with the initially expected lifetime of the application. Does a temporary solution require the best possible architecture out there, and the resulting impact that would have on the cost to build it? I'd say it most often doesn't. In these cases, there's nothing wrong with going for the approach that is simply good enough for what is required. Striving for perfection is expensive, and generally not worth it for short-term solutions. Conversely, if the solution is expected to last a long time, a solid architecture becomes more important and is worth the extra investment to (try to) get it right if that makes it possible to keep the costs of long-term development under control.</p><h3>Strategic importance of the application</h3><p>Some applications are meant to improve the <em>core business</em> of a company, while others play a more supportive role in something that isn't actually part of the company's core business. Some applications generate revenue (directly or by improving efficiency of revenue-generating activities) while others are meant to reduce costs, typically administrative in nature. Cost reduction is important, but it's not quite as important as revenue generation and that's a factor that should be taken under consideration. A strategically important application almost always warrants putting in the effort to come up with a good architecture because it's highly likely that the application will have to evolve in whichever direction the business evolves. That's not to say that the same kind of effort wouldn't be important for cost-reducing applications. But it just might be less important than you'd like to think it is. Again, good architecture increases the cost of the project, and the return on that investment in the big picture of the company should not be ignored.</p><h3>Skill-level/discipline of team</h3><p>Good code and good architecture requires skill and discipline. A strong team is capable of letting good architecture grow organically and keep it at a high quality level. For a mediocre team, letting it grow organically is too often a recipe for disaster. Ideally, we'd always be working with strong teams but as we all know, that simply isn't always possible. For mediocre teams, it often makes sense to put architecture in place that is more restrictive in nature and where everyone knows how the code should be structured in advance. The downside is that this typically reduces flexibility and individual creativity, and often introduces more ceremony and indirection than what many of us would ideally like to see. But it's likely that those downsides are offset by having everyone on the same page and taking some possibly difficult decisions away from people who might not be strong enough to make the right decisions. I know that sounds harsh, but that's a reality that many of us have to deal with.</p><h3>Conformity/Continuity</h3><p>A lot of IT departments or software development companies prefer to use the same architecture and frameworks/libraries for most (or even all) of their projects because it makes it easier to have people work on multiple projects. In this situation, it's easier to move people between projects or have them do maintenance work on a project they weren't initially involved in. While this often prevents going with the most suitable approach for every project, it does introduce a few benefits that are hard to argue with. People will need less time to get familiar with a code base. In-house training and sharing of knowledge get easier. Bringing in new people (especially for temporary assignments) also gets somewhat easier because you have a baseline of required skills/knowledge that should go a long way within the organization. A huge downside however, is that it creates an environment that will quickly frustrate creative developers. Also, when new strong developers are brought in, they could quickly tune out once they realize they got there too 'late' to have any influence on how projects are developed. The interesting part about this driver is that it is influenced by the skill-level-discipline driver, while simultaneously influencing it towards the future.</p><h3>Non-functional requirements</h3><p>This is the last driver on the list, but it is certainly not the least important. Obviously, architecture is greatly influenced by some important non-functional requirements. There are many factors that can have a profound influence on what the most suitable architecture of a system could be, and it's important to think about those as quickly as possible. Do you need to respond in real time? Do you need to support mobile devices? Will there be connectivity issues? Will you need to scale massively? What are the auditing requirements? Are you dependent on third-party services? Do you need to minimize resource consumption? What kind of accessibility do you have to keep into account? The list goes on and on and many of these issues can end up becoming huge problems if you don't think about them ahead of time. Of course, you can't think of everything in advance, and many non-functional requirements can be introduced during the lifetime of the application instead of being known in advance. But the more you know in advance, the better you can prepare your software for them from the beginning.</p><h3>Conclusion</h3><p>There is a virtually endless variety of architectural choices and styles that can influence how you develop software. And unfortunately, there's a lot of dogma surrounding it as well. I hope this post made it clear that there is no definitive 'right' or 'wrong' when it comes to architecture, and that (as pretty much everything else in this business) it all really depends <img
src='http://d18sni7re4ly7f.cloudfront.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p>I'm sure you can think of more architectural drivers, and I'd love to hear about any that you think I've forgotten.</p><div
class="bottomcontainerBox" style=""><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <iframe
src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavybrion.com%2Fblog%2F2012%2F02%2Farchitectural-drivers%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:85px; height:21px;"></iframe></div><div
style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <g:plusone size="medium" href="http://davybrion.com/blog/2012/02/architectural-drivers/"></g:plusone></div><div
style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://davybrion.com/blog/2012/02/architectural-drivers/"  data-text="Architectural Drivers" data-count="horizontal" data-via="davybrion"></a></div><div
style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://davybrion.com/blog/2012/02/architectural-drivers/" data-counter="right"></script></div><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://davybrion.com/blog/2012/02/architectural-drivers/"></script></div></div><div
style="clear:both"></div><div
style="padding-bottom:4px;"></div>]]></content:encoded> <wfw:commentRss>http://davybrion.com/blog/2012/02/architectural-drivers/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>DTO&#8217;s Should Transfer Data, Not Entities</title><link>http://davybrion.com/blog/2012/02/dtos-should-transfer-data-not-entities/</link> <comments>http://davybrion.com/blog/2012/02/dtos-should-transfer-data-not-entities/#comments</comments> <pubDate>Sun, 19 Feb 2012 19:32:17 +0000</pubDate> <dc:creator>Davy Brion</dc:creator> <category><![CDATA[Architecture]]></category> <category><![CDATA[Code Quality]]></category><guid
isPermaLink="false">http://davybrion.com/blog/?p=3907</guid> <description><![CDATA[I've read a couple of posts recently where the authors were complaining about excessive mapping to DTO's and whether or not it's worth it. I've been a fan of DTO's for a long time, but I'm not a fan of how I frequently see them being used. More specifically, I very much dislike the all-too-common [...]]]></description> <content:encoded><![CDATA[<p>I've read a couple of posts recently where the authors were complaining about excessive mapping to DTO's and whether or not it's worth it. I've been a fan of DTO's for a long time, but I'm not a fan of how I frequently see them being used. More specifically, I very much dislike the all-too-common approach of creating a DTO for each entity in your domain model. In the worst cases, the DTO's actually reference each other and try too hard to mimic the structure of the entities. For example, an OrderDto which has a reference to a CustomerDto and a collection of OrderLineDto instances. In those cases, I absolutely agree that all of the mapping involved introduces way too much ceremony to the codebase without really offering any concrete benefits.</p><p>Of course, it really depends on the architecture of your system.  If you're using services that are only used by the front-end of your system, then I'd still advise against making those entities available outside of the service boundary for reasons that I've discussed <a
href="http://davybrion.com/blog/2010/05/why-you-shouldnt-expose-your-entities-through-your-services/">earlier</a>. That doesn't mean that you should go the entity-mimicking-DTO-route though. In fact, entity-mimicking-DTO's introduce a few of the same downsides you'd get from exposing entities directly through  your services. For an 'internal' service (i.e. only used by <em>your</em> application), I think it makes a lot more sense to use DTO's per service operation which are optimized for the use case that that service operation is meant to implement.</p><p>Essentially, an internal service's operations will be either <em>queries</em> or <em>commands</em>. When it comes to queries, why not just do the simplest thing possible? In most cases, it's sufficient to just return the data in the exact same form that the data will be displayed in. Quite often, that means a denormalized set of data where the data comes from more than one type of entity/table. There's no reason to send a bunch of entity-mimicking DTO's that reference each other to the client if you're going to use the data to populate a grid. Just send a list of DTO's which are already in the most optimal form. Populating those DTO's can then simply be done through straight SQL, a view, a stored procedure, a <a
href="http://davybrion.com/blog/2010/09/think-twice-before-you-map-entities-to-dtos/">projection through your ORM</a>, a map/reduce operation, or whatever else that makes sense. The point is that in most cases, you should just <em>populate</em> those DTO's as directly as you can instead of <em>mapping</em> to those DTO's. In this case, the DTO's offer a clear-cut benefit and don't really introduce tedious ceremony code in your codebase.</p><p>As for the commands (inserts, updates, anything that <em>creates</em> something or results in something <em>happening</em>), why even use DTO's in the first place? People will often argue that the ability to reuse the entity-mimicking-DTO's for these operations is a benefit. Personally, I don't really see the benefit. The mere presence of entity-mimicking-DTO's only encourages people to use them for the queries as well. Instead, I prefer to go with types that encapsulate all of the data relevant to the current command (the data to be inserted/updated, or whatever else the command needs). In a very simple scenario, this could be an InsertCustomerCommand type which simply has properties for the data that needs to be inserted. Nobody will be confusing these types with any other purpose than what their name communicates.</p><p>If you're building a web app where the server-side code does everything in process (i.e. without a WCF service), then you can in many cases just use the entities directly without a real drawback, though I'd recommend keeping an eye on unexpected select N+1 problems, since those will frequently come up when you're preparing your views. But even in this case, using DTO's that are optimized for the scenarios in which they're being used can really simplify the query-side of your system a lot.</p><p>There's nothing inherently wrong with DTO's if you use them to simply transfer <em>data</em>. If you're using them to transfer entities, you're robbing yourself of their biggest benefit while only making things more complex than they need to be.</p><div
class="bottomcontainerBox" style=""><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <iframe
src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavybrion.com%2Fblog%2F2012%2F02%2Fdtos-should-transfer-data-not-entities%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:85px; height:21px;"></iframe></div><div
style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <g:plusone size="medium" href="http://davybrion.com/blog/2012/02/dtos-should-transfer-data-not-entities/"></g:plusone></div><div
style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://davybrion.com/blog/2012/02/dtos-should-transfer-data-not-entities/"  data-text="DTO&#8217;s Should Transfer Data, Not Entities" data-count="horizontal" data-via="davybrion"></a></div><div
style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://davybrion.com/blog/2012/02/dtos-should-transfer-data-not-entities/" data-counter="right"></script></div><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://davybrion.com/blog/2012/02/dtos-should-transfer-data-not-entities/"></script></div></div><div
style="clear:both"></div><div
style="padding-bottom:4px;"></div>]]></content:encoded> <wfw:commentRss>http://davybrion.com/blog/2012/02/dtos-should-transfer-data-not-entities/feed/</wfw:commentRss> <slash:comments>10</slash:comments> </item> <item><title>Highly Recommended Book: REST In Practice</title><link>http://davybrion.com/blog/2011/03/highly-recommended-book-rest-in-practice/</link> <comments>http://davybrion.com/blog/2011/03/highly-recommended-book-rest-in-practice/#comments</comments> <pubDate>Sat, 05 Mar 2011 18:12:42 +0000</pubDate> <dc:creator>Davy Brion</dc:creator> <category><![CDATA[Architecture]]></category> <category><![CDATA[Books]]></category><guid
isPermaLink="false">http://davybrion.com/blog/?p=3167</guid> <description><![CDATA[A couple of months ago, i knew very little about REST and Restful services. Since it was increasingly getting more attention in the developer community, i wanted to find out what it was about. Luckily for me, O'Reilly had recently released REST In Practice and when it was listed as an O'Reilly Deal Of The [...]]]></description> <content:encoded><![CDATA[<p>A couple of months ago, i knew very little about REST and Restful services. Since it was increasingly getting more attention in the developer community, i wanted to find out what it was about. Luckily for me, O'Reilly had recently released <a
href="http://oreilly.com/catalog/9780596805838/">REST In Practice</a> and when it was listed as an <a
href="http://feeds.feedburner.com/oreilly/ebookdealoftheday">O'Reilly Deal Of The Day</a>, i bought it without thinking twice. Unfortunately, i temporarily stopped reading it about halfway through because i needed to finish some other books first, but i recently picked up where i left off.</p><p>Coming from the Microsoft world were SOAP services have for a long time been the norm in most projects, it's very interesting to see what REST is all about and how these Restful services are built.  If you don't know anything about REST, you might want to check out this <a
href="http://martinfowler.com/articles/richardsonMaturityModel.html">article</a> by Martin Fowler first. Obviously, you don't need to read that before you can read the book but it might pique your interest. The first chapter does a great job of introducing to the architecture of the Web and how the REST architectural style fits within it.  After that, you're introduced to the Restbucks example, which is used throughout the book to show you how Restful services can work.</p><p>The book then gradually starts diving deeper and deeper into the implementation of the Restful services for the Restbucks example. Everything is explained very clearly, and the authors continuously show both the requests and the responses that are going over the wire to illustrate what is going on at the HTTP level, which makes it even easier to understand everything that's being discussed.  After the explanations and request/response examples, there's typically a code-based example in either Java or .NET to show how you can implement these systems. After a while i just started skipping these examples entirely because i thought they didn't really bring any added benefit.  Though i'm sure some people will appreciate those examples being included as well.</p><p>Another great thing about this book is that it remains very clear and easy to follow, even though it covers <em>a lot</em> of ground. Here's a brief overview of the things you'll see implemented in the next couple of chapters:</p><ul><li>CRUD</li><li>Hypermedia (the engine of application state transformations)</li><li>Everything you need to know about caching</li><li>Event-driven services with Atom</li><li>Atom publishing</li><li>Security: authentication as well as authorization</li></ul><p>After that, there's a chapter on the semantic web and microformats, which i didn't find very interesting but others likely will. The final 2 chapters make for a great conclusion of the book. First, there's a pretty extensive comparison between the benefits and drawbacks of SOAP versus REST.  And finally, the last chapter covers when it makes sense to use REST and when it doesn't and ends with a recap of the major selling points of using the Web as a central building block of your architecture.</p><p>I grok the REST architectural style now, and definitely like it.  If you read this book with an open mind, i'd bet you'll like it a lot as well.</p><div
class="bottomcontainerBox" style=""><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <iframe
src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavybrion.com%2Fblog%2F2011%2F03%2Fhighly-recommended-book-rest-in-practice%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:85px; height:21px;"></iframe></div><div
style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <g:plusone size="medium" href="http://davybrion.com/blog/2011/03/highly-recommended-book-rest-in-practice/"></g:plusone></div><div
style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://davybrion.com/blog/2011/03/highly-recommended-book-rest-in-practice/"  data-text="Highly Recommended Book: REST In Practice" data-count="horizontal" data-via="davybrion"></a></div><div
style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://davybrion.com/blog/2011/03/highly-recommended-book-rest-in-practice/" data-counter="right"></script></div><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://davybrion.com/blog/2011/03/highly-recommended-book-rest-in-practice/"></script></div></div><div
style="clear:both"></div><div
style="padding-bottom:4px;"></div>]]></content:encoded> <wfw:commentRss>http://davybrion.com/blog/2011/03/highly-recommended-book-rest-in-practice/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>The MVVM Pattern Is Highly Overrated</title><link>http://davybrion.com/blog/2010/07/the-mvvm-pattern-is-highly-overrated/</link> <comments>http://davybrion.com/blog/2010/07/the-mvvm-pattern-is-highly-overrated/#comments</comments> <pubDate>Wed, 21 Jul 2010 14:23:58 +0000</pubDate> <dc:creator>Davy Brion</dc:creator> <category><![CDATA[Architecture]]></category> <category><![CDATA[Code Quality]]></category> <category><![CDATA[Silverlight]]></category><guid
isPermaLink="false">http://davybrion.com/blog/?p=2392</guid> <description><![CDATA[Update: check out my MVP In Silverlight/WPF series which discusses the MVP approach as an alternative to MVVM If you're doing Silverlight or WPF, you've no doubt come across the MVVM (Model-View-ViewModel) pattern. It seems to be the most popular client-side architecture pattern used among Silverlight/WPF developers. I find the pattern to be highly overrated, [...]]]></description> <content:encoded><![CDATA[<p>Update: check out my <a
href="http://davybrion.com/blog/2010/08/mvp-in-silverlightwpf-series/">MVP In Silverlight/WPF series</a> which discusses the MVP approach as an alternative to MVVM</p><p>If you're doing Silverlight or WPF, you've no doubt come across the MVVM (Model-View-ViewModel) pattern.  It seems to be the most popular client-side architecture pattern used among Silverlight/WPF developers.  I find the pattern to be highly overrated, and actually have some big issues with the whole thing.</p><p>First, let's briefly cover what MVVM is about for those of you who don't know yet.  MVVM virtually eliminates all of the code that would typically be placed in the code-behind file of your View (a user control, a screen, whatever) by putting all of that logic in the ViewModel.  The ViewModel itself is never tightly coupled to the View. If it does have a reference to it, it's typically through an interface that the View implements instead of a reference of the actual type of the View.  This increases, or better yet, introduces testability to a large part of your UI code that you normally wouldn't be able to cover with unit tests if you'd go with the traditional "put it all in the code-behind"-approach.</p><p>The ViewModel typically contains properties for the data that is to be shown in the View, and also raises notification events when the data in those properties changes.  The View uses the powerful data-binding capabilities of Silverlight/WPF to bind the properties of the ViewModel to other user controls that the View is composed of.  User-events that are captured by the View are sent to the ViewModel through Commands.  Typically, these commands execute a method in the ViewModel which contains some kind of logic... usually to either send the updated data in the ViewModel's properties to the Model (usually a Service Layer in Silverlight, in WPF it could be either a true Domain Model or also a Service Layer), to perform some business logic in the Model, or to retrieve data from the Model so it can be placed in the ViewModel's properties so the View can bind to it.</p><p>That is, in a nutshell, how the MVVM pattern works.  So why do i have issues with this? You can develop and test most of the application's logic without being dependent on a physical View and that is typically a Good Thing, no? It sure is!  However, my problem with this approach is that  too many responsibilities are concentrated within the ViewModel.  Its main responsibilities are to facilitate databinding <em>and</em> to interact with the Model.  And that, in my opinion, isn't very clean.  In some ways, you could actually think of the ViewModel as a glorified code-behind, with the only difference being that it's not tightly coupled to the (physical) View.</p><p>In most (if not all) MVVM implementations, a ViewModel has many possible reasons to be changed.  It might need to be changed because of different data-binding requirements.  Then again, it might also require changes when a part of the Model is modified.  Now, i'm sure many of you can agree with me when i say that two of the most important principles in software development are <a
href="http://en.wikipedia.org/wiki/Separation_of_concerns">Seperation of Concerns (SoC)</a> and the <a
href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle (SRP)</a>.  Obviously, the ViewModel doesn't fare well when it comes to both of these principles.</p><p>Lets forget about MVVM for a second and focus on the concerns and responsibilities that a user control can have... say, a user control that shows customer information and allows the user to edit that data so it can be persisted:</p><ul><li>visualization of the actual control (its own layout as well drawing other user controls that it is composed of)</li><li>communication/interaction with the Model</li><li>making data (from the Model) available so it can be displayed</li><li>outputting data in the correct user controls (for instance: various textboxes)</li><li>(simple validation) of modified/inputted data (for instance: no string values for numeric fields, etc...)</li></ul><p>Without MVVM, all of these would be taken care of in the View.  Obviously, not a good idea right?  After all, if it were a good idea, we'd never have had a reason to start using MVVM in the first place.</p><p>Now, with MVVM, a lot of people would divide these concerns and responsibilities like this:</p><p>View:</p><ul><li>visualization of the actual control (its own layout as well drawing other user controls that it is composed of)</li><li>outputting data in the correct user controls (for instance: various textboxes)</li><li>(simple validation) of modified/inputted data (for instance: no string values for numeric fields, etc...)</li></ul><p>ViewModel:</p><ul><li>communication/interaction with the Model</li><li>making data (from the Model) available so it can be displayed</li></ul><p>In this case, the View still has 3 responsibilities which is still too much according to 'the guidelines', but it's not that big of a deal (though plenty of people would argue that the simple validation would be better placed in the ViewModel).  You're highly unlikely to actually want to write automated tests for pure visualization anyway and the SRP is not something that you absolutely need to follow to the extreme in every single place.  For the View, this is really not a problem and very much acceptable.</p><p>The ViewModel however has 2 important responsibilities in this case, and i'd argue that these 2 things should not be done in the same place.  Making data available is done through data-binding.  To do this, you need a set of properties and you need to raise the necessary events.  In most cases, raising those events is very straightforward, but in more complex controls you might need a bit of additional logic to determine which event should be raised at what point.  The other important responsibility is the communication/interaction with the Model.  In most Silverlight applications, the Model will be a Service Layer.  To communicate with this Service Layer, you need Service Proxies.  That means that your ViewModel is essentially responsible for communicating with the Service Layer, dealing with business exceptions that might be thrown by some service calls, and dealing with technical exceptions that can occur simply because of network-related problems.  Group all of those together and i don't think i'm going out on a limb here by saying that that is a lot of logic to put in <em>one</em> class, no?</p><p>(Sidenote: what i don't really understand is that many people who vigorously advocate adherence to SRP and SoC in their domain and business code don't seem to hold their UI code to the same standards. I do.)</p><p>At work, we do <em>a lot</em> of Silverlight development.  We typically have around 5 Silverlight projects in active development at the same time.  And it's been that way for over a year now.  That equals a lot of Silverlight code that we've written and experience and knowledge that we've built up.  And we haven't used MVVM for any of it.  All this time, we've been using the MVP pattern (Supervising Controller variant) with a slight twist.  That twist being a slimmed down version of a <a
href="http://www.martinfowler.com/eaaDev/PresentationModel.html">Presentation Model</a>.  Our Presentation Model's sole responsibility is to facilitate data-binding, and in some cases, a touch of validation is added as well.</p><p>If we go back to our previous example of the customer screen, the responsibilities and concerns would be divided like this in our MVP-PMlight approach:</p><p>View:</p><ul><li>visualization of the actual control (its own layout as well drawing other user controls that it is composed of)</li><li>outputting data in the correct user controls (for instance: various textboxes)</li><li>(simple validation) of modified/inputted data (for instance: no string values for numeric fields, etc...)</li></ul><p>Presenter:</p><ul><li>communication/interaction with the Model based on the contents of the Presentation Model</li></ul><p>Presentation Model:</p><ul><li>making data (from the Model) available so it can be displayed</li></ul><p>Which leads to classes which are more focused on their task instead of trying to focus on too many things at the same time.  In my opinion, this approach is much better/cleaner than MVVM.  Not only is there a noticeable benefit in code quality (classes are more focused), there is also increased potential to reuse our 'light Presentation Models' in multiple controls.  Testability is increased over MVVM as well since it's always easier to test classes which are focused versus testing classes which have too many responsibilities.  All in all, a couple of important benefits and we still haven't thought of a real drawback compared to MVVM.</p><div
class="bottomcontainerBox" style=""><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <iframe
src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavybrion.com%2Fblog%2F2010%2F07%2Fthe-mvvm-pattern-is-highly-overrated%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:85px; height:21px;"></iframe></div><div
style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <g:plusone size="medium" href="http://davybrion.com/blog/2010/07/the-mvvm-pattern-is-highly-overrated/"></g:plusone></div><div
style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://davybrion.com/blog/2010/07/the-mvvm-pattern-is-highly-overrated/"  data-text="The MVVM Pattern Is Highly Overrated" data-count="horizontal" data-via="davybrion"></a></div><div
style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://davybrion.com/blog/2010/07/the-mvvm-pattern-is-highly-overrated/" data-counter="right"></script></div><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://davybrion.com/blog/2010/07/the-mvvm-pattern-is-highly-overrated/"></script></div></div><div
style="clear:both"></div><div
style="padding-bottom:4px;"></div>]]></content:encoded> <wfw:commentRss>http://davybrion.com/blog/2010/07/the-mvvm-pattern-is-highly-overrated/feed/</wfw:commentRss> <slash:comments>102</slash:comments> </item> <item><title>Why You Shouldn&#8217;t Expose Your Entities Through Your Services</title><link>http://davybrion.com/blog/2010/05/why-you-shouldnt-expose-your-entities-through-your-services/</link> <comments>http://davybrion.com/blog/2010/05/why-you-shouldnt-expose-your-entities-through-your-services/#comments</comments> <pubDate>Mon, 17 May 2010 15:18:43 +0000</pubDate> <dc:creator>Davy Brion</dc:creator> <category><![CDATA[Architecture]]></category> <category><![CDATA[Code Quality]]></category> <category><![CDATA[Opinions]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[WCF]]></category><guid
isPermaLink="false">http://davybrion.com/blog/?p=2318</guid> <description><![CDATA[I sometimes still get questions from people who want to expose their entities through their WCF Services.&#160; Regardless of whether these are entities that are populated through NHibernate or any other ORM, this is just not a good thing to do.&#160; Many people prefer to accept and return entities through their services because they believe [...]]]></description> <content:encoded><![CDATA[<p>I sometimes still get questions from people who want to expose their entities through their WCF Services.&#160; Regardless of whether these are entities that are populated through NHibernate or any other ORM, this is just not a good thing to do.&#160; Many people prefer to accept and return entities through their services because they believe this is an easier programming model.&#160; They believe that it takes less work than mapping to DTO’s and that as a whole, this solution is much more manageable.&#160; Rest assured that this is a fallacy.&#160; Any perceived benefit that you’ll get from exposing entities outside of your service layer will only last a <em>very short time</em> and will quickly be dwarfed by added complexity, increased maintenance overhead and a performance overhead which must not be ignored.&#160;</p><p>In this post, i’d like to take the chance to explain the downsides to exposing entities through services.&#160; Though i’ll probably miss quite a few of the downsides (feel free to add to the list through comments), the ones i will mention are IMO important enough to take note of.</p><p><strong>Exposing entities to clients means your clients are very tightly coupled to your service(s)</strong></p><p>Entities are a part of your domain.&#160; These entities in your domain can change for various reasons.&#160; Sometimes because functional changes are required, but quite often also for optimizations (whether they are for performance reasons or to improve the clarity and maintainability of your domain).&#160; Functional changes <em>can </em>impact your clients, though that is not necessarily the case.&#160; Optimizations hardly ever have an impact on your clients (other than possibly improved response times from your service calls obviously).&#160; If your service layer accepts and returns domain entities, each possible change is highly likely to have an impact on your clients.&#160; And this impact is not cheap.&#160; In the best case scenario, it means updating your service contracts, regenerating your service proxies and redeploying your clients.&#160; In the worst case scenario, it means making actual changes to the code of your clients.&#160; And for what? Because of changes that shouldn’t have impacted your clients in the first place?</p><p>Ideally, your clients are as dumb as they can be.&#160; They should know as little as possible about the actual <em>implementation</em> of the domain because that implementation is simply not relevant to them.&#160; They should present users with data and give them the option to modify that data, to trigger actions and to perform certain tasks.&#160; They should focus squarely on those tasks and pretty much everything else is typically better suited to be done behind your service layer.&#160; If you build your clients with no real knowledge of the actual domain model, but of DTO’s and possible actions to be performed then you can reduce the level of coupling between your clients and your services substantially.</p><p>Many of the people who prefer to expose entities often claim that going for the DTO approach introduces too much extra work and too many extra, seemingly unnecessary classes.&#160; For starters, they don’t want to write code that maps entities to DTO’s.&#160; First of all, the amount of code that this requires is in reality <em>very small</em>, not to mention <em>very easy</em>.&#160; Secondly, you can just as well use a library such as AutoMapper to take that pain away from you.&#160; And contrary to what you might think, there is a big performance gain to be had from returning DTO’s over entities, but i’ll get to that in the next section.</p><h5></h5><h5></h5><p><strong>Entities are hardly ever the most optimal representation of data</strong></p><p>I think we can safely say that most applications need to show data in the following 3 ways:</p><ul><li>In a grid view, either as a total listing of all instances of a certain type of data or the result of a search query or some kind of filtering action</li><li>In dropdown controls or anything else that lets users select pieces of data</li><li>In edit screens where a piece of data needs to be displayed in its entirety, perhaps even to be modified by the user</li></ul><p>There are undoubtedly more ways in which data can be presented to the user but i think it’s safe to say that most business applications will certainly rely on the following 3 ways quite heavily.</p><p>In the case of a grid view, you’re frequently showing data that is related to more than one entity.&#160; You’ll often need to include the name or the description of some associated entities.&#160; So what exactly is it that you want to do in this situation?&#160; Do you want to return a list of the main entities of the grid view, which all have their required association properties filled in so you can display the columns that you need in the grid view?&#160; Do you actually need all of the properties of these entities (for both the main entities and the associated entities)?&#160; Odds are high that you’re going to be returning a lot more data to the client than you actually need.&#160; And that is what is realistically going to hurt the performance of your system.&#160; Any piece of unnecessary data that you transmit to your clients has a cost associated with it.&#160; The unnecessary data is retrieved from the database.&#160; The entities are then serialized at the service end.&#160; Then they are transmitted to the client.&#160; Then they are deserialized by your client.&#160; All of this is pretty costly, so the more unnecessary data that is included in this operation, the more your performance and the responsiveness of your client (not to mention your database and your server) is impacted negatively.</p><p>In the case of dropdown controls or anything else that lets users select pieces of data, you typically only need very few of the properties of that piece of data.&#160; In many cases, the primary key and a name or a description are sufficient.&#160; Do you really need to transmit the entire entity every time for usages like this? Again, keep in mind that all of that extra data that will never be used by your client needs to be retrieved, serialized, transmitted and deserialized again.&#160; Surely, this is an awful waste, no?&#160;</p><p>And then there’s the case where a piece of data needs to be displayed in its entirety.&#160; In these cases, you will almost always need all of the properties of the entity that is displayed, but you’ll most often also need to show other data (things that can be selected, or linked to the main entity).&#160; This other data will in most cases fall into the previous category where you’ll only need very little information about the actual entity.&#160; If you’re smart, you’ve chosen the DTO approach to retrieve this data for the data that can be selected, and in that case, you already have all of the infrastructural code in place to project entities or data into DTO’s.&#160; So you might as well reuse it for the main entity as well since you already have the capability to do this.</p><p>Always keep in mind that your entities will frequently either contain <em>more data than needed</em>, or <em>less data than needed</em>.&#160; As such, it just doesn’t make much sense to expose entities to your clients since they are hardly ever optimal for client-side usage.&#160; If you really want to think about performance, stop worrying about the supposed cost of mapping to DTO’s (which is truly negligible) and start focusing on <em>what</em> your actually <em>sending</em> to and from your service because this is far more costly than any kind of DTO-mapping really is.</p><p><strong>Must your data really come from entities?</strong></p><p>If you are displaying data to your user, does that data really need to come from your domain model?&#160; Does it really need to be retrieved by populating a collection of entities to then return them to the client?&#160; Again, keep the <em>form</em> of the data in mind when thinking about this.&#160; In many cases, as i mentioned above, an entity is not the most optimal form of the data that your client needs.&#160; So why even retrieve it through entities? Sure, asking your ORM to retrieve a set of entities based on a set of criteria is often the easiest thing to do, but if the easiest path were the best path, the overall quality of software projects wouldn’t be in the sad state that it’s in today.&#160; If the form of the required data is not identical to the structure of an entity, it’s often far more optimal to simply populate a DTO <em>directly from the data.</em>&#160; With NHibernate, you can easily do this by adding a list of projections to your query and then using a ResultTransformer to populate the DTO’s based on the direct output of the query.&#160; In this case, no entity instance ever needs to be created when you’re just retrieving data, and no extra mapping between the entity and the DTO’s needs to be performed.&#160; Your data access code simply retrieves the resulting data from a query, and puts that data directly in your DTO’s.&#160; There’s no reason why usage of an ORM should prevent you from doing this.&#160;&#160; Once again, this approach will offer far more performance benefits than avoiding DTO mapping at all costs ever can.</p><p><strong>What about the behavior of your entities?</strong></p><p>Do your entities have any behavior in them?&#160; If not, they are already more of a DTO than a true entity.&#160; In fact, if your entities have no behavior at all, you could even wonder why you’re using an ORM in the first place.&#160; Now, behavior can mean many things.&#160; It could mean lazy loading of associations.&#160; It could mean actual business logic.&#160; Obviously, lazy-loading doesn’t (and shouldn’t!) work client-side, but what about your business logic? Do you have business logic that <em>can </em>be executed client-side? Or is it business logic that should only be executed behind the service layer? If so, how do you make the distinction between this to prevent client-side usage from these entities? Whatever you do, you’re pretty much opening up a can of worms that really is better avoided in the first place.</p><p><strong>How are you going to deal with technical issues?</strong></p><p>Accepting and returning entities from services introduces a host of technical issues that can be quite substantial.&#160; Serialization and deserialization specifically are issues that you need to be worried about.&#160; If you’re using an ORM which does lazy-loading of associations, this will certainly cause serialization issues that you need to work around.&#160; You can either disable lazy loading, or you can make sure that your entities are always fully initialized (as in: always have their associations fully loaded) before they are sent back to the client.&#160; Disabling lazy-loading <em>will </em>cause performance problems in your service layer, either in places where you don’t expect them to be or in places that you haven’t thought of before it’s too late.&#160; Fully loading your entities and their associates before returning them is another performance nightmare waiting to happen so that’s really not an ideal solution either.&#160; You can try to hook into the serialization process or even the lazy-loading features of your ORM but whatever you do in that case will be a hack that <em>will</em> cause issues sooner or later.&#160; And again, all of these problems can very easily be avoided with a solution which, i hope you realize by now, offers plenty more benefits than any solution where you accept/return entities in your service.</p><p><strong>Conclusion</strong></p><p>Every single downside to exposing entities through services are issues that i have myself encountered in past projects, either ones i’ve worked on myself, or ones that i’ve seen other people work on.&#160; If that’s not enough for you, then maybe you’ll find it interesting to know that some of the brightest and most respected people (like Udi Dahan and Ayende for instance) in the .NET community also actively recommend against exposing entities through services because of the same downsides that i mentioned, though they could probably give you even more downsides that i forgot to cover in this post.&#160; These downsides are not figments of anyone’s imagination.&#160; They are very real, and you really, really ought to think twice before dismissing this advice.&#160;</p><div
class="bottomcontainerBox" style=""><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <iframe
src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavybrion.com%2Fblog%2F2010%2F05%2Fwhy-you-shouldnt-expose-your-entities-through-your-services%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:85px; height:21px;"></iframe></div><div
style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <g:plusone size="medium" href="http://davybrion.com/blog/2010/05/why-you-shouldnt-expose-your-entities-through-your-services/"></g:plusone></div><div
style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://davybrion.com/blog/2010/05/why-you-shouldnt-expose-your-entities-through-your-services/"  data-text="Why You Shouldn&rsquo;t Expose Your Entities Through Your Services" data-count="horizontal" data-via="davybrion"></a></div><div
style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://davybrion.com/blog/2010/05/why-you-shouldnt-expose-your-entities-through-your-services/" data-counter="right"></script></div><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://davybrion.com/blog/2010/05/why-you-shouldnt-expose-your-entities-through-your-services/"></script></div></div><div
style="clear:both"></div><div
style="padding-bottom:4px;"></div>]]></content:encoded> <wfw:commentRss>http://davybrion.com/blog/2010/05/why-you-shouldnt-expose-your-entities-through-your-services/feed/</wfw:commentRss> <slash:comments>27</slash:comments> </item> <item><title>Hey Microsoft, Our Databases Aren&#8217;t Services!</title><link>http://davybrion.com/blog/2010/01/hey-microsoft-our-databases-arent-services/</link> <comments>http://davybrion.com/blog/2010/01/hey-microsoft-our-databases-arent-services/#comments</comments> <pubDate>Sun, 17 Jan 2010 17:46:11 +0000</pubDate> <dc:creator>Davy Brion</dc:creator> <category><![CDATA[Architecture]]></category> <category><![CDATA[Opinions]]></category> <category><![CDATA[WCF]]></category><guid
isPermaLink="false">http://davybrion.com/blog/2010/01/hey-microsoft-our-databases-arent-services/</guid> <description><![CDATA[Something that frequently bothers me is when people/companies create services that are basically thin layers on top of their database.&#160; The service contracts expose the typical CRUD operations for each table, and add some additional methods for specific queries etc.&#160; These kind of services will sometimes pretend to contain a bit of ‘business logic’ but [...]]]></description> <content:encoded><![CDATA[<p>Something that frequently bothers me is when people/companies create services that are basically thin layers on top of their database.&#160; The service contracts expose the typical CRUD operations for each table, and add some additional methods for specific queries etc.&#160; These kind of services will sometimes pretend to contain a bit of ‘business logic’ but they are essentially just a remote interface into your database with maybe a bit of extra security on top of it.&#160; They effectively turn your database into a remote service.&#160; Now if you’re anything like me, you’re probably thinking “why on earth would people do that?”</p><p>There are probably a few answers to that question, but one reason that can’t really be disputed is that a lot of the tooling that Microsoft offers to developers simply encourage this kind of stuff.&#160; Let’s go over a little example.</p><p>I wanted to see what some of Microsoft’s recommended tools would create for me if i wanted to create a Silverlight application which uses a database.&#160; Obviously, a Silverlight application can’t use a database directly so the application will need to communicate with a service.&#160; The service obviously does have access to the database.&#160; RIA Services is a solution that Microsoft seems to be pushing a lot for this specific scenario so i figured i’d give this a shot.</p><p>I created a RIA Services Class Library project in my solution and tried to add a ‘Domain Service Class’ (as the RIA Services templates call it) to the project.&#160; If you already have a DataContext or an ObjectContext defined within the same assembly, you can immediately select the database tables that you want to expose.&#160;&#160; So i canceled the dialog and quickly added an ADO.NET Entity Data Model to the solution for which i selected my <a
href="http://www.codeplex.com/ChinookDatabase" target="_blank">Chinook</a> database.&#160; I tried to create a ‘Domain Service Class’ again and got the following window:</p><p><a
href="http://davybrion.com/pictures/YourDatabaseIsNotAService_E97E/create_service.png" rel="prettyPhoto[2209]"><img
style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="create_service" border="0" alt="create_service" src="http://davybrion.com/pictures/YourDatabaseIsNotAService_E97E/create_service_thumb.png" width="454" height="554" /></a></p><p>Well, now that sure is easy isn’t it? I can immediately select all my ‘Entities’ and i can even check whether i want to be able to edit them through the service, and apparently i can also generate associated classes for metadata (which would be useful for validation according to the tooltip).&#160; I checked the Album table, and left the ‘Enable editing’ option unchecked.&#160; This created a service with the following code:</p><div
style="font-family: consolas; background: white; color: black; font-size: 10pt"><p
style="margin: 0px">&#160;&#160;&#160; <span
style="color: green">// Implements application logic using the ChinookEntities context.</span></p><p
style="margin: 0px">&#160;&#160;&#160; <span
style="color: green">// TODO: Add your application logic to these methods or in additional methods.</span></p><p
style="margin: 0px">&#160;&#160;&#160; <span
style="color: green">// TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access</span></p><p
style="margin: 0px">&#160;&#160;&#160; <span
style="color: green">// Also consider adding roles to restrict access as appropriate.</span></p><p
style="margin: 0px">&#160;&#160;&#160; <span
style="color: green">// [RequiresAuthentication]</span></p><p
style="margin: 0px">&#160;&#160;&#160; [<span
style="color: #2b91af">EnableClientAccess</span>()]</p><p
style="margin: 0px">&#160;&#160;&#160; <span
style="color: blue">public</span> <span
style="color: blue">class</span> <span
style="color: #2b91af">AlbumService</span> : <span
style="color: #2b91af">LinqToEntitiesDomainService</span>&lt;<span
style="color: #2b91af">ChinookEntities</span>&gt;</p><p
style="margin: 0px">&#160;&#160;&#160; {</p><p
style="margin: 0px">&#160;</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: green">// TODO: Consider</span></p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: green">// 1. Adding parameters to this method and constraining returned results, and/or</span></p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: green">// 2. Adding query methods taking different parameters.</span></p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: blue">public</span> <span
style="color: #2b91af">IQueryable</span>&lt;<span
style="color: #2b91af">Album</span>&gt; GetAlbum()</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: blue">return</span> <span
style="color: blue">this</span>.ObjectContext.Album;</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p
style="margin: 0px">&#160;&#160;&#160; }</p></div><p>&#160;</p><p>I don’t know about you, but i love those TODO statements.&#160; After all, you really do might want to <em>consider</em> constraining the resultset that could be returned by the GetAlbum method.&#160;&#160; Who knows, perhaps you have certain use cases where you don’t want <em>all</em> of the ‘entities’ in the Album <em>table</em> to be returned by your ‘domain service’<em>.</em>&#160; Hopefully, this service will be used by developers who are smart enough to realize that they should modify this method, instead of using client-side LINQ statements to filter the returned Album ‘entities’ as i’m sure we’ve all seen in too many Microsoft demo’s already.</p><p>It gets better if you recreate the service and check the ‘Enable editing’ option.&#160; Now you’re ‘domain service’ will also contain the following methods:</p><div
style="font-family: consolas; background: white; color: black; font-size: 10pt"><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: blue">public</span> <span
style="color: blue">void</span> InsertAlbum(<span
style="color: #2b91af">Album</span> album)</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: blue">this</span>.ObjectContext.AddToAlbum(album);</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p
style="margin: 0px">&#160;</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: blue">public</span> <span
style="color: blue">void</span> UpdateAlbum(<span
style="color: #2b91af">Album</span> currentAlbum)</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: blue">if</span> ((currentAlbum.EntityState == <span
style="color: #2b91af">EntityState</span>.Detached))</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: blue">this</span>.ObjectContext.AttachAsModified(currentAlbum, <span
style="color: blue">this</span>.ChangeSet.GetOriginal(currentAlbum));</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p
style="margin: 0px">&#160;</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: blue">public</span> <span
style="color: blue">void</span> DeleteAlbum(<span
style="color: #2b91af">Album</span> album)</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: blue">if</span> ((album.EntityState == <span
style="color: #2b91af">EntityState</span>.Detached))</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: blue">this</span>.ObjectContext.Attach(album);</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span
style="color: blue">this</span>.ObjectContext.DeleteObject(album);</p><p
style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p></div><p>&#160;</p><p>Man, this sure is easy, isn’t it? I now have a service that offers me full CRUD access to the Album table in my database.&#160; If i wanted to, i could now start implementing a screen in my Silverlight application which allows my users to list the albums, edit them, delete them, create new ones, etc… and i wouldn’t even have to change anything in my ‘service layer’.&#160;&#160; The problem is that too many developers actually will do exactly that.&#160;&#160; After all, why should they doubt any code that was generated by a tool which comes from Microsoft?&#160; If the tool can generate this, then certainly some people actually want you to use it like this, no?&#160; If not, why would it even generate code like this?</p><p>I’m sure this kind of stuff gets a lot of ooh’s and aah’s during the product demo’s at Microsoft events, but other than that, what good does this really bring?&#160; Is this really the way you want people to develop their services?&#160; Do you really want developers to pretty much expose the database as-is to remote clients?&#160; And those TODO statements simply won’t cut it, you know that all too well.&#160; I simply don’t think there’s any good reason to generate code like this because a lot of people will simply take it as is and use it like that directly from their client code.&#160; Oh sure, most of them will hook up the authentication but i’m willing to bet that very few people will actually put real business logic in there.&#160; Why would they?&#160; The message that a lot of people will get from the resulting code is that a service is merely a way to provide CRUD for your database tables.&#160; What’s business logic to these people?&#160; Right, the stuff they’ll implement in their presentation layer because this service doesn’t really encourage people to consider implementing it there.</p><p>The <em>only </em>benefit that i can see from using RIA services is that you don’t really have to deal with your service contracts, and your operation contracts or any of that stuff.&#160;&#160; No, we won’t be doing any of that.&#160; We simply use a [EnableClientAccess] attribute and we’re done!&#160; I don’t really consider that a benefit, though i can certainly understand why people would not want to deal with <a
href="http://davybrion.com/blog/2009/07/why-i-dislike-classic-or-typical-wcf-usage/" target="_blank">the pain of classic WCF services</a>.&#160; RIA Services is simply a solution to the <em>wrong</em> problem.</p><p>I haven’t looked at ADO.NET Data Services (which will be renamed to WCF Data Services in .NET 4.0) yet, but i suppose it’ll be more of the same: something that makes it incredibly easy to create services directly on top of your database data.</p><p>Seriously though: who on earth actually <em>wants </em>that?</p><p>I have no doubt that there are some people out there that are using RIA Services in a responsible manner and are sticking by responsible architectural guidelines.&#160; I also have no doubt that those people are ignoring most of the tooling that is offered by Microsoft around it.&#160; So really, why not get rid of this kind of tooling, and spend the effort that normally goes into those tools (or anything else which encourages bad practices for that matter) on providing actual guidance to the developers of your platform?&#160; The last thing we need are more developers who think that this is ‘ok’, or projects that have been delivered based on this kind of ‘architecture’, or customers that are turned off by .NET projects because “they all have maintenance problems”.&#160;</p><div
class="bottomcontainerBox" style=""><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <iframe
src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavybrion.com%2Fblog%2F2010%2F01%2Fhey-microsoft-our-databases-arent-services%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:85px; height:21px;"></iframe></div><div
style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <g:plusone size="medium" href="http://davybrion.com/blog/2010/01/hey-microsoft-our-databases-arent-services/"></g:plusone></div><div
style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"> <a
href="http://twitter.com/share" class="twitter-share-button" data-url="http://davybrion.com/blog/2010/01/hey-microsoft-our-databases-arent-services/"  data-text="Hey Microsoft, Our Databases Aren&rsquo;t Services!" data-count="horizontal" data-via="davybrion"></a></div><div
style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://davybrion.com/blog/2010/01/hey-microsoft-our-databases-arent-services/" data-counter="right"></script></div><div
style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://davybrion.com/blog/2010/01/hey-microsoft-our-databases-arent-services/"></script></div></div><div
style="clear:both"></div><div
style="padding-bottom:4px;"></div>]]></content:encoded> <wfw:commentRss>http://davybrion.com/blog/2010/01/hey-microsoft-our-databases-arent-services/feed/</wfw:commentRss> <slash:comments>29</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 2/34 queries in 0.020 seconds using disk: basic
Object Caching 741/805 objects using disk: basic
Content Delivery Network via Amazon Web Services: CloudFront: d18sni7re4ly7f.cloudfront.net

Served from: davybrion.com @ 2012-05-23 13:36:33 -->
