Dan's Green Shoes

Got my green shoes on!


1 Comment

Using your “User” table with MVC4 Internet Template

The MVC4 Internet template assumes that it will be adding a user table called “UserProfile” to your database.  What I think a high percentage of developers would want is to use their domain model’s “user” entity and have SimpleMembership just manage the passwords.  In this blog post I will demonstrate how I did this and welcome alternative suggestions.
I recommend you first understand the authentication process provided by the MVC 4 Internet template.  There are 2 fantastic posts you should read first.

  1. MVC 4 Authentication
  2. SimpleMembership, Membership Providers, Universal Providers and the new ASP.NET 4.5 Web Forms and ASP.NET MVC 4 templates

In addition, please check out Scott Allen’s excellent Pluralsight course called “Building Applications with ASP.NET MVC 4”.

MVC 4 Internet Template with pre-existing Domain Model

Create a new ASP.NET MVC 4 Internet Application and name it “SimpleMemberExample”

image

Custom Domain Model/Database

The first thing I want to do is create the custom domain model/database.  In the “Model” directory, lets add a class that represents our custom “User” table.  Lets call this custom user entity “Administrator” as seen here:

namespace SimpleMembershipExample.Models
{
    public class Administrator
    {
        public Administrator(){}
        #region Fields and Properties
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        #endregion
    }
}

(I was unable to figure out how my user table’s unique identifier could use a Guid.  I think the unique identifier of your custom user entity/table must be an int to use SimpleMembership.  anyone know?)

Lets add a custom DbContext as shown here to the root of the project.

using System.Data.Entity;
using SimpleMembershipExample.Models;

namespace SimpleMembershipExample
{
    public class SomeAppDbContext: DbContext
    {
        public SomeAppDbContext(): base(nameOrConnectionString: "SomeAppName"){}
        public DbSet<Administrator> Administrators { get; set; }
    }
}

The DbContext only contains a single table called Administrators just to keep this super duper easy.  Yours would contain lots more. Notice in the custom constructor, I am telling the DbContext to use a specific connection string.  Lets add that connection string to the Web.Config.

image

This connection string could be anything you want it to be.

Just for demo purposes, I am naming the database “SomeAppName”.  As a final step, in “Global.asax.cs”, put the following code:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        SomeAppDbContext dbcontext = new SomeAppDbContext();
        dbcontext.Administrators.All(i => i.Name != "Dan");

Running the application, the magical EF Code First will create the following database.  We have a custom database with a custom user table called “Administrators”.

image

Customizing MVC 4 Internet Template

Remove InitializeSimpleMembershipAttribute

The Internet template contains an Action filter called “InitializeSimpleMembershipAttribute”.  I deleted that class and removed any references to it.

Replace UserProfile with your Custom User Entity

The Internet template assumes that it will be adding a user table called “UserProfile” to your database.  What I think a high percentage of developers would want is to use their domain model’s “user” entity and have SimpleMembership just manage the passwords.  Let me demonstrate how to do this.

In the “Model” folder, in the “AccountModel.cs” file, delete the “UserProfile” class.  We want to use our “Administrator” entity instead.  Wherever UserProfile is referenced elsewhere, replace with Administrator.   The original UserProfile had properties called “UserName” and “UserId”.  Administrator has properties “Name” and “Id”.  Make sure to sync those properties up.

I want to have only 1 DbContext and I want to use my DbContext.  In the “Model” folder, in the “AccountModel.cs” file, delete the “UsersContext” class.  Wherever UsersContext is referenced elsewhere, replace with SomeAppDbContext.

Call WebSecurity.InitializeDatabaseConnection

As a final step, in “Global.asax.cs”, put the following code:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        SomeAppDbContext dbcontext = new SomeAppDbContext();
        dbcontext.Administrators.All(i => i.Name != "Dan");
        WebSecurity.InitializeDatabaseConnection("SomeAppName", 
            "Administrators", "Id", "Name", autoCreateTables: true);

WebSecurity.InitializeDatabaseConnection takes the web.config connection string name, the name of your custom user table (“Administrators”), the unique identifier field name of your user table (“Id”) and the user name field.  Running the InitializeDatabaseConnection method does several things:

  1. Initializes the static Roles and WebSecurity classes.
  2. Adds the following tables to your database: webpages_Membership, webpages_OAuthMembership, webpages_Roles, webpages_UsersInRoles, UserProfile.

Running the Internet Application

When you run the code with those changes, the following database is created:

image

The home screen looks like this:

image

Click on the Register link in the upper right.

image

Fill out the form and click the “Register” button.  The MVC 4 Internet template application will navigate you back to the home page.

image

Notice it knows the name you registered with.  If you check for cookies, you will see the typical ASPXAUTH as shown below.

image

If you click the “Log off” link and check cookies, the ASPXAUTH cookie disappears.  The ASPXAUTH cookie is the key to the authentication.

When you look at the database you will see the following:

image

Notice with the code changes, the MVC Internet template knew to save to the custom user table “Administrators”.  The password was saved to the webpages_Membership table with a foreign key relationship to the Administrators table.

Please download the code here.

GamifyMockup_thumb.png


Leave a comment

Building a LOB Gamification Service Administration Website: UI Mockup

  1. Introduction
  2. Simulation
  3. Domain Model
  4. Server Data Services
  5. Web API Crud

I have had the pleasure of working with some pretty talented U.I. developers.  There are some people that can look at a screen 10 feet away and notice a couple of borders are not lined up properly. ProModel refers to these folks as having the “pixel eye”.  I do not have the pixel eye unfortunately.  I will say however with the increased emphasis on usability and design that I am starting to be more opinionated in this area.

All along I have had a mental concept of what the administrative website would look like.  I used Balsamic Mockups and produced the following image.

GamifyMockup

This is not going to win any awards but it at least gives me a target.  ProModel uses Pivotal Tracker and I have always been impressed with the usability of their website.  If you look at my design, I was definitely inspired by Pivotal Tracker Smile.

The workflow begins with the user administrator signing in.  In my next blog post in this series, i will discuss how to register and authenticate new administrators.


2 Comments

Building a LOB Gamification Service Administration Website: Web API CRUD

At the end of the last post I had shown a high level overview of some data design patterns such as Repositories and Unit of Work and ended with the browser fetching data from the database for the Application entity of the domain model. In this post, I would like to discuss adding, deleting and updating data through the Web API.

Here is the fully coded ApplicationsController. Notice much of the code is simply delegating to the UoW which in turn simply delegates its work to the generic EFRepository.

clip_image001[1] Continue reading


2 Comments

LOB Gamification Service Admin Website: Data Services

At the end of the last post, I had a solution with multiple projects. One of the projects contained the POCO domain model and another project leveraged EF Code First to read/write the domain model to a dynamically created SQL Server database. The goal of this post is to use ASP.NET MVC Web API to surface database data to the client browser.

Most of my career I have spent on the server side of things moving data between layers. I have been part of teams where we created our own application infrastructure as well as a user of IdeaBlade’s DevForce, WCF RIA Services, CSLA, etc.

In the last year, I have had the pleasure of learning from Jon Sarker who is a senior software architect at ProModel. Jon has helped ProModel streamline moving data efficiently and quickly through the use of enterprise design patterns such as Repositories, Commanding, Factories (for repos and commands in particular), DTOs, Request/Response objects, etc. Once you understand and start using these data design patterns, everything seems so modular, clean and maintainable.

As I embarked on learning the new world of HTML5, JavaScript and Web API, I figured the learning materiel on the web would be the equivalent of showing me how to drag and drop a datasource control. That is why I was so blown away at the depth of John Papa’s course “Single Page Apps with HTML5, Web API, Knockout and jQuery”. From John’s blog, the SPA has lots of patterns “including the Repository Pattern (to expose the data in a consistent way), Unit of Work Pattern (to decouple the Web API from the lower layers and to aggregate the repositories), the Factory pattern (for creating repositories), and Single Responsibility Principle (SRP). The SRP is all throughout the client and server, in fact. Simply put, the patterns help make the app easier to debug, scale and maintain.” Continue reading

ClassDiagram


1 Comment

LOB Gamification Service Admin Website: Domain Model

In the last post about the LOB Gamification Service, the explanation of the video is a great starting point for defining the domain model I will be using.

“Every time the user crossed a lightning bolt Action, the user receives some points toward a Proficiency. A single Action might give points to multiple Proficiencies.  A single Proficiency can earn points through various ActionsProficiencies have different Levels.  As users earn points within a Proficiency, the user “levels up” that Proficiency.  Leveling up typically gives badges and other rewards.”

The words in bold are candidates for objects in the domain model.  Wikipedia defines Domain Model as “The domain model is created in order to represent the vocabulary and key concepts of the problem domain. The domain model also identifies the relationships among all the entities within the scope of the problem domain, and commonly identifies their attributes.”

Continue reading


2 Comments

Building a LOB Gamification Service Administration Website: Simulation

Welcome back to the LOB Gamification Series.  The previous post is Part 1.

ProModel has a portfolio of LOB enterprise software and I thought it would be interesting to explain gamification by looking at integrating gamification into the ProModel LOB application.

Below you see a Microsoft Visio diagram of using ProModel.

As you can see in the diagram, users begin by first launching the application.  The next step the user can either create a new model (document) or open an existing model.   The details of the steps are not important and one can easily substitute another process diagram from another software product.

The flow a user takes through a product is dependent on their rights and roles within the system.  Enterprise modeling using ProModel might have roles such as Model Builder, Data Analyst, Scenario Builder, Report Creator, etc.

The more users use the software, the more proficient they become at certain tasks within the software.

You will notice a lightning bolt on several tasks within the diagram.

The lightning bolts represent Gamification Actions place on a software feature. I defined these Gamification Actions to help me quantify:

  • Who my most proficient users of this feature are.
  • How often users use this feature.
  • Encouraging users to use a feature.

So let’s say we have 3 users:  Rockford, Fabio and myself.  Let’s simulate these 3 users using ProModel simulation software.  It is sort of like pointing a mirror at a mirror….its a simulation of a simulation :-)

The video shows the Visio diagram being brought to life by Process Simulator.   I coded in different behaviors for each of the 3 users.  Rockford and I were modeled as “Model Builders” and Fabio was coded as a “Report Viewer”.  I also coded in the probability of a model needing resources or external files.

Every time the user crossed a lightning bolt Action, the user received some points toward a Proficiency. A single Action might give points to multiple Proficiencies.  A single Proficiency can earn points through various ActionsProficiencies have different Levels.  As users earn points within a Proficiency, the user “levels up” that Proficiency.  Leveling up typically gives badges and other rewards.

I have added a Proficiency Leaderboard showing the points for each users. Leaderboards create a hopefully healthy competition and encourage the user to use certain features.  New users might reach out to a highly proficient user for guidance or help.

Users should be able to drill into a Proficiency and see a list of the Actions that earn points.  Each Action could have context sensitive help as well as video demonstrations.  I feel this could be a far superior way of training users.

In the next part within this series, I will finally get to some coding examples followed by some UI mockups.


1 Comment

Building a LOB Gamification Service Administration Website: Intro

Introduction

I received my Xbox 360 as a birthday gift from my wife in December 2005.  It has since become my all-time favorite console.  Besides Atari 2600, this is probably my most used console ever.  There are many reasons why I used this console more than any other and today I want to simply focus on just one reason referred to as Achievements.

Xbox 360 was unique to the console world at that time in that you “signed into” an account similar to how you sign into your iPhone or similar device.  The Xbox Live account had your gamer tag, an ever growing friends list and a thing called your Gamerscore.  From Wikipedia, “The Gamerscore (G) is an achievements system that measures the number of Achievement points accumulated by a user with a LIVE profile. These Achievement points are awarded for the completion of game-specific challenges, such as beating a level or amassing a specified number of wins against other players in online matches.”  “Achievements are included within games to extend the title’s longevity and provide players with the impetus to more than simply complete the game but find all of its secrets.”  (I added the bold formatting.)

Continue reading

Follow

Get every new post delivered to your Inbox.

Join 446 other followers