Derived Models (DTO's)

Posts   
 
    
DvK
User
Posts: 318
Joined: 22-Mar-2006
# Posted on: 25-Mar-2019 14:36:24   

Hi, quick question (using the latest version v5.5.2, build March 7, 2019). We've started using the Derived Models functionality for replacing our handmade DTO's for read and write operations.

In these existing DTO's, in certain cases, we added aggregated properties for showing the number of hierarchical members, e.g. Order DTO -> property : NumberOfOrderLines.

How can these type of properties be generated ?

Greetz, Danny

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 25-Mar-2019 20:30:59   

I assume you have already included one or more fields from OrderDetails into the Order derived Element. You need this to get OrderDetails into the projection.

I also assume you have manually added a new property in code in the Order derived element class, for the NumberOfOrderLines (in a partial class file, in order not to be overwritten)

So all you need to do is to customize the projection code, inside the OrderDerivedElement (or whatever it is called)Persistence.cs class specifically inside the available code region, which should look like this: // __LLBLGENPRO_USER_CODE_REGION_START ProjectionRegion_OrdersDe // __LLBLGENPRO_USER_CODE_REGION_END Then you will need to formulate the LinQ query that will fetch the required elements.

Example:

OrdersDePersistence.cs

private static System.Linq.Expressions.Expression<Func<Northwind.EntityClasses.OrderEntity, OrdersDm.DtoClasses.OrdersDe>> CreateProjectionFunc()
{
    return p__0 => new OrdersDm.DtoClasses.OrdersDe()
    {
        OrderDetails = p__0.OrderDetails.Select(p__1 => new OrdersDm.DtoClasses.OrdersDeTypes.OrderDetail()
        {
            ProductId = p__1.ProductId,
            Quantity = p__1.Quantity,
        }).ToList(),
        OrderId = p__0.OrderId,
        // __LLBLGENPRO_USER_CODE_REGION_START ProjectionRegion_OrdersDe 
        OrderDetailsCount = p__0.OrderDetails.Count
        // __LLBLGENPRO_USER_CODE_REGION_END 
    };

Fetch Code

List<OrdersDe> results = null;
using (var adapter = new DataAccessAdapter())
{
    var metaData = new LinqMetaData(adapter);
    var q = (from o in metaData.Order
             from od in metaData.OrderDetail
             where o.OrderId == od.OrderId && o.CustomerId == "ALFKI"
             select o)
            .ProjectToOrdersDe();
    results = q.ToList();
}

DvK
User
Posts: 318
Joined: 22-Mar-2006
# Posted on: 25-Mar-2019 22:37:56   

Hi Walaa,

Thank you for pointing out this solution.

I assume you have already included one or more fields from OrderDetails into the Order derived Element. You need this to get OrderDetails into the projection.

No, I didn't because I don't want these members to be fetched because that could run into thousands of records. That's why the need for an aggregated value being the total of the members. So is there an option **not **to include the member entities (OrderDetail in the example) ?

I also assume you have manually added a new property in code in the Order derived element class, for the NumberOfOrderLines (in a partial class file, in order not to be overwritten)

I did now and that works perfectly. But, I assume there's no way to get this done from the designer ?

greetz, Danny

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 26-Mar-2019 08:44:31   

No, not at this point. One of the reasons we didn't implement it yet is that it requires an expression DSL in the designer which is in most cases less usable than what people are used to: C#, VB.NET

Frans Bouma | Lead developer LLBLGen Pro