The differences between SharpLite and SharpArchitecture 1.6

I am currently writing a small simple website in order to try out SharpLite. SharpLite is a new framework which is designed to cut out a lot of the things from Sharp Architecture that most projects would not use. My site has about 6 maintenance pages, a few miscellaneous pages, an interface to a web service, 10 database tables and no out-of-the-ordinary business logic other than a rather convoluted data search mechanism. These are my notes about the practical differences (for our team) between SharpLite and Sharp Architecture 1.6. I’ll update this if I come across any other glaring differences.

For my team I am not going to recommend trying to update existing Sharp 1.6 projects to SharpLite, because as you will see a lot has changed and some things would not map to the new version very well. Upgrades will have to be to Sharp 2.0 instead which hopefully still supports the things we have done in 1.6 (looks like it does – with reservations). However SharpLite looks good for small, new projects.

My notes:

Slightly different project layout. ‘Core’ and ‘Data’ are replaced by ‘Domain’ as the data layer has almost disappeared (see later). ‘ApplicationServices’ is replaced by ‘Tasks’ and includes ViewModels by default. Controllers has been merged into the Web project. There is new Infrastructure project which just contains a few config-type classes.

No NHibernate.config file. Connectionstring is now in Web.config, and the initialisation is done in Infrastructure..NHibernateProvider.NHibernateInitializer (might need to change SQL Server version here).

If you want the auto-generated database tests to run you need to add your connectionstring into the App.config file in the Tests project too.

No bespoke repositories, instead just use queries on the IRepository. To be fair you can do this in Sharp Architecture too but we didn’t. Testing of queries does not require RepositoriesTestBase, which is lucky as that does not exist anymore. Instead just create an IQueryable() list of the entities you need. Your bespoke queries should be recognised by the Tests project automatically as they are just extensions to IQueryable().

No Fluent mapping. Generally speaking you need to use conventions for table and column names. You can override it but it is a little tedious and uses NHibernate’s ModelMapper instead of Fluent, which is a slightly different syntax. By default their convention for a foreign keyed column is to have an ‘Fk’ suffix which is unusual – the convention is usually ‘Id’ wherever I have worked or whatever I have read. So if you want to change it go to Infrastructure..NHibernateProvider.Conventions.

No Check class and no PreconditionException class which is slightly annoying – I would have thought these were pretty basic. Had to write my own replacement.

Routing is now just in Global.asax rather than RouteRegister class – I expect to fall into line with vanilla MVC.

They have provided a non-mandatory basic CUD service class (in the Tasks project) that you can extend for each entity which is useful. They suggest separating querys from CUD tasks though. I’m not entirely sure why, I think it’s just the latest fashion. It means that if you follow this pattern (you don’t have to), you need to inject both the IRepository and service class into your controller rather than just the service class. Also, this CUD service class needs to be injected as a concrete class rather than an interface. I can see that this is reasonable most of the time, but I can also see the odd scenario where this might be a pain. I just went with their method for this project to see how it went.

If you do create new interfaces for dependency injection then you now need to explicitly declare their mapping to concrete instance in Infrastructure..Init.DependencyResolverInitialiser which is less convenient than Sharp Architecture but then I don’t suppose they are expecting many more interfaces if you follow their ‘CudTasks’ pattern, and are not having to interface with stuff outside the application much.

A new MVC3 thing (as opposed to SharpLite) is Razor view engine which is the default when creating a view now. I’m not sure what advantages this gives you; I think it is just a different syntax, although presumably there was a better reason than that for spending time developing it. Seems to just be a matter of personal preference. There also seems to be a pretty random bug with the paste when editing a .cshtml file – in that it can take 20-30 seconds to paste. No obvious pattern. You can still use the old ASPX view engine but by default SharpLite creates a few Razor files for you so you are being pushed in that direction.

Need to add a reference to SharpLite.Domain dll into Tests project. I think that ideally this should be added for you.

Need to add a reference to Rhino.Mocks into Tests project. Can’t remember if you need to do this in Sharp Architecture or not, but it would be useful if it was there to start with. I suppose this is where the ‘Lite’ kicks in though – it only takes a second to install Rhino via Nuget if you need it.

Need to remember to include the ajax javascript file in your layout (master page) or view in order for the Ajax helpers to work. This is a different file than MVC2, called jquery.unobtrusive-ajax.min.js. Depending on where you put it, it might be best to wrap this include with ‘!Request.IsAjaxRequest()’ too, otherwise the Ajax calls will get slower and slower and…

If you follow their CUD pattern you need to remember about MVC conventions when posting the table data. Your controller post method needs a parameter of your Entity type, however the View uses a ViewModel that has a property of your Entity type. The parameter to the controller post method needs to be the same name as your property or it will not post anything – note that you cannot post the ViewModel itself if you want to use the HasUniqueDomainSignature annotation on your Entity.

I found a bug with HasUniqueDomainSignature annotation fairly early on in my project: if you have a validation error from an annotation on an Entity, and that Entity has another entity as a property (i.e. signifying a foreign key link between tables), it will trigger the HasUniqueDomainSignature error message from the linked Entity, overriding the one that should be shown, which is confusing! This has been fixed but not officially released yet. You need to download the latest version via the ‘zip’ button on one of the code pages and copy the binaries into your project if you come across this problem. Only a problem if you are using HasUniqueDomainSignature though!

Updated: When you deploy your site you will also need to add the MVC assemblies to your site’s bin directory (if your server does not already have them installed). Sharp Architecture does this automatically but Sharp Lite does not. In order to make sure I had the .Net 4 assemblies I downloaded Sharp Architecture 2.0 and in the ‘Referenced Assemblies’ folder you will find all the relevant assemblies.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: