views:

54

answers:

3

Dear All,

I am currently involved in a parts catalogue project.

To give you some background info, I have 3 nHib Entities Part, Application and Vehicle.

Part.cs

public class Part : Entity
{
    public Part()
    {
        Quality = new PartQuality();
        FitmentPosition = new FitmentPosition();
        OEPartNumbers = new List<OEPartNumber>();
        Applications = new List<Application>();
    }

    public virtual string Description { get; set; }

    [NotNull]
    [NotEmpty]
    [Pattern(@"\d{4}[-]\d{3}$", RegexOptions.None, "Must be a valid part number")]
    [DomainSignature]
    public virtual string PartNumber { get; set; }

    public virtual PartQuality Quality { get; set; }

    public virtual FitmentPosition FitmentPosition { get; set; }

    public virtual string Notes { get; set; }

    public virtual string VehicleComments { get; set; }

    public virtual string Image { get; set; }

    public virtual IList<OEPartNumber> OEPartNumbers { get; set; }

    public virtual IList<Application> Applications { get; set; }
}

Application.cs

public class Application : Entity
{
    [DomainSignature]
    public virtual string Name { get; set; }

    public virtual DateTime DateFrom { get; set; }
    public virtual DateTime DateTo { get; set; }

    // Fuel
    public virtual bool Diesel { get; set; }
    public virtual bool Petrol { get; set; }

    // Transmission
    public virtual bool Manual { get; set; }
    public virtual bool Automatic { get; set; }
    public virtual bool SemiAutomatic { get; set; }

    // Air Con
    public virtual bool WithAC { get; set; }
    public virtual bool WithOutAC { get; set; }

    // Body
    public virtual bool Hatchback { get; set; }
    public virtual bool Saloon { get; set; }
    public virtual bool Convertable { get; set; }
    public virtual bool Estate { get; set; }
    public virtual bool Coupe { get; set; }
    public virtual bool Van { get; set; }

    // Number of Doors
    public virtual bool Doors2 { get; set; }
    public virtual bool Doors3 { get; set; }
    public virtual bool Doors4 { get; set; }
    public virtual bool Doors5 { get; set; }

    [DomainSignature]
    public virtual Part Part { get; set; }

    public virtual IList<Vehicle> Vehicles { get; set; }
}

Vehicle.cs

public class Vehicle : Entity
{
    public virtual string Make { get; set; }

    public virtual string Model { get; set; }

    public virtual string Type { get; set; }

    public virtual string Engine { get; set; }

    public virtual DateTime ProductionStart { get; set; }

    public virtual DateTime ProductionEnd { get; set; }

    public virtual IList<Application> Applications { get; set; }

}

It can be seen that a part can have many applications and an application can have many vehicles.

I am trying to pull back a list of all vehicles, using Make, Model, Type and Engine, but also highlight if any of the vehicles are linked to a given Application. I was going to use a DTO with properties of make, model, type, engine and islinked(bool).

I can pull back the filtered vehicles fine, but I am having the issue of identifying whether the vehicle is linked to an application or not. It would be nice if I could do the following

IsLinked = ((Vehicle.Applications.Count(x => x.Name == _name) > 0)

But it doesn't compile. Any Ideas??

Regards

Rich

A: 

I did not understood whats your DTO code. but maybe it will help to figure out.

public class DTO
{
    public bool IsLinked { get; set; }
}

public IList<DTO> Get(string _name)
{
    return Session.Linq<Vehicle>()
            .Select(v => new DTO
                             {
                                 IsLinked = v.Applications.Any(a => a.Name == _name)
                             })
            .ToList();
}
cem
Technically my object is not a DTO, but here it is.public class ApplicationVehicleSummary{ public string Make { get; set; } public string Model { get; set; } public string Type { get; set; } public string Engine { get; set; } public bool IsLinked { get; set; }}
Richard Tasker
A: 

Using criteria queries.

If you wanted to find those vehicles with a particular application:

result = session.CreateCriteria<Vehicle>()
    .CreateAlias( "Applications", "a" )
    .Add( Expression.Eq( "Make ", make ) )
    .Add( Expression.Eq( "a.Name", applicationname ) )
    .List<Vehicle>();

Or, instead of filtering on applicationname, you want a DTO flag based on it:

vehicles = session.CreateCriteria<Vehicle>()
    .Add( Expression.Eq( "Make ", make ) )
    .List<Vehicle>();

dto = vehicles
        .Select(v => new DTO
        {
             IsLinked = v.Applications.Any(a => a.Name == applicationname )
        })
        .ToList();
Lachlan Roche
+1  A: 

Originally I was writting the query using ICritreia (like Lachlan),

public override IQueryable<ApplicationVehicleSummary> GetQuery(ISession session)
{
        ICriteria criteria = session.CreateCriteria<Vehicle>();

        // SELECT
        criteria
            .SetProjection(
            Projections.Property("Make"),
            Projections.Property("Model"),
            Projections.Property("Type"),
            Projections.Property("Engine")
            );
        // WHERE
        criteria
            .Add(
            Restrictions.Eq("Make", _criteria.Make) &&
            Restrictions.Eq("Model", _criteria.Model) &&
            Restrictions.Eq("Type", _criteria.Type) &&
            Restrictions.Eq("Engine", _criteria.Engine)
            );

        //criteria.Add(Something("IsLinked",Subqueries.Gt(0,subCriteria)));

        criteria.SetResultTransformer(Transformers.AliasToBean<ApplicationVehicleSummary>());

        return criteria.List<ApplicationVehicleSummary>().AsQueryable();
}

But after reading Cem's post decided to use a Linq query.

public override IQueryable<ApplicationVehicleSummary> GetQuery(ISession session)
    {
        var results = session.Linq<Vehicle>()
            .Select(v => new ApplicationVehicleSummary
                             {
                                 Make = v.Make,
                                 Model = v.Model,
                                 Type = v.Type,
                                 Engine = v.Engine,
                                 IsLinked = v.Applications.Any(a => a.Name == _name)
                             })
            .Where(v =>
                   v.Make == _criteria.Make &&
                   v.Model == _criteria.Model &&
                   v.Type == _criteria.Type &&
                   v.Engine == _criteria.Engine
            );
        return results;
    }

Which works, thanks for your help.

Richard Tasker