Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 15, 2012 14:28
Show Gist options
  • Save davybrion/3728197 to your computer and use it in GitHub Desktop.
Save davybrion/3728197 to your computer and use it in GitHub Desktop.
code snippets for "Think Twice Before You Map Entities To DTOs" post
public class AuthorizationDto
{
public long Id { get; set; }
public Guid ApplicationId { get; set; }
public string ApplicationName { get; set; }
public Guid? UserGroupId { get; set; }
public string UserGroupName { get; set; }
public Guid? UserId { get; set; }
public string Username { get; set; }
public Guid PermissionId { get; set; }
public string PermissionName { get; set; }
public string PermissionDescription { get; set; }
}
var items = Session.CreateCriteria<Authorization>()
.CreateAlias("Application", "a", JoinType.InnerJoin)
.CreateAlias("User", "u", JoinType.LeftOuterJoin)
.CreateAlias("UserGroup", "g", JoinType.LeftOuterJoin)
.CreateAlias("Permission", "p", JoinType.InnerJoin)
.Future<Authorization>();
var dtos = new AuthorizationDtoMapper().ToDtos(items);
var dtos = Session.CreateCriteria<Authorization>()
.CreateAlias("Application", "a", JoinType.InnerJoin)
.CreateAlias("User", "u", JoinType.LeftOuterJoin)
.CreateAlias("UserGroup", "g", JoinType.LeftOuterJoin)
.CreateAlias("Permission", "p", JoinType.InnerJoin)
.SetProjection(Projections.ProjectionList()
.Add(Projections.Id(), "Id")
.Add(Projections.Property("a.Id"), "ApplicationId")
.Add(Projections.Property("a.Name"), "ApplicationName")
.Add(Projections.Property("u.Id"), "UserId")
.Add(Projections.Property("u.UserName"), "Username")
.Add(Projections.Property("g.Id"), "UserGroupId")
.Add(Projections.Property("g.Name"), "UserGroupName")
.Add(Projections.Property("p.Id"), "PermissionId")
.Add(Projections.Property("p.Name"), "PermissionName")
.Add(Projections.Property("p.Description"), "PermissionDescription"))
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(AuthorizationDto)))
.Future<AuthorizationDto>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment