Missing .Less Files with Visual Studio 2010 Deployment Package

Recently, I deployed a Visual Studio 2010 web site and was perplexed by the omission of my .less files from the deployment package. What stumped me was my other projects (in VS2012) deployed .less files without any issues.

The solution is to mark the file’s Build Action as Content in the file Properties in Visual Studio. By default Visual Studio 2010 does not recognize .less files.

  1. View → Solution Explorer
  2. Right-click on the .less file → Properties
  3. Change Build Action to Content

file properties

Visual Studio 2012 does not have this problem. One of the common web tools plugins probably accounts for .less files.

Unit of Work and Repository Pattern with Unity Dependency Injection

May 20, 2017 — My new preferred implementation.

Here’s my implementation of a Unit of Work and Repository pattern for ASP.NET MVC with Unity Dependency Injection.

The following example touches on several debatable issues. It is not the intent of this post to contribute to the debate, rather, as an academic or mental exercise to learn more about the patterns and issues. It should not be taken as a recommendation for or against the patterns presented below.

Package and Framework Versions in this article:
  1. MVC4
  2. Entity Framework 5
  3. Unity 3
  4. Unity.MVC3

Unit of Work

public interface IUnitOfWork : IDisposable
{
    MovieContext DbContext { get; }
    int Save();
}

public class UnitOfWork : IUnitOfWork
{
    protected string ConnectionString;
    private MovieContext context;

    public UnitOfWork(string connectionString)
    {
        this.ConnectionString = connectionString;
    }

    public MovieContext DbContext
    {
        get
        {
            if (context == null)
            {
                context = new MovieContext(ConnectionString);
            }
            return context;
        }
    }

    public int Save()
    {
        return context.SaveChanges();
    }

    public void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (context != null)
            {
                context.Dispose();
                context = null;
            }
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

Repository

Generic repositories are presented here but often I’ll extend GenericRepository to provide a more specific or restricted API.

public interface IGenericRepository<TEntity> where TEntity : class
{
    IQueryable<TEntity> GetAll();
    IQueryable<TEntity> FindBy(Expression<Func<TEntity, bool>> predicate);
    TEntity Add(TEntity entity);
    void Delete(TEntity entity);
    void Update(TEntity entity);
}

public class GenericRepository<TEntity> :
    IGenericRepository<TEntity> where TEntity : class
{
    protected MovieContext DbContext;

    public GenericRepository(IUnitOfWork unitOfWork)
    {
        this.DbContext = unitOfWork.DbContext;
    }

    public IQueryable<TEntity> GetAll()
    {
        IQueryable<TEntity> query = this.DbContext.Set<TEntity>();
        return query;
    }

    public IQueryable<TEntity> FindBy(Expression<Func<TEntity, bool>> predicate)
    {
        IQueryable<TEntity> query = this.DbContext.Set<TEntity>().Where(predicate);
        return query;
    }
    ...
}

Service

The service layer keeps much of the business logic out of the controllers and provides a convenient place to put the Entity → model projections.

public interface IMovieService, IDisposable
{
    IEnumerable<MovieDTO> GetMovies(MovieSearchCriteria searchCriteria);
    IEnumerable<MovieDTO> GetRecommendedMovies(MovieSelectionCriteria criteria);
    ...
}

public class MovieService : IMovieService
{
    private IUnitOfWork unitOfWork;
    private IGenericRepository<Movie> movieRepository;
    private IGenericRepository<Genre> genreRepository;

    public MovieService(IUnitOfWork unitOfWork,
                        IGenericRepository<Movie> movieRepository,
                        IGenericRepository<Genre> genreRepository)
    {
        this.unitOfWork = unitOfWork;
        this.movieRepository = movieRepository;
        this.genreRepository = genreRepository;
    }

    public IEnumerable<MovieDTO> GetMovies(MovieSearchCriteria searchCriteria)
    {
        var movies = movieRepository.FindBy(m => m.Year == searchCriteria.Year)
            .Select(p => new MovieDTO { Title = p.Title, Year = p.Year, ... }).ToList();
        return movies;
    }

    ...

    public void Dispose()
    {
        this.unitOfWork.Dispose();
    }
}

Unity Config

Register everything with Unity. A single line of code registers GenericRepository for all Entity types. Specific repositories, if defined, will also get registered here. Unity.MVC3 takes care of the MVC controllers so they are not registered here.

public static IUnityContainer BuildUnityContainer(string connectionString)
{
    var container = new UnityContainer();
    container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager(),
        new InjectionConstructor(connectionString));
    container.RegisterType(typeof(IGenericRepository<>), typeof(GenericRepository<>));
    container.RegisterType<IMovieService, MovieService>();
    return container;
}

The Microsoft.Practices.Unity.HierarchialLifetimeManager provides:

  1. A Dispose() call after each request
  2. The same DbContext instance per request

Controller

public class MovieController
{
    private IMovieService movieService;

    public MovieController(IMovieService movieService)
    {
        this.movieService = movieService;
    }
    ...
}
And that’s it.