KISS Architecture (part 4)

by Chris 19. December 2008 15:59

In this part, I will get more practical by showing how the first parts of this architecture looks in code, and if you want some background, please see the previous parts:

  • Part 1 was a general introduction
  • Part 2 outlined the architecture (tiers, etc)
  • Part 3 showed the benefit of loosely coupled tiers (distribution, cloud, etc)

    As I mentioned in the first part, the implemented architecture is published on CodePlex in a project called KISS Architecture, and this means that you can access the full source code as well as discuss it, come with suggested improvements, etc. As I walk you through the creation of the architecture, I suggest you keep the source code handy to check out more details.

    Ok, let's start building the architecture from the ground up! To make things really simple (remember the KISS principle), I start with an "ASP.NET Web Application" named Kiss.Data as the data tier. In that assembly I add an "ADO.NET Entity Data Model" class named OrderModel.edmx, and add the following tables from the Northwind database (renamed Customer, Order, OrderDetail, Product and Category):

    KISS Architecture Entity Model

    If we take a look at a simplified version of the generated code (in OrderModel.Designer.cs)...

    namespace Kiss.Data
    {
       
    public partial class OrderEntities : ObjectContext
        {
           
    public OrderEntities(string connectionString) :
                base(connectionString, "OrderEntities")
            {
               
    this.OnContextCreated();
            }
           
    public ObjectQuery<Customer> CustomerSet
            {
               
    get
                {
                   
    if(this._CustomerSet == null)
                       
    this._CustomerSet =
                           
    base.CreateQuery<Customer>("[CustomerSet]");
                   
    return this._CustomerSet;
                }
            }
           
    private global::System.Data.Objects.ObjectQuery<Customer> _CustomerSet;
        }

        [
    EdmEntityTypeAttribute(NamespaceName = "Kiss.Data", Name = "Customer")]
        [
    DataContractAttribute(IsReference = true)]
        [
    Serializable()]
       
    public partial class Customer : EntityObject
        {
            [
    EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
            [
    DataMemberAttribute()]
           
    public string CustomerID
            {
               
    get { return this._CustomerID; }
               
    set
                {
                   
    this.OnCustomerIDChanging(value);
                   
    this.ReportPropertyChanging("CustomerID");
                   
    this._CustomerID = StructuralObject.SetValidValue(value, false);
                   
    this.ReportPropertyChanged("CustomerID");
                   
    this.OnCustomerIDChanged();
                }
            }
           
    private string _CustomerID;
           
    partial void OnCustomerIDChanging(string value);
           
    partial void OnCustomerIDChanged();

    ...we see that in the model, or object context, (OrderEntities) there is a class created for each entity (Customer) and a query (set) for that entity (CustomerSet). Also note that each of the entity classes and its members has the necessary decorations (attributes) to be serialized correctly over WCF.

    In the next part, I will look at how to publish the data model as a data service.

  • Currently rated 5.0 by 1 people

    • Currently 5/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    Tags: , , ,

    Architecture | Chris | Compact Framework | Windows Mobile

    KISS Architecture (part 2)

    by Chris 5. December 2008 15:55

    To continue this series that started with KISS Architecture (part 1), I will now talk more about a modern mobile multi-tier architecture. Because a picture always says more than a thousand words, let's start with a classic one.

    KISS n-tier architecture

    This image should be familiar to anyone who read the Application Architecture for .NET from back in 2002. Interestingly, the Microsoft's patterns & practices team are working on a new version of this guide called Application Architecture Guide 2.0 that is already in its second beta (there is also a specific pocket guide for mobile architectures). Building on the discussions in that guide, I have taken the old diagram (because I like the colors) and created an updated version with a small modification. The modification is related to the placing of the business entities, that I think belong in the data tier. The main reason for that is that the ORM technologies (in my case, LINQ to Entities or ADO.NET Entity Framework) take care of defining the business entities. For more details on multi-tier architectures, please read the guide, it's a great resource.

    A nice addition to the traditional diagram is to show how other systems ("External Systems") access my system's services that are published as service interfaces with WCF, and that is exactly how other systems ("Services") are accessed by my system.

    However, the biggest changes compared to my previous architecture blueprint are probably in the data tier. Even if I was a big fan of LINQ to SQL, the ADO.NET team have announced that the long-term plan is that in ".NET 4.0, LINQ to Entities will be the recommended data access solution for LINQ to relational scenarios". Therefore, I now use LINQ to Entities (ADO.NET Entity Framework) in the data tier.

    However, the changes in the data tier doesn't stop there. Another (probably even bigger) change is that the data tier is now made loosely coupled for the first time. Many attempts have been made in this direction (SQLXML to mention one) before, but I think that this time Microsoft got it right with ADO.NET Data Services. In short, it's a way of accessing data through a simple RESTful interface over HTTP using formats like ATOM and JSON.

    Even if this means that it becomes very easy to access the data source directly form the presentation tier, it doesn't mean that a sound architecture should use or even suggest such nonsense. It may be interesting and fun for quick demos, but doesn't belong in any serious enterprise solution - mobile or not. Therefore, the thinking that I introduced in A New Mobile N-tier Architecture (part 2) about keeping the data and logic as logically bound together as possible (in what I call domains) still holds. I strongly believe that the data should only be accessed through the logic.

    In the next part I will talk about the benefits of having tiers that are loosely coupled.

    Currently rated 4.0 by 2 people

    • Currently 4/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    Tags: , , , , , ,

    Architecture | Chris | Compact Framework | Windows Mobile

    A New Mobile N-tier Architecture (part 6)

    by Chris 17. October 2008 15:14

    In this part, I will get more practical by showing how the first parts of this new mobile architecture looks in code, and if you want some background, please see the previous parts:

    • Part 1 was a general introduction
    • Part 2 talked about the changes in the lower tiers (logic + data, LINQ2SQL)
    • Part 3 discussed the changes in communication (WCF)
    • Part 4 covered important stuff in the user interface (MVC)
    • Part 5 summarized the theory and outlined the new architecture

    The implemented architecture is published on CodePlex in a project called Windows Mobile Architecture Blueprint, and this means that you can access the full source code as well as discuss it, come with suggested improvements, etc. As I walk you through the creation of the architecture, I suggest you keep the source code handy to check out more details.

    nwdatacontext Ok, let's start building the architecture from the ground up!  To make things really simple (remember the KISS principle), I have started with a plain class library called Blueprint.Domain where I will keep my business domains. My first domain will be covering a small part of the classic Northwind database. I started by creating a LINQ2SQL data context called Northwind and dragged two of the Northwind tables into it (if you're like me, and don't have the Northwind database installed, you can get it here) as you can see on the right (note that I have removed the Picture field in the Category table to save bandwidth).

    When that is done, a neat trick that will come in very handy is to select Properties on the design canvas of the data context and set Serialization Mode to Unidirectional. By doing this simple task, the code generated for the data context will include the necessary decorations (attributes) to make the data context ready to be published by WCF. If we look at a stripped version of the generated code (in Northwind.designer.cs)...

    public partial class NorthwindDataContext : System.Data.Linq.DataContext
    {
        [DataContract()]
        public partial class Category
        {
            [DataMember(Order=1)]
           
    public int CategoryID
            {

    ...you will see that each class (entity) has a DataContract attribute, and each field (attribute) has a DataMember attribute. These attributes will be use by WCF when the entities are communicated.

    In the code above, please also note that the generated class for the data context (NorthwindDataContext) is declared "partial" which perfectly matches our intention of implementing the business logic in a parallel class. By creating such a class (Northwind.cs), and putting in some simple code like this...

    public partial class NorthwindDataContext
    {
       
    public Category[] GetCategories()
        {
            return Categories.ToArray();
        }
    }

    ...we have implemented the first (however sparse) service into our first business domain, and the functionality is that it returns the list of all categories. Note that behind the scenes, LINQ comes into play, and to show a more obvious example, we can do this instead...

    var q = from c in Categories select c;
    return q.ToArray();

    ...and this will create the same result as the code above. Worth mentioning already is the fact that even if the Category entity holds a relation to all products in that category, those child entities are not loaded by default. This is a good thing as you probably don't want to load all products with all categories, but when you want to load related entities, you can do this by explicitly call...

    var q = from c in Categories select c;
    Category[] categories = q.ToArray();
    categories[0].Products.Load();

    ...to load all the products for the first category. There are some other options for controlling how related entities are loaded, but it's out-of-scope for this blog post. In future posts in this series, I will cover more parts of the architecture blueprint implementation.

    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    Tags: , , , ,

    Architecture | Chris | Compact Framework | Windows Mobile

    A New Mobile N-tier Architecture (part 2)

    by Chris 22. August 2008 14:59

    To continue this series that started with A New Mobile N-tier Architecture (part 1), I will talk more about the changes in a modern mobile multi-tier architecture.  We can see that the most important changes are happening on two levels, and the first is the tighter bond between the business logic and it's data.

    classicntierBefore, we had a distinct separation in the two lower tiers between the logic and the data. The thinking was that the data services could be used by several parts of the business logic to handle basic CRUD operations. This is shown in the Application Architecture for .NET from 2002 that you see on the left where the business logic and the data access was divided into the lower two of the classic three tiers. However, the problem was that data without logic is almost useless, and even if the logic existed (in various business components) the natural relation with the data was not manifested.

    The other change is the change in the way that clients exchange information with the server (or more general, the way that two systems or peers exchange information), and that will be the focus of the next part in this series.

    newntier

    If we look at technologies like LINQ and specifically LINQ2SQL (LINQ to SQL) the traditional separation between the logic and the data is encouraged. On the right you find an illustration what is happening in the lower two of the traditional three tiers. The observant note that the business entities is moving down to he data tier (where they belong), but more importantly, the two tiers are actually moving together. With a LINQ2SQL, the business logic can be implemented in the same data context (the ORM) class (by using the ability to define partial classes). I would like to call these merged entities domain components as they define and handle both the data and the logic for a specific business domain. So, the service interface (facade) would actually work with a number of domain components to deliver functionality to its clients. The challenge is how the functionality (logic + data) is spread among the domains to make them a perfect tradeoff between versatility and reusability. The bigger, the more versatile, the smaller, the more reusable. The general message is probably to keep them small and distinct.

    In upcoming posts, I will continue with more of my thoughts on the changes in mobile architecture, and of course also some code samples to show the theory in practice.

    Currently rated 5.0 by 1 people

    • Currently 5/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    Tags: , , ,

    Architecture | Chris | Compact Framework | Windows Mobile

    Powered by BlogEngine.NET 1.4.5.0
    Theme by Mads Kristensen