Build an Employee Tracker with AngularJS and Web API – Part 1

  • In Solution Explorer, right-click the EmployeeTrackerRS Solution, and select Add and then New Project…

    In the Add New Project dialog box, select Class Library under the Visual C# tab.

  • In Solution Explorer, right-click the EmployeeTracker.Services project, and select Add and then New Folder.
  • Replace the generated code with the following:

     

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using public interface IRepository
    {
    TaskT TaskT getData);
    SqlConnection GetConnection(bool multipleActiveResultSets = false);
    }
    }

     

    Next, add the Repository class that implements IRepository interface:

     

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using public class Repository: IRepository
    {
    private readonly string _connectionString;

    public Repository(IConnection db)
    {
    _connectionString = db.ConnectionString;
    }

    // use for buffered queries that return a type
    public async TaskT TaskT getData)
    {
    try
    {
    using (var connection = new {
    await connection.OpenAsync();
    return await getData(connection);
    }
    }
    catch (TimeoutException ex)
    {
    throw new TimeoutException(“Timeout Exception”, ex);
    }
    catch (SqlException ex)
    {
    throw new DBConcurrencyException(“Sql Exception”, ex);
    }
    }

    public SqlConnection GetConnection(bool multipleActiveResultSets = false)
    {
    try
    {
    var cs = _connectionString;
    if (multipleActiveResultSets)
    {
    var scsb = new SqlConnectionStringBuilder(cs) { MultipleActiveResultSets = true };
    if (scsb.ConnectionString !

  • = null) cs = scsb.ConnectionString;
    }
    var connection = new SqlConnection(cs);
    connection.Open();
    return connection;
    }
    catch (TimeoutException ex)
    {
    throw new TimeoutException(“Timeout Exception”, ex);
    }
    catch (SqlException ex)
    {
    throw new DBConcurrencyException(“Sql Exception”, ex);
    }
    }

    }
    }

     

    This Repository class implements Joe’s WithConnection() method to manage opening database connection, accessing data, and closing connection Explorer, right-click the EmployeeTracker.Services project, and select Add and then Reference….

  • Replace the generated code with the following:

     

    using System.Reflection;
    using System.Web.Http;
    using Autofac;
    using EmployeeTracker.Api
    {
    public static class WebApiConfig
    {
    public static void Register(HttpConfiguration config)
    {
    // Web API configuration and services

    // Web API routes
    // use camel case for JSON data
    =
    new config.Routes.MapHttpRoute(
    name: “DefaultApi”,
    routeTemplate: “api/{controller}/{id}”,
    defaults: new { id = RouteParameter.Optional }
    );

    /*** Autofac: Build the container ***/
    var builder = new ContainerBuilder();

    // register Web API Controllers
    // register your classes and expose their interfaces – shared
    builder.RegisterModule(new StandardModule());

    var container = builder.Build();
    config.DependencyResolver = new }
    }
    }

     

    Create a new folder called Autofac.

This is the first part of Building an Employee Tracker using AngularJS and ASP.NET Web API Series.; Author: Cathy Wun; Updated: 31 Jan 2017; Section: Web Services; Chapter: Web Development; Updated: 31 Jan 2017

@mak_in: “Build an Employee Tracker with AngularJS and Web API – Part 1 – CodeProject”

In this tutorial, we will lay down the foundation for this single page application using Visual Studio 2013 and SQL Server.

I came across this article written by Joe Sauve on how to use Dapper accessing data from the database asynchronously. Dapper also supports numerous different database vendors. With that in mind, you can refactor the following classes to support different databases other than SQL Server.

Next, we are going to use Autofac as the IoC container that will handle all the lifetime management of our dependencies and do the dependency injection for us.

In this tutorial, we have just finished creating the initial structure of the Employee Tracker Web API application. In the next tutorial, we will build the Web API services.

Build an Employee Tracker with AngularJS and Web API – Part 1

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.