Tuesday, November 1, 2016

Guide to Custom Database Access in Umbraco 7 With Built in PetaPoco

Though there are a lot of ways to write custom database code, and in many place in Umbraco custom tables my no be needed as the CMS can handle most any custom data needs. There are reasons and use cases for using custom database tables and code including:
  • Reading data from external systems.
  • Large datasets that we don’t want to clog out cache’s and Umbraco tree with.
  • Performance sensitive querying where the Document -> Property models setup may not be preferable.
  • Complex cross-joins and many-to-many relationships that may not be easy to replicate in Umbraco.
  • Customer data, log data, or “write once” style form submissions where the data need not be in Umbraco trees.
  • Data that may need to be easily accessed from external systems. 
    • They can also use public Web API methods or custom views that read from the Umbraco core tables but writing data this was it less safe.

Though there are a lot of ways to do the database code and everyone has their own preferences, this guide is using the same PetaPoco system that Umbraco uses internal.  This has a few key benefits:
  • Very lightweight system yet powerful.
  • Fully compatible with Umbraco models, methodologies, and serialization.
  • Easy to implement and generate models.
  • All code is using existing database code already present in the Umbraco Core to minimize overhead.

Monday, October 31, 2016

Guide to using Strong Typed PublishedContnetModels in Umbraco 7

The recent Umbraco 7.4 release there is an included Model Builder tool.  This may not seem like a big deal but it can be very helpful to developers and I recommend embracing it.  In essence what it does it let you generate strong typed models in .Net based off your Document Types and Media Types and their hierarchy.  This can be done a few ways including at runtime in memory or with pre-compile DLL’s.  Then in Views, Macro’s and Controllers these strong typed models can be used instead of normal code to reduce potential coding errors and typos, and to provide IntelliSense in Visual Studio if you do editing there.  These changes for the most part are non-breaking and the old style can still be used.

In short if will replace the first example template with the second.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@Model.Content.GetPropertyValue(“myProperty”)
@(Umbraco.Media(CurrentPage.myImage).umbracoFile)
@Model.Content.GetPropertyValue(“myProperty”, true, “Missing”)
@Umbraco.Field("myProperty ", recursive: true, altFieldAlias: "defaultProperty")

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<DocumentType>
@Model.Content.MyProperty
@(((Image)Model.Content).MyImage.UmbracoFile
@Model.Content.GetPropertyValue(s => s.MyProperty, true, “Missing”)
@Model.Content.GetPropertyValue(s => s.MyProperty, true, Model.Content.DefaultProperty)

This article describes the following approach:  Generating C# models in the live Umbraco Instance and then pulling them down into the generated models down into the source Visual Studio Project and pre-compiles them there in the main application DLL.  This has some major benefits over other approaches including:
  • All code is pre-compiled and no generation or compilation is happening when the website is actually running or starting up.
  • Models generation isn’t dependent on IIS Express and you don’t have to do desin work there or start up slow projects to manually create models.
  • Models are generated the same place you edit the document types and generated automatically.
  • Models are partial class’s that can be extended and can implement custom interfaces as needed.
  • There is no need for any special visual studio plugins or deployment rules to make this work.  All parts described will work on any Visual Studio Editions.

Wednesday, October 26, 2016

Removing Double Redirects or Redirect Chains form .htaccess files.

If you manage and large site using mod_rewrite or ISAPI_Rewrite 3 you will eventually run into issues maintaining the rewrite rules. After doing a few redesigns or moving things around repeatedly you can end up with situations where one page redirects to another page then another and so on in a chain. This is functional but inefficient, it can have a negative effect on user experience and on SEO value, and makes everything harder to maintain. This tool is a simple script you can pass you .htaccess file through to remove all these multi-step redirects and all pages redirect immediately on the first redirect to the final destination page.

Tuesday, October 25, 2016