Using NHibernate 2.1, I'm trying to project an entity and its child collection into a DTO. My entity looks like this..
public class Application
{
public int Id {get;set;}
public string Name {get;set;}
public List<ApplicationSetting> Settings {get;set;}
// A bunch of other properties that I don't want in the DTO
}
public class ApplicationSetting
{
public int Id {get;set;}
public string Name {get;set;}
public string Code {get;set;}
// A bunch of other properties that I don't want in the DTO
}
My DTO looks like this..
public ApplicationDto
{
public int Id {get;set;}
public string Name {get;set;}
public List<ApplicationSettingDto> Settings {get;set;}
}
public class ApplicationSettingDto
{
public int Id {get;set;}
public string Name {get;set;}
public string Code {get;set;}
}
My code to select JUST the Application and project it is this (using Nhibernate 2.1 and nhLambdaExtensions)
var applicationAlias = new Application();
var criteria = Session
.Add<Application>(a => a.Id == id);
int? Id = null;
string Name = null;
criteria
.SetProjection
(
Projections.Distinct(
Projections.ProjectionList()
.Add(LambdaProjection.Property<Application>(a => a.Id).As(() => Id))
.Add(LambdaProjection.Property<Application>(a => a.Name).As(() => Name))
)
);
criteria.SetResultTransformer(Transformers.AliasToBean(typeof(ApplicationDto)));
var contract = criteria.UniqueResult<ApplicationDto>();
My question is, how do I project just SOME of the properties from the ApplicationSettings entity to the ApplicationSettingsDto child collection?
I think you might need to do a MutiQuery and bring together the DTO parents and children yourself.
Related
I am trying to join 2 tables, and project directly to DTOs (NHibernate 5).
I have following entities:
public class Person {
public Guid Id {get;set;}
public string Name {get;set;}
}
public class Car {
public Guid Id {get;set;}
public string Brand {get;set;}
public Person Owner {get;set;}
}
as we see, there is just reference from Car to Person (car knows its owner), and this is ok in my whole project.
However, there is one place, where I need to query all Persons, and make each person with collection of owned cars.
I created such DTOs:
public class PersonDto {
public Guid Id {get;set;}
public string Name {get;set;}
public IList<CarDto> {get;set;}
}
public class CarDto {
public Guid Id {get;set;}
public string Brand {get;set;}
}
it is kind of present the data linked together upside-down.
This task seems trivial using SQL or LINQ (GroupJoin) however I found it extremly hard to do in NH, since GroupJoin is not implemented in NH.
Can you please help me how to solve above issue?
Just add a Car collection to the existing Person entity and mark the collection inverse. Collections in NHibernate are lazy by default, so when you query Person, it won't read the cars from the DB, until you start iterating them. In other words adding the Car collection won't affect the way your code works.
When you want to efficiently query persons together with cars, force NH to do a join
.QueryOver<Person>.Fetch(person => person.Cars).Eager
I'd like to answer my own question. Thanks Rafal Rutkowski for his input.
To get data that has association in only 1 direction, we need to introduce a new data model, and then, manually convert result to our entities. I hope following example is best possible answer:
1) create such data model to store NH response:
private class PersonCarModelDto
{
public Guid PersonId { get; set; }
public string PersonName { get; set; }
public Guid CarId { get; set; }
public string CarBrand { get; set; }
}
2) create such a model to store hierarchical data as output:
private class PersonModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public IList<CarModel> Cars { get; set; }
}
private class CarModel
{
public Guid Id { get; set; }
public string Brand { get; set; }
}
3) now the NH query:
Person personAlias = null;
Car carAlias = null;
PersonCarModelDto resultAlias = null;
var response = GetCurrentSession().QueryOver<Car>(() => carAlias) // notice we are going from from 'downside' entity to 'up'
.Right.JoinAlias(c => c.Owner, () => personAlias) // notice Right join here, to have also Persons without any car
.SelectList(list => list
.Select(() => personAlias.Id).WithAlias(() => resultAlias.PersonId)
.Select(() => personAlias.Name).WithAlias(() => resultAlias.PersonName)
.Select(() => carAlias.Id).WithAlias(() => resultAlias.CarId)
.Select(() => carAlias.Brand).WithAlias(() => resultAlias.CarBrand)
.TransformUsing(Transformers.AliasToBean<PersonCarModelDto>())
.List<PersonCarModelDto>()
;
4) now we have flat data as a list of PersonCarModelDto, but we want to make output model:
var modelResult = response.GroupBy(p => p.PersonId)
.Select(x => new PersonModel
{
Id = x.Key,
Name = x.Select(y => y.PersonName).First(), // First() because each PersonName in that group is the same
Cars = x.Select(y => new CarModel
{
Id = y.CarId,
Name = y.CarBrand
})
.ToList()
})
.ToList()
;
Conclusions:
the problem is much easier to solve having bidirectional associations
if for some reason, you don't want bidirectional associations, use this technique
this approach is also useful if your entities has a lot of other properties, but for some reason you need just a small part of them
(optimiziation of data returned for DB)
I have the following sudo-code
class viewModel
{
public ICollection<modelA> parentModel
public modelC formModel
}
class modelA
{
public int ID {get;set;}
public virtual Icollection<modelB> {get;set;}
}
class modelB
{
public int ID {get;set;}
public string SomeString {get;set;}
public virtual modelA ModelA {get;set;}
}
class modelC
{
public int ModelAID {get;set;}
public string SomeString {get;set;}
}
So. the view model contains a collection of As. Each A contains a collection of Bs and there is a separate model for posting back as a form: the form will be repeated on the page, once in each instance of A with the A.ID passed in to ModelAID as a hidden field. Only one form posting is allowed on the page, The id of the form fields are formModel.ModelAID and .formModel.SomeString as they are derived from the non-parent element of the viewModel.
How do I get the ActionResult to bind only to formModel?
[HttpPost]
Public ActionResult Input(formModel vm)
{
... by default the view model being passed back is full VM, I only want the formModel so the post signature does not match
}
You can try something like
public ActionResult Input([Bind(Prefix = "formModel ")]modelC model)
{
}
Using LINQ, is it possible to combine properties from both an object and a nested collection of that object into a new object? So for each item in the nested collection, I want to create a new object that has the nested object information coupled with the parent object's info.
Using a sample scenario, I'm trying to do something like this:
Teachers.Select(Function(item) New TeacherRecord() With
{.TeacherId = item.Id,
.TeacherName = item.Name,
.StudentID = ? ,
.StudentName = ?}).ToList()
Sample Classes
Public Property Teachers as List(of Teacher)
Public Class Teacher
Public Property ID as Integer
Public Property Name as String
Public Property Room as String
Public Property Students as List(of Student)
End class
Public Class Student
Public Property ID as Integer
Public Property Name as String
End Class
Public Class TeacherRecord
Public Property TeacherId as Integer
Public Property TeacherName as String
Public Property StudentId as Integer
Public Property StudentName as String
End Class
You need to use SelectMany, I don't know much about VB but this is how you do it in C#:
List<TeacherRecord> records = teachers.SelectMany(t => t.Students, (t, s) =>
new TeacherRecord { TeacherId = t.ID,
TeacherName = t.Name,
StudentId = s.ID,
StudentName = s.Name }).ToList();
you will need to rethink your TeacherRecord relationship to a Teacher Or Student - now it is unclear, but generally you can combine then for your ViewData and code will look like this:
void Main(){
List<Teacher> teacher = new List<Teacher>();
List<Student> student = new List<Student>();
student.Add(new Student{ID=1,Name="Tom"});
student.Add(new Student{ID=2,Name="Jerry"});
teacher.Add(new Teacher{ID=1
,Name="John"
,Room = "Room A"
,Students = student});
var combined = (from t in teacher
select t).ToList();
}
// Define other methods and classes here
public class Teacher
{
public int ID { get; set; }
public string Name { get; set; }
public string Room { get; set; }
public List<Student> Students { get; set; }
}
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
}
public class TeacherRecord
{
public int TeacherId { get; set; }
public string TeacherName { get; set; }
public int StudentId { get; set; }
public string StudentName { get; set; }
}
and result will be something like this:
I am new to PetaPoco and initially I was liking it but then hit a wall which I simply dont know how to search for.
I have a object which needs to set a property within one of its properties, ie Job.Min.BaseValue. The source of this data is "min_mb".
So basically my object is not a direct mapping of the source table
public class Usage
{
public Decimal BaseValue {get;set;}
public Decimal BaseScale {get;set;}
public Decimal BaseUnit {get;set;}
}
[PetaPoco.TableName("data")]
[PetaPoco.PrimaryKey("date, client_name")]
[PetaPoco.ExplicitColumns]
public class Job
{
[PetaPoco.Column("date")]
public DateTime Date {get;set;}
[PetaPoco.Column("client_name")]
public String ClientName {get;set;}
public Usage Min {get;set;}
public CommvaultJob() { Min = new Usage() { BaseScale=1024, BaseUnit="MB" }; }
}
I think you're just missing the extra type when you call Fetch or Query. This worked for me :
Calling PetaPoco :
var allData = _db.Fetch<TestJobPoco,Usage>("select * from dataTEST");
return View( allData);
The pocos :
[PetaPoco.ExplicitColumns]
public class Usage
{
public Usage()
{
BaseScale=1024;
BaseUnit="MB";
}
[PetaPoco.Column("base_value")]
public Decimal BaseValue {get;set;}
[PetaPoco.Ignore]
public Decimal BaseScale {get;set;}
[PetaPoco.Ignore]
public string BaseUnit {get;set;}
}
[PetaPoco.TableName("dataTEST")]
[PetaPoco.PrimaryKey("id")]
[PetaPoco.ExplicitColumns]
public class TestJobPoco
{
[PetaPoco.Column("id")]
public int Id {get;set;}
[PetaPoco.Column("date")]
public DateTime Date {get;set;}
[PetaPoco.Column("client_name")]
public String ClientName {get;set;}
public Usage Min {get;set;}
public TestJobPoco()
{
//Min = new Usage() { BaseScale=1024, BaseUnit="MB" };
}
}
My test database has an id, date, client_name and base_value columns. The primary key is id so it's slightly different than yours but this shouldn't change the way the poco mapping happens.
If your objects do not map with the table structure, an ORM can't help much.
You will need to do the mapping manually or made new shadow properties that copy the values of the other fields, but this added complexity will defeat the purpose of an ORM.
I have simple classes:
public class Order
{
public int Id {get;set;}
public IList<Name> Names{get;set;}
}
public class Name
{
public int Id {get;set;}
public int LangId {get;set;}
public string LocalName {get;set;}
}
The quesetion is how to query subcollection of Order to get all that have some Order.Names.LocalName (something like this):
IList<Order> orders = repository.GetByProductLocalName("laptop");
I would like to use new NH3 feature QueryOver, not using Linq (I'v found some solutions in SO but all of them uses Linq).
All you need to do is declare an alias variable for the Names collection and then you can use that in the where clause...
String localName = "laptop";
Name namesAlias = null;
return session.QueryOver<Order>()
.JoinAlias(x => x.Names, () => namesAlias)
.Where(() => namesAlias.LocalName == localName)
.TransformUsing(Transformers.DistinctRootEntity)
.List<Order>();