Telerik OpenAccess ORM Model First Approach

Telerik OpenAccess ORM is a developer productivity tool used to generate data access layers for applications.  It allows software developers to quickly and easily map objects in an object-oriented domain model to relational database tables, views and/or stored procedures.

In this post I'll demonstrate the Model First approach, also known as forward mapping.  I'll use the OpenAccess ORM Visual Model Designer to create a domain model and then use the Update Database from Model wizard to create the database backend from that model.

I'm going to model a simple contacts list data structure that will contain the following four tables.  Salutation, Person, Contact and Contact Type.  The data relationships are as follows.

  • A person has a single salutation (Mr., Mrs., Miss).
  • A person has a number of contact records (Various ways to contact a person).
  • A contact record has a type (Home #, Cell #, Email).

To get started we need to create a new Class Library project in Visual Studio 2010.  Delete the default Class1.cs file that gets created and add a new folder to contain your ORM code.  For this example I simply named the folder ORM.  Right-click on your ORM folder, select Add -> New Item...  Locate the Telerik OpenAccess Domain Model template, name your .rlinq file and click Add.

New Domain Model

This will initiate the Telerik OpenAccess New Domain Model Wizard.  For the model first approach you want to start with an empty domain model.  Select your database back end, name your model and adjust your namespace if required.  Click Next.

Select Model Type

Two additional screens are presented as a part of this wizard.  One allows you to define your naming rules for classes, fields and properties.  The other allows you to select code generation templates and make changes to the output settings.  Accept the defaults for both screens by clicking Next and then Finish.

ToolboxWith the wizard now complete you're presented with a blank entity diagram that says "Drag from the toolbox to add items to the diagram".  Do just that, open up your toolbox and you'll see the items that are available to be dragged onto the canvas.

Add four Domain Class objects, one for each database table desired.  Use the diagram surface and properties windows together to model your Domain Classes to represent eventual database tables.  Do this by adding a series of properties to each Domain Class object to act as the fields of your database table.  From the properties window you can set a fields data type, whether or not it's a nullable field or if it's an identity field.  Once the Domain Objects are configured add Associations, one for each relationship in the data structure.  Configure associations the same way you would relationships of a database table.  Here's my completed diagram for the contacts list example.

Model Diagram

When saving your diagram, Telerik OpenAccess ORM generates source code for each Domain Class Object, making them working class objects usable by applications.  This is great and all, however performing a quick build will result in warnings telling us we're not mapped to database objects yet.

Buildwarnings

Being a model first approach, this makes perfect sense.  We've completed our model but have yet to transfer it to a database.  Without the database existing there are no tables for our classes to be associated with.  To apply the model to a database right click on your diagram canvas and select Update Database from Model...This will launch the Update Database from Model wizard.

The first thing the wizard requires is a connection to the database.  You can choose an existing connection, create a new one or simply provide a connection string.  I provided a connection string and named my connection.  Click Next >.

Setup DB Connection

With a connection now made to the database, the wizard can look up the available database schemas.  Select the schema you want to use and the types of objects you need to create.  This example only required tables to be created but you can also model views and stored procedures.

Selectdbobjects

The next step of the wizard lets you select which changes to apply to the database.  You have the option to create a new database or migrate changes into an existing one.  I created an empty database ahead of time so chose Migrate Database.  The lower portion of the screen displays the database objects and what operations will be performed.  Being new tables, all operations are set to Add.  Click Next >.

Updatefrommodel

Lastly, the wizard displays information about the SQL script it generated to make changes to the database.  You have the option to save the script file or save and execute it.  I chose to save and execute.  Click Finish.

Scriptexecute

With the database tables created one task remains.  To map the Domain Classes to their database tables.  From the diagram canvas right-click on a Domain Class and select Map to Table...This opens up the Edit Table dialog.  The database tables created earlier will be shown in the "Already available tables:" dropdown list.  Select the database table for your Domain Class and click OK.  Do this for each of your Domain Classes.

Maptotable

That's all there is to it, at this point you now have a fully developed data access layer and object model to use in your software applications.  Everything you need to create, retrieve, update and delete records from the database tables has been generated without a developer having to write a single line of code.

In part two of this blog post series I'll demonstrate how easy it is to use the object model by creating a contacts list ASP.Net web application.

Gordon Family Bathroom Renovation - Part 1: Tear Down

This is the first in a series of posts where I'll be covering a bathroom renovation project that I'm working on for some friends of mine.  We're not talking a small renovation project here, we're talking about a complete tear down and rebuild.  The type of project that in order to clean a mess, you have to first make a mess...and a mess we made...as well as found!

Our first task was to tear down the existing bathroom, removing the sink, toilette, tub, walls and ceiling tile.  I found it odd the bathroom ceiling was finished with ceiling tile because it acts like a sponge and soaks up moisture.  Because of this, the original ceiling was severely damaged above the shower area, which meant the ceiling needed to come down too.

With the drywall removed we discovered a damaged soil stack.  It was an old cast iron stack that had somehow rusted completely through, leaking sewage gases into the wall cavity. Luckily this cast iron section tied into PVC at the bottom so it was fairly easy to replace with a new PVC stack.  We also uncovered a sealed off window behind the bathtub surround that we'll need to deal with.

Ceiling Damage New Stack Hidden Window

It became clear this wasn't the first renovation the bathroom has seen. We found old plumbing runs on the opposite side of the room from where the sink and toilette now reside.  This was our first clue.  Then we found the window behind the tub surround, our second clue.  But what we discovered next is downright scary!  Our third and final clue, the electrical wiring.

There was 3 major issues with the electrical work. First was an unused live wire cut and terminated using duct tape.  This was left dangling in a wall cavity rather than being terminated in a junction box or better yet removed.  Second was an electrical junction consisting of 4 power lines all duct taped together.  This too was left dangling in a wall cavity with no junction box or marrets used.  Third was the power line for the ceiling fan.  This was done by cutting into another power line and T'ing off up to the fan.  Again using duct tape with no junction box or marrets.  Needless to say, the electrical wiring was also removed.  I could never stress this enough.  Electrical wiring is not something to be taken lightly.  If you don't know what you're doing or are uncomfortable in any way working with electricity, hire a professional!  Please, for your own safety and the safety of future home buyers, don't do things like this.

Crazy Junction Ceiling Fan Wire

The tear down process certainly uncovered a fair amount of unexpected work that needs to be done.  I can't say I was all that surprised though, it's quite common during home renovations to uncover things you didn't expect to see.  Be sure to check out part 2 where we address the ceiling repairs and exterior wall.

My Must Have Development Apps

Software development requires tools. This could range from a simple text editor/compiler combination to a full blown developer work station with all the bells and whistles. I'm in favor of empowering developers to do their job efficiently and effectively, so I tend to prefer the bells and whistles approach. Here's my personal list of must have development apps.

Microsoft SQL Server Management Studio

SQL Server Management Studio is a tool for software developers, database architects and database administrators to visually work with SQL Server databases.  If you use SQL Server as a database server, this is a must have.

Microsoft Visual Studio

Visual Studio is a powerful integrated development environment, or IDE, that offers a number of flavors to accommodate various needs. If you're a .Net developer chances are this is your code editor of choice.

MySQL Workbench

MySQL Workbench is a tool for software developers, database architects and database administrators to visually work with MySQL databases. If you use MySQL as a database server, this is a must have.

NUnit

NUnit is an open source unit testing framework for Microsoft .Net development. It's feature rich and highly supported by 3rd party tools. If you practice Test Driven Development or simply want to code with confidence, then NUnit is a fantastic option.

Submain TestMatrix

TestMatrix is 3 in 1 application to add Visual Studio support for running unit tests, collecting code coverage and profiling your code. If you want to run your unit tests, see how much of your code is being tested and to better understand the implications of your design choices all from within Visual Studio, give TestMatrix a shot.

TortoiseSVN

TortoiseSVN is an easy to use Apache Subversion client that integrates seamlessly into Windows explorer. If Subversion is your Source Control Management system of choice, TortoiseSVN is possibly the best option for you.

Telerik JustCode

JustCode is a Visual Studio add-in that offers on-the-fly code analysis, error checking, code navigation and refactoring tools. If you care about productivity and code quality, JustCode has a lot to offer.

Telerik JustMock

JustMock is a developer productivity tool designed to ease the creation of mock objects. It allows you to create unit tests that are independent of external dependencies such as databases, web services or tightly bound objects. Mock on!

Telerik OpenAccess ORM

OpenAccess ORM is a productivity tool used to generate data access layers for your .Net applications. It greatly reduces development time by taking over the task of writing database access code.

Telerik RadControls

Telerik offers a huge variety of user interface controls for a number of development platforms. RadControls exist for ASP.Net AJAX, ASP.Net MVC, Windows Forms, Windows Phone, WPF and Silverlight. These control toolsets increase developer productivity and add large value to your software due to their slick UI and excellent performance.

Telerik Reporting

Telerik offers a great reporting package. Like many developers out there I've never been a fan of report writing. Telerik helps to ease this pain by offering a solution that's easy to use and feature rich.

Telerik Test Studio Express

Test Studio Express contains a test recorder for creating automated user interface tests. By building a library of user interface tests you can efficiently regression test your applications.

 

If you have a suggestion for a must have development app or want to share what's on your list, please leave a comment and let me know!

My Must Have PC Apps

We've all been there, sitting in front of a shiny new computer running a fresh operating system.  Chances are the system is running fantastic but lacks the functionality we need.  So what's to be done?  The installation of our must have apps of course!  Here's my personal list of must have PC applications.

AVG Anti-Virus

AVG offers an excellent anti-virus software package for free.  If you own a PC you need anti-virus software and AVG Anti-Virus is a great option at no cost.

CCleaner

CCleaner is the application I use to keep my Windows PCs clean.  It removes un-needed files, abandoned registry entries and has a couple of other useful tools.

Daemon Tools Lite

Daemon Tools Lite allows users to make CD/DVD/Blu-ray disc images, create virtual drives and play CDs, DVDs & Blu-rays without the need for the physical disc.

Dropbox

Dropbox is 2GB of free cloud storage space synchronized to all of my devices.  Drop a file here, read it there.  Nifty.

Google Chrome

Google Chrome is my web browser of choice.

NewsLeecher

NewsLeecher is an NNTP client application that is used to download from UseNet groups or .nzb files. If you're into news groups, NewsLeecher is a reasonably  priced option.

iTunes

I'm going to be honest with you, I'm not happy about this one, in fact I hate it.  I do own a number of apple devices though and iTunes is required to manage my media libraries.

Notepad++

Notepad++ is a free source code editor and notepad replacement. When compared to Windows Notepad or Wordpad, Notepad++ is superior in every way.

Paint.NET

Paint.NET is a free image and photo editing application. Originally intended to be a replacement for Windows Paint, Paint.NET has grown into a product that competes with the likes of Photoshop and Paint Shop Pro.

Windows Live Messenger

I have used instant messaging software since 1997 when first introduced to ICQ (I Seek You). I have used a number of instant messaging applications through the years and have settled in with MSN Messenger, now known as Windows Live Messenger.

WinRAR

WinRAR is an easy-to-use archiving utility that can unpack most archive formats, as well as compress to both RAR and ZIP.

Wunderlist

Wunderlist is a great task management application that runs on a wide variety of platforms.  With Wunderlist I can access, use and synchronize my task lists to all of my devices.

 

If you have a suggestion for a must have PC app or want to share what's on your list, please leave a comment and let me know!

Windows Authentication and Roles in ASP.Net 4

When your IIS 7 web server is a member of an Active Directory domain, it's very easy to configure ASP.Net 4.0 web sites to authenticate domain users and be able to test for group membership. This is a great option when building secured or intranet web sites that require authentication and roles functionality, but is not recommended for unsecured internet sites.

We'll get started by creating a new ASP.Net Empty Web Application project in Visual Studio 2010.

New Project

With our new web site project created we need to make some modifications to the system.web configuration section of its Web.config file.

<system.web>
  <compilation debug="true" targetFramework="4.0" />
  <authentication mode="Windows" />
</system.web>

By adding authentication mode="Windows" to our configuration file, we're passing the responsibility of authenticating users over to the web server itself. The user identity supplied by IIS 7 will be treated as the authenticated user in our ASP.Net 4.0 web site.

This will require a small configuration change to our web site settings in IIS 7 when deployed. We've asked IIS to be responsible for user authentication so we need to configure how we expect it to be done.

wawr_iis_auth

From the Features View of our web site in IIS 7 management console, we need to select Authentication from the IIS grouping.

Wawr _auth _settings2

Ensure Anonymous Authentication is disabled and that Windows Authentication is enabled, done deal.

With authentication taken care of we need to configure our roles functionality. User roles will be managed in Active Directory by adding or removing domain users to and from groups. 

<system.web>
  <compilation debug="true" targetFramework="4.0" />
  <authentication mode="Windows" />
  <roleManager defaultProvider="WindowsTokenRoleProvider"
    cacheRolesInCookie="false"
    enabled="true">
    <providers>
      <add name="WindowsTokenRoleProvider"
        type="System.Web.Security.WindowsTokenRoleProvider"/>
    </providers>
  </roleManager>
</system.web>

Here we've added a roleManager element and configured it to use the WindowsTokenRoleProvider. This configuration enables our web site to get user role information from Windows group memberships.

We now have everything required to implement access restrictions.

<system.web>
  <compilation debug="true" targetFramework="4.0" />
  <authentication mode="Windows" />
  <authorization>
    <allow roles="YOURDOMAIN-COM\WebSiteUsers" />
    <deny users="*" />
  </authorization>
  <roleManager defaultProvider="WindowsTokenRoleProvider"
    cacheRolesInCookie="false"
    enabled="true">
    <providers>
      <add name="WindowsTokenRoleProvider"
        type="System.Web.Security.WindowsTokenRoleProvider"/>
    </providers>
  </roleManager>
</system.web>

The authorization configuration section allows us to control who has access to the web site. Above, we are granting access to users who are members of the WebSiteUsers group on a domain and denying access to all others. Access restrictions can be kept as simple as this or configured much more granular to meet more demanding requirements. The roles framework is also accessible programmatically so there's a wide range of options.

// Get a user's roles.
Roles.GetRolesForUser();
Roles.GetRolesForUser(@"YOURDOMAIN-COM\SpecificUserName");

// Test if a user is in a role
Roles.IsUserInRole(@"YOURDOMAIN-COM\WebSiteUsers");
Roles.IsUserInRole(@"YOURDOMAIN-COM\SpecificUserName", @"YOURDOMAIN-COM\WebSiteUsers");

That's about it, with a few lines of XML and a small tweak to a web site setting in IIS 7 we're able to configure ASP.Net 4.0 web sites to authenticate users on a domain and be able to test group memberships.