Friday, November 1, 2013

Single Page Application vs. MVC application

Currently I am facing the decision to choose the front end between the popular and kind-of-mature AngularJS based single page application (SPA) vs. the less-exciting ASP.NET MVC application.  I came across this article The "SPA Tax", it lists the following reasons showing that SPA is more expensive than MVC:

Some reasons why it is more expensive to build a SPA vs. a normal MVC application as of 2013:
  • You need more experienced web developers that understand JavaScript better than the usual Web Developer (higher labor cost)
  • There are more pieces to assembling a SPA (example: multiple javascript libraries from multiple authors are needed) than a MVC application (R&D cost and maintenance cost)
  • Development tools are not yet on par with normal MVC application development (time added, so more labor cost)
Though it has some cents in terms of development tools. The need for more experienced developer and more pieces to assembling is trivial. What hold me back to not using AngularJS as the front-end now is the impedance between JavaScript and C#.

The reason I like C# and .NET so much is that it addresses a big (the biggest technical ?) headache in application development: the ORM. As long as a relational DBMS is used to store application data, then LINQ and Entity Framework combined brings the most development productivity you can find in today's development environment. So I choose C#/.NET as my back-end development programming tool.

Then when you compare any SPA with ASP.NET MVC, the pros and cons become obvious: using any SPA (AngularJS, Knockout etc), you need to define all view models again in JavaScript. Coming with this re-definition of view models, you also need to replicate all validation rules in the client side. For large application, it could be a big burden.

When should I choose SPA as my front end? It is the day that I can use the same programming language (TypeScript, JavaScript or Dart) to seamlessly access the back-end data repository. When JavaScript-based database such as MongoDB is mature enough to support real-life business transactions, we can use JavaScript in both front end and back end. Then there is no reason to not use SPA in the front end.

So the timing depends on the back end database layer. The key is to make ORM really easy with JavaScript. W can use JSON-based database for all business transactions. Both CouchDB (http://en.wikipedia.org/wiki/CouchDB)  and PostgresSQL(http://en.wikipedia.org/wiki/PostgresSQL)  have a great potential here.


Tuesday, October 22, 2013

A ubiquitos language for a typical 3-tier .Net Application

Introduction

There many different definitions/terms in a typical 3-tier application that often cause confusions among application developers. Following the philosophy of domain driven design (DDD) (see http://en.wikipedia.org/wiki/Domain-driven_design  and http://dddcommunity.org/learning-ddd/what_is_ddd/) , we try to develop a ubiquitous language (http://martinfowler.com/bliki/UbiquitousLanguage.html ) for a typical 3-tier .Net application architecture. The ubiquitous language facilitates communication among developers and helps to standardized code naming conventions. Here we summarize the best practices used in a typical three-tier .Net application (http://en.wikipedia.org/wiki/Multitier_architecture). It also helps to clarify concepts such as whether or not to define business logic in a domain entity. 

Three-tier Application Architecture

The three tiers are Database tier, Domain tier and View tier. There are three types of data used in a three-tier application: Database Data, Domain Entities and View Models. The operations on these data are called services. Correspondingly, the services are named Database service, Domain Service, and Model Service. In addition to their services, the data tier and view tier also include data mapping functions that map their data to/from domain entities. Domain tier only has domain entities and entity methods that implement domain-specific processing logics.

The following diagram depicts the relationships among different modules in the three tiers. The arrows describe a use or dependent relationship between two modules. The diagram suggests that the domain tier is agnostic of the data tier and the view tier. In Visual Studio, the domain project should not dependent on the view project and, maybe a surprise to a few people, the database project. The runtime dependency of view à domain à database is resolved by an independency inject framework such as Unity.

The texts in the parentheses give suggested naming convention for classes and interfaces. For example, a Person entity will have a class name Person, a persistence service interface IPersonDbService, a persistence service implementation named PersonDbService, a Database-Entity mapper called PersonDbMap, a view model named PersonModel and a Enity-View mapper called PersonModelMap. Some modules are optional for a specific project. For example, if a library such as AutoMapper is used for Entity-View mapping, there are no mapping classes for Enity-View Mapping module.  


Domain Tier

As the core of an application, domain entities are domain concepts/terms defined by POCO (Plain Old CLR Object) classes. For example, a person is defined in a Person POCO class. A POCO class usually has a list of public properties that only have get and set accessors defined. Additionally, a domain entity can have single-entity business rules encapsulated in it (see http://martinfowler.com/eaaCatalog/domainModel.html  and http://martinfowler.com/bliki/AnemicDomainModel.html). For example, a Person class has a DateOfBirth property. It can also define a property such as IsAdult to return true/false based on the business domain rule. The property and method definitions of domain entities are solely based on domain-specific requirements.
Cross-entity domain logics should be defined in the domain services. The relationships among domain entities reflect the relationships defined by the ubiquitous language of that domain. The domain service only defines entity aggregations and cross-entity business logics using domain entities and Database service interfaces.  The database tier API is defined in the domain tier with a naming convention of IEntityDbService. For example, the Person entity should have a database service interface IPersonDbService. The interface IPersonDbService is defined in Domain tier because its APIs are derived from domain-specific persistency requirements.

Database Tier 

Database tier provides functions to access persistent SQL or NoSQL database data. Database tier implements the mapping between the persistent data and the domain entities. The mappings are defined in mapping classes that should have a “Map” postfix. For example, the mapping class for the Person entity is named as PersonMap.  All maps are defined in EntityMaps folder.

A database tier service implements IEntityService (defined in Domain tier) that provide CRUD operations on domain entities. Generally there are one interface and one implementation for each entity aggregation. The interface and class should have a “DbService” postfix. For example, a database service implements PersonDbService  for the Person entity’s IPersonDbService interface.

.Net Entity Framework (EF) significantly reduces the workload in data service development. For the mapping task, in code-first development, EF allows us to define a set of data type mapping classes using fluent API (http://msdn.microsoft.com/en-us/data/jj591617.aspx) to map the CLR types (properties) to database tables (columns). The mapping classes also define some constraints such as not null or referential integrity.  

Presentation Tier

View Models are view-specific data classes used by the view layer. It may aggregate multiple domain entities and transform some domain entity properties. For example, we often display related domain entities together and may want to display the date of birth in a user-friendly format.

The view tier provides functions mapping between domain entities and view models. It implements view logics and should only expose high-level simple API to other view layer components (usually controllers in a MVC application). Libraries such as AutoMapper (https://github.com/AutoMapper/AutoMapper) can help to map/transform domain entities to/from view models.

The model classes in view tier should have a postfix of “Model”. For example, the Person class used in a view in the presentation layer is named as PersonModel. If mapper classes are used to map between entities and models, they should have a “ModelMap” postfix. For example, for the Person entity, the view tier mapper is called PersonModelMap.

Project Layout

We put the three tiers into their corresponding projects. In Database project, we have a Mappers folder for data mapping classes and a Services folder for data services. In domain project, we have an Entities folder, a Service folder and a DbInterface folder. In presentation project, we have a Models folder, a Mappers folder, and a Services folder in addition to other common folders such as Views and Controllers.

Saturday, April 6, 2013

Akin's Laws of Spacecraft Design ( a copy)

I really like these laws http://spacecraft.ssl.umd.edu/akins_laws.html. I copy them below as a reference.  Among others, the following rules matter a lot for me:

15. (Shea's Law) The ability to improve a design occurs primarily at the interfaces. This is also the prime location for screwing it up.
22. When in doubt, document.
39. To get an accurate estimate of final program requirements, multiply the initial time estimates by pi, and slide the decimal point on the cost estimates one place to the right.


==== Original List =====

1. Engineering is done with numbers. Analysis without numbers is only an opinion.
2. To design a spacecraft right takes an infinite amount of effort. This is why it's a good idea to design them to operate when some things are wrong .
3. Design is an iterative process. The necessary number of iterations is one more than the number you have currently done. This is true at any point in time.
4. Your best design efforts will inevitably wind up being useless in the final design. Learn to live with the disappointment.
5. (Miller's Law) Three points determine a curve.
6. (Mar's Law) Everything is linear if plotted log-log with a fat magic marker.
7. At the start of any design effort, the person who most wants to be team leader is least likely to be capable of it.
8. In nature, the optimum is almost always in the middle somewhere. Distrust assertions that the optimum is at an extreme point.
9. Not having all the information you need is never a satisfactory excuse for not starting the analysis.
10. When in doubt, estimate. In an emergency, guess. But be sure to go back and clean up the mess when the real numbers come along.
11. Sometimes, the fastest way to get to the end is to throw everything out and start over.
12. There is never a single right solution. There are always multiple wrong ones, though.
13. Design is based on requirements. There's no justification for designing something one bit "better" than the requirements dictate.
14. (Edison's Law) "Better" is the enemy of "good".
15. (Shea's Law) The ability to improve a design occurs primarily at the interfaces. This is also the prime location for screwing it up.
16. The previous people who did a similar analysis did not have a direct pipeline to the wisdom of the ages. There is therefore no reason to believe their analysis over yours. There is especially no reason to present their analysis as yours.
17. The fact that an analysis appears in print has no relationship to the likelihood of its being correct.
18. Past experience is excellent for providing a reality check. Too much reality can doom an otherwise worthwhile design, though.
19. The odds are greatly against you being immensely smarter than everyone else in the field. If your analysis says your terminal velocity is twice the speed of light, you may have invented warp drive, but the chances are a lot better that you've screwed up.
20. A bad design with a good presentation is doomed eventually. A good design with a bad presentation is doomed immediately.
21. (Larrabee's Law) Half of everything you hear in a classroom is crap. Education is figuring out which half is which.
22. When in doubt, document. (Documentation requirements will reach a maximum shortly after the termination of a program.)
23. The schedule you develop will seem like a complete work of fiction up until the time your customer fires you for not meeting it.
24. It's called a "Work Breakdown Structure" because the Work remaining will grow until you have a Breakdown, unless you enforce some Structure on it.
25. (Bowden's Law) Following a testing failure, it's always possible to refine the analysis to show that you really had negative margins all along.
26. (Montemerlo's Law) Don't do nuthin' dumb.
27. (Varsi's Law) Schedules only move in one direction.
28. (Ranger's Law) There ain't no such thing as a free launch.
29. (von Tiesenhausen's Law of Program Management) To get an accurate estimate of final program requirements, multiply the initial time estimates by pi, and slide the decimal point on the cost estimates one place to the right.
30. (von Tiesenhausen's Law of Engineering Design) If you want to have a maximum effect on the design of a new engineering system, learn to draw. Engineers always wind up designing the vehicle to look like the initial artist's concept.
31. (Mo's Law of Evolutionary Development) You can't get to the moon by climbing successively taller trees.
32. (Atkin's Law of Demonstrations) When the hardware is working perfectly, the really important visitors don't show up.
33. (Patton's Law of Program Planning) A good plan violently executed now is better than a perfect plan next week.
34. (Roosevelt's Law of Task Planning) Do what you can, where you are, with what you have.
35. (de Saint-Exupery's Law of Design) A designer knows that he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away.
36. Any run-of-the-mill engineer can design something which is elegant. A good engineer designs systems to be efficient. Agreat engineer designs them to be effective.
37. (Henshaw's Law) One key to success in a mission is establishing clear lines of blame.
38. Capabilities drive requirements, regardless of what the systems engineering textbooks say.
39. The three keys to keeping a new manned space program affordable and on schedule:
       1)  No new launch vehicles.
       2)  No new launch vehicles.
       3)  Whatever you do, don't decide to develop any new launch vehicles.
40. Space is a completely unforgiving environment. If you screw up the engineering, somebody dies (and there's no partial credit because most of the analysis was right...)

Monday, March 18, 2013

20 Rules of Software Consulting (by Josh Berkus)

The following is from http://www.databasesoup.com/2013/03/20-rules-of-software-consulting.html. Should review this rules frequently.


  1. Technology Reflects the Business: show me a client with a chronic software problem, and I'll show you a client with a chronic management problem.
  2. Three Things You Will Never See:
    a. A timeline which is too generous;
    b. A client who pays too quickly;
    c. An accurate and complete specification.
  3. Half of Applications Are Immortal: "temporary, one-off" applications often last for years, and there is code from the 1960's which is still running today. Always plan for longevity.
  4. Bad Clients Will Destroy Your Business: half of your success will be built on the ability of recognizing bad clients and avoiding them or terminating their contracts before they suck away all of your time and resources. Always be able to walk away, even if it means giving a refund.
  5. Ask Not What's Possible: the question is not what you can do, the question is how much the client is willing to pay for it and how long they will wait.
  6. Time Substitutes for Money on a Logarithmic Scale: e.g cutting the time by 20% will require doubling the budget. Cutting the budget by 30% will quadruple the amount of time.
  7. All Estimates are Optimistic: new application development will take three times as long as you expect, and cost twice as much. Or vice-versa.
  8. Three Things You Will Never Have Enough Time For:
    a) The specification and prototypes
    b) Documentation
    c) Code maintainability
  9. All Substantial Applications Have Platypuses, which are objects or bits of data which defy all attempts to fit them to well-defined business processes. Platypuses are both why perfect data integrity is unachievable, and the source of at least 30% of troubleshooting.
  10. Don't Call it Refactoring:  clients never pay for cleanup, even if that's what they need.  Figure out a way to call the refactoring something else so you can get it done.
  11. The Longer You Wait to Refactor, the Longer It Will Take. Major template or schema changes at production time are particularly deadly.
  12. Always Have a Contract, even for one-day jobs. Also, use your own contract, and not the client's, and have your contract written by a real attorney. It's worth it.
  13. The Contract-Writing Process Is a Litmus Test for Its Fulfillment. If the client spends a lot of time arguing over the contract, then actually working with them (or getting paid) will be even more difficult. If the client insists on an odd and obscure clause, they're planning to exercise it.
  14. The Client Has Very Poor Memory: no matter what they sign, the client will have forgotten what they agreed to within days, if not hours. Document all requests and changes and keep copies.
  15. Never Agree to a Fixed Bid for anything where you have not done the same exact task at least twice before.
  16. Third Parties are Incompetent: never agree to a fixed bid or success-based payment for any task which is even partially dependent on the speed, documentation or product quality of a third party not under your direct control. This means no fixed bids for data interchange or fixing other people's code, ever.
  17. The Client has No Taste: never allow the client to choose your tools, your subcontractors or your work environment. Or at least charge them a lot extra for the privilege.
  18. Always Bill for Meetings, or you will spend half your life attending them.
  19. A Half-Empty Mailbox Is The Exception: usually, if one client decides to pay unusually late in a month, all of your clients will. Always be able to survive 60 days on your savings.
  20. A Sufficiently Late Project Will Never Be Completed.  In general, any project which is more than 150% past its original deadline has sufficient systemic issues to permanently prevent delivery.


Sunday, February 24, 2013

Comparison of Web Service API implementation methods in ASP.NET

In ASP.NET, there are several options to implement Web Service API. Below is a quick summary:

1) XML Web Service -- SOAP
When: .NET Framework 1.0 (Feb. 2002)
What: simple service call using SOAP, XML, HTTP and WSDL.
Development tool: ASP.Net WebMethod attribute, Web Service class, auto-generated asmx description
Pros: very simple for simple service
Cons: not flexible because client has to understand SOAP. the overhead of SOAP message and XML encoding.

2) WCF -- Service Oriented Architecture (SOA)
When: .NET Framework 3.0 (Nov. 2006)
What: SOA, typically SOAP and other protocols/services/message types (XML, JSON etc).
Development tools: Visual Studio templates, classes, configurations
Pros: very flexible (duplex communication, messaging), the focus of Microsoft for general distributed SOA
Cons: heavy in size and configuration due to its flexibility

3) Web API -- RESTful API
When: was part of WCF but was combined with ASP.NET MVC 3.0  Web API (Jan 2011)
What: RESTful API over HTTP, message types are usually XML and JSON, but customizable
Development tools: many tools in both server and client side.
Pros: the current industry trend, simple and flexible.
Cons: more complex than XML Web Service because of the routing design

Which to use? The XML Web service is deprecated. The choose is between WCF and Web API. The article (http://blogs.microsoft.co.il/blogs/idof/archive/2012/03/05/wcf-or-asp-net-web-apis-my-two-cents-on-the-subject.aspx) summarized the debate as below:

  • For resource-oriented services over HTTP, use Web API
  • Others (messaging, duplex communication, TCP/UDP communication), use WCF
For Web Service development, the design of URI and API interface is a big challenge. If you application needs a set of Web API to access a data set, OData (http://www.odata.org/) standardized the APIs for this specific area.

Both WCF and Web API have very good support for OData development. It is a good start for you to develop data access API.