Archive for the ‘Architecture’ category

Super Tuesday!

November 17th, 2009

One of my coworkers recommend combining some posts together than one a bunch of ‘one offs’ of links or news of interest.

I’ve accumulated a set of links, thoughts, experiences, etc… over the last few days (I’ve been busy). 

New Sharp Architecture Release

http://devlicio.us/blogs/billy_mccafferty/archive/2009/11/12/s-arp-architecture-1-0-2009-q3-with-nhibernate-2-1-1-ga-released.aspx

Basically Sharp Architecture is about as close as you can currently get to a Spring/Hibernate MVC architecture in .NET.  My last project used several pieces of Billy’s previous ‘NHibernate Best Practices’ that I retrofit for asp.net mvc.  Then, Billy helped put together Sharp Architecture!

A snippet of what Sharp Architecture is at the wiki:

Pronounced "Sharp Architecture," this is a solid architectural foundation for rapidly building maintainable web applications leveraging the ASP.NET MVC framework with NHibernate. The primary advantage to be sought in using any architectural framework is to decrease the code one has to write while increasing the quality of the end product. A framework should enable developers to spend little time on infrastructure details while allowing them to focus their attentions on the domain and user experience. Accordingly, S#arp Architecture adheres to the following key principles:

  • Focused on Domain Driven Design
  • Loosely coupled
  • Preconfigured Infrastructure
  • Open Ended Presentation

 

SpringSource updated to 2.2.1 includes new GSP Editor

I updated my SpringSource (STS) – (see more below about STS + Grails)
The update to 2.2.1 is here: http://www.springsource.org/node/2172

Grooy and Grails Random Thought

As a very well done project for building web applications…what is it’s popularity out in the world?  Are companies afraid of it?  Take NHibernate in the .NET world, in spite of it’s best of breed position, companies would rather use less to not use open source (the ‘it’s not from MS position’).  Is Groovy and Grails popular out there ?  If you do Grails/Groovy development – ping me please – let me know where your at, how you like it, etc… I’d like to see if it’s made much of a dent out there!

What I’m reading: ‘Grails in Action’ : Some Thoughts While Reading

 
Shout out to Manning for this e-book, love their setup, easy to purchase, prices are fair, many up and coming books on topics you don’t see in the mainstream yet!

Chapter 1 – good start.  Steps through the creation of a sample grails app.  Covers the basic commands.  Only issue I had was using the ‘file db’ – so I setup Grails to run on MySQL, which was a good exercise.  Tests still run in memory, which is good.  Final part was using prototype with taglibs to do some simple ajax.  I like how it starts by not bogging down into details, but getting something up and running.    

Highlights:

  • convention over configuration
  • removal of much of the xml configuration
  • testing built in.
  • Domain focus, code generated is clean and ‘pojo’.
  • Built in common functionality ie. prototype.
  • Good helper support with tablibs.

Chapter 3

…Chapter 2 was a nice overview of Groovy.  I’m now moving on to ‘Modeling the domain’:
This chapter covers
â–  What GORM is and how it works
â–  Defining domain model classes
â–  How domain classes are saved and updated
â–  Techniques for validating and constraining fields
â–  Domain class relationships (1:1, 1:m, m:n)

STS and Grails

http://www.grails.org/STS+Integration
Nice setup I’m using, shows how to create or import an existing Grails project into the workspace

I like the way that Grails provides integration tests as part of the framework that automatically handle transactional rollbacks.

I like the integration that STS does provide for Grails.  But I do think doing the work from the command line is faster and more efficient.  After adding new items, just refresh the Eclipse project and it’s there.  This is especially good for quickly running tests.

Grom

One thing I see in other frameworks (ie. asp.net mvc) is the concentration on views and controllers.  Whereas in Grails, I think the pattern all starts with the domain model.  Generate the model which has what I would call ‘built in DAO support’.  GROM makes life easier  :)

Validation

Grails validation is very well done, it’s built on a ‘constraints’ model with a built in validation DSL.  From Grails in Action:

static constraints = {
        userId(size:3..20, unique:true)
        password(size:6..8)
        homepage(url:true, nullable:true)
    }

This says ‘userid must be unique (in db) and be between 3 to 20 characters long’  – nice eh?

 

Software Craftmanship Manifesto


Did some pair programming last couple of days.. it was fun to say ‘whoa, this needs a new class here!’, refactor, make it more readable, etc… Must have been the next day I was shown this manifesto.  The only part that is still missing is the testing part, although some of the code is using legacy code that doesn’t lend itself well to testing… nevertheless, there is a real sense of accomplishment and success in writing code when there is craftsmanship put into it… so this manifesto is spot on!

http://manifesto.softwarecraftsmanship.org

 

How to start agile development in your team:

http://weblogs.asp.net/rosherove/archive/2009/11/16/how-to-start-agile-development-in-your-team.aspx

Thanks to Roy Osherove, as my current client was recently asking it’s team ‘are we agile’… From the link above:

    1. start doing daily stand up meeting
    2. start doing automated builds
    3. start working in pairs on relatively complicated problems
    4. start doing code reviews
    5. start showing visible progress
    6. start trying to show demos every two or three weeks
    7. start learning unit testing
    8. learn how to code better, design better
    9. start doing TDD

As a company, there are maybe 2 items here, as a team, more like 50% ?  I will say this: more and more it’s about the culture.  Happens everywhere, even on ESPN radio there was talk about the ‘culture’ on a team in the NFL, take a so-so quarterback, joins a new team, with a different culture, and he does well…(Denver Broncos) take a star on one team, move him into another culture… and he struggles…(Chicago Bears). 

More work needs to be done to promote a new culture in companies.  How to ‘sell’ to upper management who doesn’t seem to care ‘how’ you do it, as long as you go it… vs.  technical people in a company that totally reject anything new.   Where I’m at now, every new idea is really rejected, the company is fine with it’s ‘it was made here’ approach, but it is already showing it’s age and it’s big, bulky and hard to move it forward.  Some work was done to add some domain driven design concepts, add some tests, etc…  In the end… culture… If you haven’t watched ‘Pimp My Architecture’– watch it! 

Here is a related article from InfoQ:

Tips to Select a Pilot Project for Agile Adoption (InfoQ) :

One of the most important factors which influences the success of Agile adoption is the set of learnings derived by applying Agile to a pilot project. These learnings significantly influence the organization to go ahead with Agile or fall back to their usual process. This places a lot of emphasis on the selection of the pilot project. A wrong type of pilot could end up aborting, which would be a poor advertisement for the new process.

and

An engaged business sponsor can help the team if it needs to push against entrenched business processes, departments, or individuals. The time and energy of a business sponsor are critical to the success of the project…all these factors become meaningful with a strong team. Hence, choosing the right team is a precursor to all the above factors.

It takes a “strong/right” team with a mindset to embrace the agile methodology.  They later become the flag bearers for other projects within the organization.  

NServiceBus – Web Asynchronous Publishing


nservicebus includes an async pages sample.  Publishing a message asynchronous to a queue and it returned back to the page.  Get 2.0 beta – and learn how nservicebus works.  This brings to mind a lunch conversation today..SOA isn’t just ‘web services’ – there is much more to it.  NServiceBus helps provide a message based architecture.   

ie. Command Query Pattern

The command service publishes messages about changes to data, to which the query service subscribes. When the query service receives such notifications, it saves the data in its own data store which may well have a different schema (optimized for queries like a star schema). The query service may also keep all data in memory if the data is small enough.

Learn More

TCP Sockets, Chat Program

Don’t ask about this one.  I am working on some port listener code for a flying sim hobby I enjoy.

Good start …

Building a Chat Server

Building a Chat Client

… by the way… Groovy does it better  :)

s = new Socket("localhost", 8283)
s << "Groovy Rocks"
s.close()
 

(reference : http://gallemore.blogspot.com/2008/01/socket-client-for-groovy.html and Groovy Sockets: http://pleac.sourceforge.net/pleac_groovy/sockets.html)

 

asp.net mvc 2 beta released at PDC


http://haacked.com/archive/2009/11/17/asp.net-mvc-2-beta-released.aspx

Here are some highlights of what’s new in ASP.NET MVC 2.

    * RenderAction (and Action)

    * AsyncController

    * Expression Based Helpers (TextBoxFor, TextAreaFor, etc.)

    * Client Validation Improvements (validation summary)

    * Add Area Dialog

    * Empty Project Template

I hope to see some ‘grails like’ command line scaffolding and richer domain class support in upcoming versions.   In my post there, Phil says ‘Version 3’… so.. stay tuned!

I’m really glad to see Microsoft continuing this project, it’s a breath of fresh air – I have a 1-1/2 year project that is using it and I never grow tired of the technology part of it.   

LifeCycle Data Services (LDS)

 

Huh?  :)   This is one a bit out of my normal range, but I was curious about an InfoQ article talking about Flex/Flash/AIR, etc… and one of the tools they are making is LDS.   LDS is about domain modeling.

And so the way it works is that, we have a model that becomes the centre of the universe, if you want, of the server-side developer and you create a model that represents your data. You can create it from scratch or you can introspect the database as a starting point

…

There is a new plugin that you add on top of Flash Builder: that is the Model Designer. The process that I just described here is actually done within Flash Builder in a kind of a visual way; so we have a visual model editor. In that model, it’s more than what you typically do in a pure data model – you can add constraint and validation rules, you can specify things that typically you would have to specify again and again in view components

…

Once you create your model, you deploy the model and basically you don’t have to write server side code because, based on the model, we have enough to really provide a persistence layer

Nothing new here right..? Well, what caught my eye was that they are using Hibernate (my bff) !!!  So, even Adobe is hopping into the mix here :)

Behind the scenes, this is implemented using Hibernate – the persistence layer is provided by Hibernate – and then we also do some code generation on the client side, again the value objects and the service proxies. The key point is that the foundation for that is still the data management service

There you have it.  Now if we can just get Microsoft to embrace NHibernate and see the light…  :)

And last…. but not least… I saved the best for last:

Slashdot post: ‘Less than Free’

 

On the day that Google announced its new service, the stock in the two companies that had controlled the market for map data, Garmin and TomTom, dropped by 16% and 21%, respectively. (Those companies had bought Google’s erstwhile map-data suppliers, Tele Atlas and NavTeq, in 2007.)

Read more about ‘the strategy behind Google’s releasing turn-by-turn mapping for free…’ .  Wow.

MSDN Article: ‘Employing the Domain Model Pattern’

August 2nd, 2009

I’m a big advocate of domain driven design.  One of the ‘gurus’ in the community, Udi Dahan (NServiceBus creator, etc…) has written an article for the latest version of MSDN magazine titled ‘Employing the Domain Model Pattern’

Topics include:

  • Domain model pattern
  • Scenarios for using the domain model pattern
  • Domain events
  • Keeping the business in the domain

While I’m here, I’d like to make a plug for NCommon – which is a framework that helps fulfill the DDD pattern (including recent addition of domain events).  NCommon currently supports NHibernate, Linq to Sql, and the Entity Framework.  The trunk already has parts of EF 4.0 being setup as well.

This article builds off the concepts here:

‘Domain Events – Salvation’ by Udi

For usage in NCommon see ‘Domain Events pattern in NCommon

This is a very powerful way of keeping with the SRP.  One technique I hope to deploy is the ability to separate out service to service calls.

ie. EmailService handles emailing.  I don’t want to inject an ‘EmailService’ into my ‘OrdersService’

When the SendEmail event is fired, it can be handled by a handler that has an EmailService injected into it instead.

I might as well also mention an article in the same issue ‘N-Tier Application Patterns’ – although it should be titled ‘Entity Framework with WCF’  :)

This article discusses:

  • N-tier design patterns
  • Entity Framework
  • Microsoft .NET Framework 4

This article uses the following technologies:
Entity Framework, Windows Communication Foundation

Included in this month’s MSDN is ‘Entity Framework v2 and Data Access Architecture Best Practices’

Domain-Driven Design By Eric Evans

June 22nd, 2009

I must say, emphatically, that Domain-Driven Design by Eric Evans should be a required read by any software engineer in the field that is using an object-oriented language (I can’t speak outside of that yet).

"This book belongs on the shelf of every thoughtful software developer."

–Kent Beck

This book has been out for awhile – I first read  Applying Domain-Driven Design and Patterns: With Examples in C# and .NET by Jimmy Nilsson – and although it was a good starter book, in retrospect, it would have been better for me to start from the source.

To me there is a ladder of growth emerging:

1. First the goal of writing code that follows good object oriented design.  ie. loosely coupled, encapsulation, programming w/interfaces, etc…

2. Then we see patterns emerge in that code base – ‘factory pattern, strategy pattern, inversion of control’ – not even as much as ‘in code’ but as a way to ‘talk about code’.  When one dev says to another ‘we need to use a factory here’ – it’s a common language.

3. Once these start to emerge, there is an architectural awareness – a ‘how do I fit these pieces together’.  DDD by Eric Evans (and Patterns of Enterprise Application Architecture by Martin Fowler) is the next great place to explore.

What is interesting is several projects I’ve seen were driven from a technology perspective and not from a domain driven one – and have required someone coming in and ‘saving’ the project.

In my current project it’s been an evolving model, expanding and refactoring as I have learned more of the business and it’s rules.  Our jobs as developers are twofold – be experts in the technologies we work in , but more than that – to learn how businesses work – to relate to customers, etc… it’s quite a task!  Many times I see organizations attempt to conquer the problem through the technology only without really grasping the domain being modeled.  

I started with the layered approach, and honestly began to learn how to shape the parts as the knowledge of the domain began to shape and form.  Evolving is the keyword, to me though – it’s a learning process, there are times that difficult decisions are made when shaping the code, and yet when trying to stick close to first being conscious and aware of ‘how’ I’m writing the code.  Having a DDD up front, separating the domain, repositories, understanding ‘specification’, ‘factories’, etc… all produce an environment where the software can shape and grow without becoming too unwieldy.   Eric Evans helps to shape and form his experience is a way that provides insight and wisdom into domain-driven development.

(see also http://domaindrivendesign.org/ )

More on NCommon

June 4th, 2009

Once again, I’m going to talk about NCommon.  I want to share some of Ritesh Rao’s posts on concepts that NCommon incorporates.  I’m very impressed with his architecture and it helps that he provides NCommon for NHibernate, Linq2Sql, Entity Framework (and I see he is venturing into EF v2).

My last post touched on this, but I’ll review it again:

  1. Framework for implementing a Unit of Work Pattern
  2. Framework for implementing a Repository pattern that utilizes Linq
  3. Framework for implementing a Validations and Business Rules
  4. Implementation of the Specification pattern using Expressions
  5. Utility class to help store application specific data in the Thread Local Storage / Current Web Request and AppDomain Level Storage
  6. Guard class that mimics common guard statements that verify parameter values and throws exceptions if values are not acceptable.

NCommon uses the Apache License 2.0

NCommon uses the  Microsoft.Practices.ServiceLocation – this allows you to plug in your dependency inversion container of choice.  

I have gotten rid of the generic IoC wrapper implementation from the library as it doesn’t seem necessary anymore with the rally behind the Common Service Locator project.

This is shown in the accompany tests setup:

[SetUp]
        public void SetUp()
        {
            EFUnitOfWorkFactory.SetObjectContextProvider(() =>
            {
                var context = new TestModel();
                return context;
            });
            var locator = MockRepository.GenerateStub<IServiceLocator>();
            locator.Stub(x => x.GetInstance<IUnitOfWorkFactory>())
                   .Return(new EFUnitOfWorkFactory()).Repeat.Any();
            ServiceLocator.SetLocatorProvider(() => locator);
        }

NCommon sets the object context and implements the IUnitOfWorkFactory (the above shows with Entity Framework).

An example of how this is used can be found in the UnitOfWorkScopeTransaction class (UnitOfWorkScopeTransaction.cs- trunk/NCommon/src/Data)

ie. the ‘GetTransactionForScope’ method to retrieve the UnitOfWorkFactory:

 
var factory = ServiceLocator.Current.GetInstance<IUnitOfWorkFactory>();
var newTransaction = new UnitOfWorkScopeTransaction(factory, isolationLevel);
newTransaction.AttachScope(scope);
CurrentTransactions.Add(newTransaction);
return newTransaction;
 

The NCommon tests highlight the features of NCommon as well, including Fetching Strategies, Specification, Repositories, and Unit of Work.

Let’s look at some of Ritesh’s posts…

http://www.codeinsanity.com/2009/04/repository-pattern-thoughts.html

Some key items in this post to quote:

repositories should really represent a queryable data store that doesn’t abstract away queries behind methods that some unfortunate developer has to maintain and evolve, but rather allow consumers of the repository to query it directly. Hence why NCommon relies on repositories to implement the IQueryable interface to provide a query infrastructure directly on top of NCommon repositories.

I think this is an important piece to understand, and Ritesh follows up with the comment:

The approach I would take is rather than exposing the infrastructure requirements in the query object, I’d like it to take in an IQueryable and return back a IQueryable. This will allow chaining of queries by multiple of such Query objects without exposing any infrastructure concerns.

…

The problem with exposing ICriteria, or any other infrastructure component, to all layers of the application is that eventually a lot of infrastructure concern creeps into layers of the application where they don’t belong. I dislike the idea of having the expose ICriteria to the UI for adding paging and sorting on top of the query encapsulated by ICriteria.

That being said, in my opinion IQueryable is best suited for this job. Its a framework level member that is infrastructure agnostic, provides a very nice way to chain queries together and encapsulates our query requirements rather well.

» Read more: More on NCommon

Using NCommon – Unit Of Work, Repository, etc…

March 27th, 2009

 

***Update***

The NCommon codebase has been moved from CodePlex to Google Code.  You can get this code here:

http://code.google.com/p/ncommon/

Any links below have not been updated

—-

I was looking for a good repository for using Entity Framework in a proof of concept project.  I stumbled upon a project called ‘NCommon’.   The beauty of this framework is that is appears to support Entity Framework, Linq to Sql, and NHibernate.

The basis of NCommon is that it helps build a ‘domain driven’ architecture.  A summary from the link above describes the major parts of NCommon:

NCommon is a library that contains implementations of commonly used design patterns when developing applications. So far NCommon provides the following:

  • Framework for implementing a Unit of Work Pattern
  • Framework for implementing a Repository pattern that utilizes Linq
  • Framework for implementing a Validations and Business Rules
  • Implementation of the Specification pattern using Expressions
  • Utility class to help store application specific data in the Thread Local Storage / Current Web Request and AppDomain Level Storage
  • A Guard class that mimics common guard statements that verify parameter values and throws exceptions if values are not acceptable.

NCommon includes a sample website – done with asp.net mvc.  I setup a ‘test’ (it’s not really a test, more of a ‘ok, looks good, how does it work’)

My sample snippet is a call to retrieve and address entity and update it’s state:

Step one:  Register the Unit of Work and Repository with your IOC container of choice – in this case I use Castle Windsor, you can use any IOC however, since NCommon uses the Microsoft ServiceLocator.  (Here is an example done in Unity)

image

Since I have chosen to try Entity Framework, I’m registering the ‘EFUnitOfWorkFactory’ and the ‘EFRepository’ – again, you could use Linq2Sql or NHibernate here.  The unit of work factory is, of course, in charge of creating the unit of work  :)

Step Two:  This is some underlying plumbing that occurs, I’m telling the ServiceLocator what IOC container I’m using for NCommon, additionally, I’m telling the EFUnitOfWorkFactory what EF DataContext I’m using (‘BistroEntities’ is my ObjectContext for my EF test project):

image

I’ll show in a bit what happens at a asp.net mvc level, but this is the core ‘setup’ involved – this is done once in your application, after that you never look at it again   :)

Now, let’s show how I can use this ‘unit of work’ with a ‘repository’ pattern:

image

First off, I want to define in the code that I’m beginning a unit of work.  Martin Fowler tells us all about ‘Unit of Work’ :

Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems.

Obviously we want this in our application !  ( I should say you ‘need’ this – lol)

In my senario above, I’m only making one ‘save’ call, however, I could have a situation where I’m saving a collection of entities – the address, a company update, an employee update, a audit trail save, etc… This is all accomplished through the UnitOfWorkScope – it is handling the underlying Transaction as well.  The UnitOfWorkScope does a lookup to the ServiceLocator to find the corresponding type of unit of work.

Secondly, I’m instantiating a new EFRepository.  In the asp.net mvc sample, you’ll see this will be associated to a controller by using dependency injection.  It would look something like this:

public AddressController : Controller
{
       private readonly EFRepository<Address> _addressRepository
       public AddressController(EFRepository<Address> addressRepository)
       {
              _addressRepository = addressRepository;
…

(Basically, the container handles the instantiation of the repository on your behalf via dependency injection)

The entity is retrieved, updated, and saved.

Notice the Unit of work ‘Save’ call:  This save call is committing the underlying transaction.  If I left this out…the save wouldn’t occur  :)

So, how does this work in the NCommon sample website code?

Obviously the real question is, ‘how do we set this up to use in our application’:

In the asp.net mvc application you must set it up in the global.cs file – where the application starts basically: (note, the NCommon sample code uses NHibernate):

image

I’ll skip over the NHibernate specific parts here, you can download the sample, the key item here is telling the Store about the NHibernate ISession (for EF/Linq2Sql this is ‘similiar’ to your context object):

image

The next section is the registration (again, see the sample code)

image

As with above, you can see the registration of the NHUnitOfWorkFactory and the NHRepository.

These get registered along with the controller registration that asp.net mvc offers as well as the ServiceLocator registration of our container.

Lastly then we move to the controller itself, again, looking at the NCommon sample web application:

image

Since the controller is registered, and the IRepository is registered, this is ‘autowired’ to find in the container the IRepository instance and create it on your behalf.

Lastly, we use it:

image

I think this is a fantastic setup and architecture – I would probably insert a ‘service layer’ in between my controller and my repository however.  (and if you have seen my other posts, I think something like RIA.NET – which I think should be called ‘DomainServices.NET’ comes into play…eventually…)

Thanks to Ritesh Rao for his NCommon library as well as for his help in getting starting with NCommon

Architecture Layering Part II

September 10th, 2008

Back awhile ago I made this post on Architecture layering.

Having a service layer in a ASP.NET MVC is a great thing!

Initially I was wrapping Actions with transactional attributes, making calls to the Dao objects, etc… Sure, the dao objects are being injected by my IoC container (Windsor) – which by the way, is very sweet, it’s so transparent that I forget who it creating the objects… I just use the interface… (that is another story)

I show a simple example below, but this sample doesn’t capture where I’ve been able to move business logic back into services – simplifying the controllers.   (Also, it cuts back on any duplication)

Simple example:

MyCustomerController

private readonly ICustomerDao CustomerDao;

ctor : MyCustomerController(ICustomerDao CustomerDao){ this.CustomerDao = CustomerDao;}  //injected

Then on a save:

[Transaction]
public ActionResult Save(string id)
{
     …  CustomerDao.Save(Customer);
}

Instead I’ve moved this into a service layer – now the controllers know nothing about the data access layer:

public class CustomerService : ICustomerService
{
    private readonly ICustomerDao CustomerDao;
    public CustomerService(ICustomerDao CustomerDao){ this.CustomerDao = CustomerDao;}  //injected

    public void SaveCustomer(Customer customer)
    {
         using(ITransaction tran = NHibernateSession.Current.BeginTransaction())
         {
                try….
                CustomerDao.Save(Customer);
                tran.Commit();
                …

         }
    }
}

That is our service – it handles all transactions – where needed – in conjunction with the Dao objects

Now the controller gets an injected CustomerService rather than a CustomerDao.  Notice the Transaction attribute is no longer needed.

MyCustomerController

private readonly ICustomerService CustomerService;

ctor : MyCustomerController(ICustomerService CustomerService){ this.CustomerService= CustomerService;}  //injected

Then on a save:

public ActionResult Save(string id)
{
     …  CustomerService.SaveCustomer(Customer);
}

————-

As far as the IoC goes, I’m doing that all within the Global.cs

ie.

ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(Container));

_container.RegisterControllers(Assembly.Load(“Controllers”));

Then I use reflection to add the Dao’s and the Services’s to the container.  It uses Windsors ‘autowiring’ to handle creation based for dependencies.