Configure a Visual Studio Online (VSO) Build Agent


When using Visual Studio Online (VSO) in your team or for your personal projects, it is very useful to integrate a compilation and a continuous deployment process. With MSDN subscriptions, you have about 240 minutes of hosted build. After this time, it’s very expensive and you can not install any tools of your choice on the server.

Microsoft allow to install freely the Build and Deployment agent on a on-premice server [note 2]… and very quickly and easily.
In this 5 minutes video, I am going to show how to configure a Build Agent linked to your VSO account.

In order to configure a Build Agents, you must install all tools to compile and to deploy your projects on the server:

Next, you must download and extract the VSO Build Agent included in the Admin portal of your Visual Studio Online account: https://[your_account].visualstudio.com/_admin/_AgentPool

And finally you need to run the ConfigureAgent.cmd program.

D:\agent>ConfigureAgent.cmd
Enter the name for this agent (default is Agent-AL423966) Agent-AL423966
Enter the URL for the Team Foundation Server (default is ) https://dvoituron.visualstudio.com
Configure this agent against which agent pool? (default pool name is ‘default’) default
Enter the path of the work folder for this agent (default is ‘D:\agent\_work’) D:\agent\_work
Would you like to install the agent as a Windows Service (Y/N) (default is N) Y
Enter the name of the user account to use for the service (default is NT AUTHORITY\NETWORK SERVICE) AL423966\BuildUser
Enter the password for user account AL423966\BuildUser: *********
Installing service vsoagent.dvoituron.Agent-AL423966…
Service vsoagent.dvoituron.Agent-AL423966 has been successfully installed.
Creating EventLog source vsoagent.dvoituron.Agent-AL423966 in log Application…
Configuration successful.

There are no special firewall rules to define, because when you start a build process from VSO, all requests are initiated from your Build Server (in https).

That’s all😉

My next article will be “How to create a Build Definition, to compile and to deploy your SQL and Web projects in Azure

More information:

Posted in General, Team Foundation Server, Tips and others, Visual Studio

Simple Object Mapping Toolkit


A few days ago, I talked about an interesting topic when developing applications that require frequent access to databases (eg. SQL Server). Many frameworks are existing to connect your Business Layer to a Database : the best known is often Entity Framework.

After multiple projects using EF, we’ve decided to avoid this framework. Why? First for performance reasons and secondly to avoid maintenance problems (see my previous article about the migration of EF5 to EF6).

We prefer to use mapping tools where we need to create SQL queries and these tools convert data results to equivalents C# objects. For small projects, EF can be a solutions but when you need to develop a large application with a large database, you prefer manipulate, optimize queries to retrieve data quickly.

Dapper.NET is a toolkit, developed by the Stackoverflow team, to simplify querying and to convert SQL data to C# objects (see examples in my presentation).

But Dapper.NET will extend your IDbConnection interface (eg. SqlConnection). It’s easy to use, but to define guideline and for future evolution, I prefer to create an object with properties, methods and a live cycle. For these reasons, I’ve published the project SqlDatabaseCommand: you create an object SqlDatabaseCommand (eg. in a DataService), you define properties and you execute your command. Logging, Exceptions, Entities Generator are automatically managed by the toolkit.

using (var cmd = new SqlDatabaseCommand(CONNECTION_STRING))
{
    cmd.CommandText.AppendLine("SELECT * FROM EMP");
    var emps = cmd.ExecuteTable<Employee>();
}
Posted in General

Introduction to SqlDatabaseCommand


Since some years, I’ve tried to write some performing SQL Queries but also with easy way. In the .NET beginning, I’ve tried System.Data.SqlClient with DataAdapter and DataSets. Next, I’ve tried the Linq to SQL project. And next, I’ve tried Entity Framework. This last framework is the most used at this moment, but when I need some performing requests, it’s very difficult to optimize SQL requests generated by EF.

So, I decided to create a very light toolkit to use the full SQL features and to retrieve quickly all data: SqlDatabaseCommand. This class inherits from a base class called DatabaseCommandBase that contains all main features using only System.Data namespace (DbCommand, DbConnection, …). So, you can inherit this base class to extend the toolkit to other providers like Oracle, SqlLite, etc.

This project is Open Source and hosted on Github: https://github.com/Apps72/Dev.Data

Samples

For example, if you have already wrote a class (Employee) with all properties mapped to the EMP table. You can use this code and the ExecuteTable<T> method.

using (var cmd = new SqlDatabaseCommand(CONNECTION_STRING))
{
    cmd.CommandText.AppendLine(" SELECT * FROM EMP ");
    var emps = cmd.ExecuteTable<Employee>();
}

The following code can be used to retrieve only the first data row, via the ExecuteRow<T> method.

using (var cmd = new SqlDatabaseCommand(CONNECTION_STRING))
{
    cmd.CommandText.AppendLine(" SELECT * FROM EMP WHERE EMPNO = 7369 ");
    var emp = cmd.ExecuteRow<Employee>();
}

If you know that your request returns only one item, use the ExecuteScalar method.

using (var cmd = new SqlDatabaseCommand(CONNECTION_STRING))
{
    cmd.CommandText.AppendLine(" SELECT COUNT(*) FROM EMP ");
    int count = cmd.ExecuteScalar<int>();
}

You can also add parameters in your queries and send parameters easily.

using (var cmd = new SqlDatabaseCommand(CONNECTION_STRING))
{
    cmd.CommandText.AppendLine(" SELECT * FROM EMP WHERE HIREDATE = @HireDate ");
    cmd.Parameters.AddValues(new { HireDate = new DateTime(1980, 12, 17) });
    var emps = cmd.ExecuteTable<Employee>();
}

Find more features to manage Transactions, Logging, customization of your data results, extensions, data injection for Unit Tests and Best practices on https://github.com/Apps72/Dev.Data.

NuGet

To use this toolkit in your .NET projects, add a NuGet Reference to Apps72.Dev.Data.

  • First, search SqlDatabaseCommand in the NuGet Package Manager.
  • Next, select Apps72.Dev.Data (the second NuGet package is to include in a SQL Server CLR Project).

SqlDatabaseCommand-Nuget

Posted in General, SQL Server, SqlDatabaseCommand

ASP.NET MVC and Unity IoC

122915_1425_ASPNETMVCan1.png

Since several years, software are becoming increasingly complex. A technique to simplify the maintenance is to use a design pattern that allows to remove the dependencies to difficult parts of the software (loosely-coupled design). The Dependency Injection Pattern is part of these techniques. In this post, I explain how to use the Unity framework in an ASP.NET MVC project.

The main objective of the IoC is to use the Unity framework to register Model Objects, based on the environment where it is used: for production (DataService), for development (DesignDataService) or for unit tests (TestDataService). Whenever a Controller need data from the model, we call a Resolver method (included in my UnityLocator class) that returns the good instance of this model, based on this environment. So, with a simple flag (#DEBUG, IsDesignMode property, …) you can switch from a Design environment to a Production environment. Another interesting point is for Unit Testing, where you can define your sample data in a TestDataService class to validate all of your methods.

To integrate Unity in a ASP.NET project, you can use existing packages like Unity.Mvc, but I prefer define my lightweight class to have all control on it.

  1. The first step is to add Microsoft Unity in your existing ASP.NET MVC project.

    Select the command Project / Manage NuGet Packages and search Unity 4.0 (or more).

  2. Add a new project ‘Class Library’ where all Models and ViewModels (adaptation of your models to your applications, your views).

  3. In your Model library, add an interface IDataService that will be implemented by the production service (SqlDataService) and the development service (DesignDataService).

  4. In your ASP.NET MVC project:
    (these classes are downloadable here).
    1. Add a reference to you Model library (to use IDataService).
    2. Create a class UnityResolver to manage the Unity framework, with a ‘container’ for your application.
    3. Create a class A UnityDependencyResolver used by ASP.NET MVC, in Global.asax.cs and the Mvc.DependencyResolver.Current property embedded by MVC.
    4. Create a class A UnityLocator to resolve all ViewModels or Models in a single class.

  5. The UnityLocator is the main custom class to register all ViewModels (or Models) and to be referenced by the Global.ascx.cs.
private UnityContainer _container = new UnityContainer();

public UnityLocator()
{
    // Self registration to use it later
    _container.RegisterInstance<UnityLocator>(this);

    // Registration of DataService
    //_container.RegisterType<IDataService, DesignDataService>();
    _container.RegisterType<IDataService, SqlDataService>();

    // Registration of all ViewModels
    _container.RegisterType<HomeViewModel>();
}

public HomeViewModel Home
{
    get
    {
        return _container.Resolve<HomeViewModel>();
    }
}

public UnityDependencyResolver UnityDependencyResolver
{
    get
    {
        return new UnityDependencyResolver(_container);
    }
}

6. Next, you need to include de DependencyResolver in MVC framework global.ascx.cs file.

protected void Application_Start()
{
    // Create and register the IoC Unity resolver
    DependencyResolver.SetResolver(new UnityLocator().UnityDependencyResolver);

    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

7. Finally, you can use your Data objects in all Controllers. To simplify my code, I’ve created a ControllerBase class with a property Locator.

public abstract class ControllerBase : Controller
{
    public UnityLocator Locator
    {
        get
        {
            return DependencyResolver.Current.GetService<UnityLocator>();
        }
    }
}

public class HomeController : ControllerBase
{
    public ActionResult Index()
    {
        var home = this.Locator.Home;
        return View(home);
    }
}

So, you have a separated library with all Models and ViewModels (without references to your UI or Unity framework).
You can create sample data in DesignDataService class, or mokup data in TestDataService for Unit Testings.
In a future evolution, you can remove the reference between your ASP.NET MVC project and your Model library, using a configuration file to load dynamically your data models (but I think that is too complex in a first step).

Source code including a sample of this implementation is here : https://github.com/dvoituron/DevAppsPodcast

Resources: https://unity.codeplex.com and http://amzn.com/B00EO89ZM8

Posted in ASPNET, IoC, MVVM

DevApps Podcasts – 12 épisodes


Voilà déjà quelques mois que nous vous présentons des Podcasts dédiés aux technologies et aux développements Microsoft. C’est avec un grand plaisir que nous préparons déjà les prochaines émissions… nouveaux sujets, nouveaux invités plus intéressants les uns que les autres… Et tout cela, grâce à vous qui nous écoutez régulièrement… Merci.

Vous avez manqué un épisode ? Rendez-vous sur http://devapps.be.

Posted in Uncategorized

How to migrate EntityFramework 5 to EntityFramework 6 with Visual Studio 2015


If you have developed your application with EntityFramework 4 or 5, using the graphical model designer EDMX… May be you received a compilation error after some updates of this EDMX. Why ? Because the model is different with EF6. See this Microsoft Magazine article for more information: https://msdn.microsoft.com/en-us/magazine/dn532202.aspx.

The solution is described in the new file generated by the new EF Visual Studio Wizard: T4 code generation is enabled for your model. To enable legacy code generation, change the value of the ‘Code Generation Strategy‘ designer property to ‘Legacy ObjectContext’. This property is available in the Properties Window when the model is open in the designer.

So, you need to update your EDMX, to delete new generated files (below .tt files), change the property Code Generation Strategy to Legacy ObjectContext and save your EDMX to re-generate all classes.

Watch this short video to a full example.

 

Posted in Tips and others, Visual Studio

DevApps Podcast


DevAppsSince some weeks, I record a Podcast about Microsoft Development technologies (architecture, development and news). These podcasts are in french (my first language). You can listen me at http://devapps.be.

The last podcast was on MVVM Pattern : http://devapps.be/Podcast/4… but there are also others subjects : #1. What’s new in C# 6.0, #2 A summary of the Scrum Guide and #3. How to trace via NLog library.

In the second part of these podcast, I list some Dev News… find here the last list.

Posted in Uncategorized
Follow me
   RSS Feed    Twitter    Linked In

Write you email address to subscribe at this blog and to receive new posts by mail.

Join 10 other followers

Who I am?