Skip to content

Instantly share code, notes, and snippets.

@JoachimL
Created August 30, 2013 08:29
Show Gist options
  • Save JoachimL/6387577 to your computer and use it in GitHub Desktop.
Save JoachimL/6387577 to your computer and use it in GitHub Desktop.
namespace WebApi.OData
{
public class DomainToModelMappingProfile : Profile
{
public DomainToModelMappingProfile()
{
Mapper.CreateMap<Movie, MoviePresentationModel>()
.ForMember(m => m.YearOfRelease, x => x.MapFrom(y => y.Released.Year))
.ForMember(m => m.AverageRating, x => x.MapFrom(y => y.Ratings.Average(r => r.Rating)))
.ForMember(m => m.Actors, x => x.MapFrom(y => y.Actors.Select(a => a.Name)));
}
}
}
return movieRepository.GetAll().Project<Movie>().To<MoviePresentationModel>();
return movieRepository.GetAll().Select(Mapper.Map<MoviePresentationModel>);
namespace WebApi.OData.Domain
{
public class Movie
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual DateTime Released { get; set; }
public virtual ICollection<Person> Directors { get; set; }
public virtual ICollection<Person> Actors { get; set; }
public virtual ICollection<UserRating> Ratings { get; set; }
public virtual Administrator AddedBy { get; set; }
public virtual byte[] CoverImage { get; set; }
}
}
namespace WebApi.OData.Models
{
public class MoviePresentationModel
{
public int Id { get; set; }
public string Title { get; set; }
public int YearOfRelease { get; set; }
public double AverageRating { get; set; }
public string[] Actors { get; set; }
}
}
namespace WebApi.OData.Repositories
{
public class MovieRepository
{
static Context context = new Context();
internal IQueryable<Domain.Movie> GetAll()
{
return context.Movies;
}
}
}
namespace WebApi.OData.Services
{
public class MovieService
{
private readonly MovieRepository movieRepository;
public MovieService(MovieRepository movieRepository)
{
this.movieRepository = movieRepository;
}
public IQueryable<MoviePresentationModel> Get()
{
return movieRepository.GetAll().Project<Movie>().To<MoviePresentationModel>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment