Journal of a software dev

September 12, 2009

Microsoft Sync Framework

Filed under: Uncategorized — Tags: , , , — Michael Cromwell @ 6:52 pm

I recently had a requirement to move backups from a server’s local filesystem over to a network location now my first thoughts were of a windows service and a FileSystemWatcher watching the local directory and upon a folder/file being changed copy the file over to the network, however once you start digging into the details you find it isn’t as straight forward for one thing if a backup file is deleted on the server to free up space (ony the last month’s worth for example) this should be reflected on the network end plus there’s also file renames, locked files etc…

Anyhow I googled around and found the MS Sync Framework which is specially created to deal with these scenario’s and out of the box comes with providers for dealing with file synchronization great stuff! The framework is pretty big as it has extension points to let you do stuff with custom providers (for other transport synchronization), custom data transformation, metadata stored items etc… however for what I wanted I barely needed to scratch the surface.

In it’s most basic form you end up with the following code:

FileSyncProvider sourceProvider = null ;
FileSyncProvider destinationProvider = null ;

try
{
    sourceProvider = new FileSyncProvider(
        sourceReplicaId, sourceReplicaRootPath, filter, options);
    destinationProvider = new FileSyncProvider(
        destinationReplicaId, destinationReplicaRootPath, filter, options);

    destinationProvider.AppliedChange +=
        new EventHandler <AppliedChangeEventArgs>(OnAppliedChange);
    destinationProvider.SkippedChange +=
        new EventHandler <SkippedChangeEventArgs>(OnSkippedChange);

    SyncOrchestrator orchestrator = new SyncOrchestrator ();
    orchestrator.LocalProvider = sourceProvider;
    orchestrator.RemoteProvider = destinationProvider;
    orchestrator.Direction = SyncDirection.Upload; // Sync source to destination

    Console .WriteLine( "Synchronizing changes to replica: " +
        destinationProvider.RootDirectoryPath);
    orchestrator.Synchronize();
}
finally
{
    // Release resources
    if (sourceProvider != null ) sourceProvider.Dispose();
    if (destinationProvider != null ) destinationProvider.Dispose();
}

There is one gotcha that caught me out, the MS Sync Framework will not do any of the scheduling of when the synchronization is performed that is completely dealt with by you, so in my case I was back to using a FileSystemWatcher to watch for file changes in the source folder and once a change was detected I would then tell the SyncOrchestrator to synchronize this seemed to work fine and was not that much extra work. The only other step was to wrap this functionality up to be used from a windows service and that was it! Overall I would recommend anyone looking to perform synchronization to take a look to see if either there’s something out of the box the framework gives you or if it can give you a head start.

June 24, 2009

C# 3.0 Language Features Presentation

Filed under: Uncategorized — Tags: — Michael Cromwell @ 7:54 am

Update: Source code now available from here http://code.google.com/p/mcromwell/source/browse/#svn/trunk/3_0_Features get hold a good SVN client to checkout, such as TortoiseSVN

Yesterday evening I put together a presentation for the Isle of Man devG, I think it went ok overall considering this was my first presentation.

I have yet to make the source code available but once I get round to uploading it I will update this post, If anyone attending has any question’s feel free to drop me an email or leave a comment.

February 12, 2009

UML & Design Anti-Patterns

Filed under: UML, Uncategorized — Tags: — Michael Cromwell @ 9:47 pm

Intro

First off it’s worth saying that I use UML a lot and glad that we have a somewhat standardised way of communicating  our models, this post is not a dig at UML but rather some anti-patterns I have come across in relation to people using UML.

Where’s the Behaviour

When we are thinking about objects we should be thinking about the behaviour an object performs, what messages does it accept, other objects it uses perform it’s behaviour etc… And yet time and time again all I see is class diagrams this is no good, class diagrams are static and don’t provide us with enough information to show it’s behaviour instead you should start with dynamic diagrams i.e. collaboration diagrams or sequence diagrams these provide us with a much better way to model the behaviour of our objects:

user repository

If we compare the two diagrams we can see that the class diagram makes us perceive that the object sits on it’s own and exposes some messages it accepts, however in the sequence diagram we have already began to think about what other objects will use the object were designing and the collaborating objects it uses this gives us a much better idea about how we can start to implement the design.

Michelangelo Effect

UML should be used to go through quick design examples, not to create a work of art! You should not be getting hung up on every little semantic detail (should this line be dotted or filled, do I need to underline this etc…) as long as the general rules of the model are followed that’s enough. Don’t spend time trying to stick to every UML specifications you won’t get any benefit from doing so, this one of the reasons I prefer to use a whiteboard or simple pencil sketches rather than  UML tool such as visio as it’s conformity to every single rule slows you down.

Modeling the World 

You should model within a reasonable context, not try and model the system as a whole. We use UML to go through some designs of the system and in these cases we are concentrating on a particular context of the system, when we use UML we should be modeling only the items that make sense for this context once we start modeling the whole system we get bogged down in details that hinder rather than help us work out the design. This is somewhat related to the above anti-pattern in that we start treating UML models more than just sketches to help with designing.

July 11, 2008

Using Repositories with nHibernate

Filed under: nhibernate — Tags: , , — Michael Cromwell @ 8:02 pm

Intro

The point of this article is to demonstrate 2 ways that I have been using the Repository pattern in conjuction with nHibernate to provide my applications with rich domain objects, I wanted to also get feedback to the options shown and if there are any other ways to implement Repository with nHibernate.

Repository manages session

The first option and the one I used for the Staff Intranet and for older applications is wereby the repository manages the session object and decides where transactions should be used, here is an example of one the methods that we might typically see:

public int Save(User user)
{
    ISession session = NHibernateSessionManager.GetSession();
    ITransaction transaction = session.BeginTransaction();
    try
    {
        session.Save(user);
        session.Flush();
        transaction.Commit();
        return user.Id;
    }
    catch
    {
        transaction.RollBack();
        throw;
    }
    finally
    {
        NHibernateSessionManager.CloseSession();
    }
}

This has a number of the issues:

  • If you need to use a number of repositories in a single transaction this option will not work
  • Integration testing becomes more difficult as the repository handles the session and transactions

Caller manages session

The other way we can implement repositories is to let the calling code manage session and when transactions take place, here is an updated version of the above to accomodate the caller managing session & transactions:

public int Save(User user)
{
    ISession session = NHibernateSessionManager.GetSession();
    session.Save(user);
    session.Flush();
    return user.Id;
}

Our code has been reduced however the caller would now contain the code removed:

public int SaveUser(User user)
{
    ITransaction transaction = NHibernateSessionManager.GetSession().BeginTransaction();
    try
    {
        int userId = userRepository.Save(user);
        transaction.Commit();
        return userId;
    }
    catch (Exception thrownException)
    {
        transaction.RollBack();
        //... log exception, wrap up exception, rethrow, etc...
    }
    finally
    {
        NHibernateSessionManager.CloseSession();
    }
}

We could now quite happily make calls before or after the saving of the user to other repositories knowing that they would all be using the session & transaction.

I’m pretty convinced that this option of having the caller responsible for manageing session & transactions is a better way of implementing repositories, however if you of any other ways then please point me in the right direction :-)

Helping Integration testing

One thing that I didn’t like having to do was performing integration tests against the database the reason being the extra care that needed to be taken to make sure that data was reverted back to the state it was to begin with, by using the caller in charge of session & transactions approach you can rollback any data changes made.

June 6, 2008

Looking beyond blame

Filed under: Uncategorized — Michael Cromwell @ 8:09 pm

While sat eating my lunch at the Software Architect conference I overheard a brief conversation between someone who seemed to be in charge of a project possibly Software Architect or possibly Manager, anyway the bit that caught my attention went something like this

 …I only just found out that one of the developers has bypassed the middle layer…

The next bit of the conversation went along the lines

…When I get back I’m going to bring all the developers in and instruct them that they must go through this layer…

Now I have some big issues with this, firstly would it not be more constructive to ask the developer why they didn’t go through the certain layer he was talking about first?

It may be because the architecture of how that layer is implemented does not lend itself to the functionality that the developer was working on chances are that either the implementation or the processes around it are restricting or slowing down the ability to deliver functionality hence the need to find alternatives (ideally you want to have low viscosity, when viscosity is high the right changes to software are more difficult than just doing kludges, that indicates you have big problems!). Out of this may come a refactoring that has a positive impact on all the developers that develop for the system.

My second issue is this, say that the middle layer that was bypassed turns out to be the fault of the developer (s)he decides (s)he will instead go directly to the database (who needs all this business logic anyway, the data is what were interested in ;-) ) if this is the case then I would suggest bringing the developer up to speed on why these things are important, explaining and showing examples of things such as being to write a unit test without having to hook up the database because it has been nicely decoupled may give them the aha! moment were it falls into place.

I guess both of this issues point to the fact that before any digging into the problem was performed a blame exercise that ain’t really going to help anyone or the system going forward has been organised.

 

June 5, 2008

Afterthoughts of Software Architect

Filed under: Uncategorized — Tags: , — Michael Cromwell @ 6:44 pm

I have come to the end of the 3 day conference and what are my thoughts:

  • Firstly I believe to get the most out of the sessions you need to have the right mindset, if you regard being a developer as from 9 till 5 then switching off and not having a keen interest (being passionate to coin a phrase from JP Boodhoo) in learning new things I don’t think you will get much out of it.
  • It has confirmed a lot of what I’m doing and the patterns & practices I have used for the Staff Intranet project are the right way to do things, In the last session I managed to a a quick chat with the speaker Dave Wheeler and was telling him about how I’m decoupling data access concerns from the domain model using the repository pattern with interfaces and was told this was the right way to go.
  • This morning I was in 2 sessions with Neal Ford from ThoughtWorks as the speaker they covered design patterns in dynamic languages & meta programming in Ruby and I must say that I’m still in shock at the power this language has! It will be very interesting when IronRuby is fully available.
  • Really try to go to either the Software Architect or the Dev Week especially if your like me and stuck somewhere where there isn’t the software community like you may get in the big cities, leaving the sessions to one side the interaction with other people in the same field whether they’re Software Architects, Lead Developers, Software Analysts etc… is another great aspect and can give you an idea how things differ from what you might be used to.

I have really enjoyed the last few days and could definitely see me going to another when the chance arises, now if only I can convince the business :-)

June 1, 2008

Software Architect 2008, London

Filed under: Uncategorized — Michael Cromwell @ 3:28 pm

I will be off the rock for a few days to attend Software Architect 2008, this will be the first time I have been to any software development conference and this one looks to be huge! I wouldn’t class myself as an architect however I’m really enthusiastic about the topics covered in most of the sessions available and the speakers at this place are the real deal!

The only downside is that I miss the TT over here, but it’s a small consolation in comparison :-)

May 18, 2008

Giving the staff intranet a lick of paint

Filed under: screen design, styling — Tags: , , — Michael Cromwell @ 10:01 pm

Over the weekend I made some code changes to the staff intranet project once these were out of the way I turned my attention to the look and feel, I have never really been that confident about doing design and styling work but I thought I would take up the challenge to try and make the staff intranet look better, here are the results:

Before

 

After

 

What do you think?! The mockups were done in paint.net

Update: this updated v1.1 is now on codeplex

Blog at WordPress.com.