Created
August 30, 2013 08:29
-
-
Save JoachimL/6387577 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return movieRepository.GetAll().Project<Movie>().To<MoviePresentationModel>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return movieRepository.GetAll().Select(Mapper.Map<MoviePresentationModel>); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WebApi.OData.Repositories | |
{ | |
public class MovieRepository | |
{ | |
static Context context = new Context(); | |
internal IQueryable<Domain.Movie> GetAll() | |
{ | |
return context.Movies; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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