Thursday, October 12, 2017

NetTeirs and Reflection

By now NetTiers is a rather aging templating system.  Many people have complained that it is not being actively maintained and that there are a lot of errors in it, and to some degree they are correct.

NetTiers gets random updates from those using it, but not very frequently.  The code is definitely not bug free, however it is not chalk full of bugs either.  It does rely on Microsoft's EnterpriseLibrary sweet, which is no longer maintained; although the documentation claims this is not a requirement.

But NetTiers has several key features and functionality that had me pick it up after testing multiple data access layer solutions.

  • It generates a direct copy of your database into C# code with just the click of a button.  Very few other data access layers can claim that, and their solutions do not feel as smooth as the Codesmith platform that NetTiers runs on.
  •  During the generation process NetTiers standardizes the table and column names found in your database, so they all start capitalized, words it identifies are capitalized, and table prefixes can be optionally stripped off.
  • It uses meta data from sql regarding indexes and foreign keys to auto generate methods to get data out of the database.
  • That meta data also allows links to be made between objects in the generated c# code so you can load and reference foreign key relationships easily.
  • Stored Procedures can be auto generated for those who want the added security and to avoid dynamic sql.
  • It has a reasonably powerful parameterized sql engine when required.
  • A caching layer allows the developer to cash most result sets that come back from the database without additional work.  Which can be useful for small static tables.
  • It picks up custom stored procedures and adds methods onto the generated objects to allow the developer to call them without any additional effort.
  • There are quite a few template options to allow the developer to customize the resulting code to their environment.
Many data access layers claim features similar to these, although oftentimes with less control over them.  However, there are three features that make NetTiers unique and keep it valuable and relevant:

The direct copy of the database into standard c# code takes a huge load off of the developer who no longer has to write all that plumbing code.  The generated code is in the form of normal C# objects that match the names in your database.  This means there is no guesswork as to what an object name is going to be.  It also means full object oriented and intellisense capable access to every object in the database.

Generated code is compile time safe; so most errors in the database will show up right away and not allow the code to compile.  This code has been used by many people lots of times, so most major bugs have been worked out already.  Having it all generated the same way every time nearly eliminates the ability for a random error to sneak into the data access layer.

One of my favorite features is actually a byproduct of having the data access layer so standardized.  The ability to use reflection heavily.  Because there are so many standards built into nettiers, you can always find the correct get or save methods or primary keys regardless of the object you are working with.  The generated code in these areas all follows the same pattern.

While not perfect ( I have submitted a few corrections back to the code base myself ), the ability to auto generate an entire DAL without effort, and the ability to rely heavily on reflection when using these objects, has made me a believer in NetTiers for many of the projects I work on.

It is not the only solution available.  But for server based C# projects it definitely does shine as a way to drastically reduce development time.