Introduction to .NetTiers By Thomas Oscarson
.NetTiers is a free set of code generation templates What is .NetTiers? .NetTiers is a free set of code generation templates Uses the Microsoft Enterprise Library Application blocks Which provides reusable application blocks: Data caching Data access Exception handling Logging, etc Requires CodeSmith for generating the code
What is CodeSmith Tools? CodeSmith is a code generation tool Designed to reduce the need to write repetitive code To provide a consistent style through the use of templates which you provide (.NetTiers is just one set of templates) Gives you the ability to create your own templates Syntax is similar to ASP.NET
Why use .NetTiers with CodeSmith? You can use .NetTiers and CodeSmith to generate multiple tiers of an application automatically Ex: The database, server and browser/client tiers Database level changes are easier to accommodate, with less rewriting of code Repetitive coding is greatly reduced, .NetTiers does it for you by providing a strong set of templates .NetTiers will even generate unit tests The templates are open source so you can always include custom code templates or make changes
.NetTiers Generated DAL .NetTiers can generate a data access layer for you given an existing database In CodeSmith you just need to point .NetTiers to the database and it can automatically generate code to use that database Generate enumerators, entities, relationships, collections, etc Using strong typing
Say you want to have an enumerator for UserType Enumerators Say you want to have an enumerator for UserType .NetTiers can generate a table for you provided: There is a table for UserType in the database The table has a primary key and a unique key defined as the first and second columns in the table, and optionally a description column may be defined The first column is used for the values of the enumerator (usually an INT identity column), the second column is used for the names The different UserTypes which you want to have in the enumerator are already added to the table
Enumerators continued Tell .NetTiers that the table is an enumeration table Generate the code .NetTiers will produce an enum called UserTypeList, which will have all the values present in the database at that time If the enumerator ever needs to change: Make the changes to the table Re-generate, and the enumerator will reflect the changes
Entities .NetTiers entities are object oriented representations of database tables Say you have a User table, an entity from that table would be one row representing one user The User entity would have all the values for that row, for example UserId, Username, FirstName LastName, and PhoneNumber, each of which are strongly typed Individual entities can be manipulated and changed without affecting the database Entities have a state called the EntityState, it can have any of the following values: Added, Changed, Deleted and Unchanged
Entities can be cached by .NetTiers Entities continued Entities can be composed of other entities or even collections of entities Ex: A document entity may contain a collection of note entities View objects are not entities, they are similar but they do not have a state since they do not persist in the database (you cannot write to a view) Entities can be cached by .NetTiers Entities are generated in their own project
Datarepository and Providers The Datarepository is what is responsible for loading and saving data There is only one Datarepository, but many providers Providers are generated for each type of entity Ex: The Document table would have a provider called DocumentProvider
Providers expose a common set of methods such as Read: GetAll(), GetByPrimaryKeyID(), GetByStoredProcedure()… Write: Insert, Update, Delete, Save… These methods are only a starting point Ex: To get all Documents written by Tom, you might use: DataRepository.DocumentProvider.GetByAuthorName
.NetTiers will discover stored procedures based on the name In order to do the last example a stored procedure would have been needed By default .NetTiers will not generate a method or procedure to GetByColumnX, you need to define a procedure to do so The procedure also can take multiple parameters, you are not limited to just one Ex: GetByFirstNameAndLastName might be desirable .NetTiers will discover stored procedures based on the name Ex: If the stored procedure starts with a table name it can be included in the generation
When you do the actual generation .NetTiers will generate a report Generation Report When you do the actual generation .NetTiers will generate a report Telling how many objects it create How many errors (hopefully none) Quick configuration information Sample API usage Then details of the files it generated (this is where you would see where any errors may have occurred)
Demo / Questions