I have a Silverlight Business Application application project with these codes.
I have this domain class:
public class BaseDomain
{
public virtual Guid Id { get; set; }
public virtual DateTime CreatedOn { get; set; }
}
public class Sector : BaseDomain
{
public virtual string Code { get; set; }
public virtual string Name { get; set; }
}
The mapping of domain objects has been configured and is working fine.
I have this DTO class:
public class SectorDto : BaseDto
{
[Key]
public virtual Guid Id { get; set; }
public virtual DateTime CreatedOn { get; set; }
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public SectorDto()
{
}
public SectorDto(Sector d)
{
Id = d.Id;
CreatedOn = d.CreatedOn;
Code = d.Code;
Name = d.Name;
}
}
DTO is used to smooth an object and ensure that no unnecessary relationships are serialized or wired.
Then I have this RIA DomainService (there are several options for the GetSectors () method, I will explain later):
[EnableClientAccess]
public class OrganizationService : BaseDomainService
{
public IQueryable<SectorDto> GetSectors1()
{
return GetSession().Linq<Sector>()
.Select(x => Mapper.Map<Sector, SectorDto>(x));
}
public IQueryable<SectorDto> GetSectors2()
{
return GetSession().Linq<Sector>().ToList()
.Select(x => new SectorDto(x)).AsQueryable();
}
public IQueryable<SectorDto> GetSectors3()
{
return GetSession().Linq<Sector>().Select(x => new SectorDto(x));
}
public IQueryable<SectorDto> GetSectors4()
{
return GetSession().Linq<Sector>().Select(x => new SectorDto() {
Id = x.Id, CreatedOn = x.CreatedOn, Name = x.Name, Code = x.Code });
}
}
BaseDomainService is the only parent class that provides NHibernate session processing. I set up a session for every web request.
Then I will connect the service to the DataGrid (Silverlight Toolkit) on the XAML page:
var ctx = new App.Web.Services.OrganizationContext();
SectorGrid.ItemsSource = ctx.SectorDtos;
ctx.Load(s.GetSectors1Query());
When calling various methods, I got the following results:
GetSectors1() " " GetSectors1 ". NHibernate.Linq.Expressions.EntityExpression ' ' NHibernate.Linq.Expressions.CollectionAccessExpression".".
, . AutoMapper, DTO. , AutoMapper, , , Select, . GetSession().Linq<Sector>().Select(x => CustomMap(x)).
GetSectors2() , IQueryable, .
GetSectors3() , Id CreatedOn, BaseDomain. .
GetSectors4() , DTO , !
, ? , ! , ? ?
, . .