Specification icon indicating copy to clipboard operation
Specification copied to clipboard

Search in result dto object instead of db class

Open ertugrulkaya opened this issue 10 months ago • 4 comments

Search option in result dto object instead of db class.

I have 3 classes; Material , Material Type and Material Group.

Material includes the material type and material group.

When I show list of materials including material type name to clients , client want search material type name ? But this filed is not in same db table.

How we can do it ? What is the best case ?

ertugrulkaya avatar Apr 27 '24 17:04 ertugrulkaya

If I got it right, specification<Material> and EF Core should handle your request already:

// Assuming Material having a property Type of type MaterialType

internal sealed class MaterialByMaterialTypeNameSpecification : Specification<Material>
{
    internal MaterialByMaterialTypeNameSpecification(string name)
    {
        // Deal with an empty name
        ArgumentException.ThrowIfNullOrEmpty(name);

        Query.Where(material => material.Type.Name == name)
    }
}

// Somewhere in your code
{
    private readonly IRepositoryBase<Material> _repository;
 
    // ...

    public void Bar(CancellationToken cancellationToken = default)
    {
        var specification = new MaterialByMaterialTypeNameSpecification("Foo");
        List<Material> materials = await _repository.ListAsync(specification, cancellationToken);
        // Do something with the returned materials
    }
}

enrij avatar May 03 '24 09:05 enrij

Hi Thank you for answer. But my question is somehow different.

Let me explain.

I have MachineDto which is also include Machine group And MachineGroup Class.

When i retrivening Machine data including MachineGroup and convert to MachineDto, finally show on table for user. And I already implemented Pagination.

when user try to search MAchineGroupName in the table, serach function is try to search in machine table. On the machine table hasnt machinegroup name in db table.

So that How can i implmemnt this request by using ardalis.specifiacation?

public class MachineBySearchRequestSpec : EntitiesByPaginationFilterSpec<Machine, MachineDto>
{
    public MachineBySearchRequestSpec(SearchMachinesRequest request)
        : base(request) =>
        Query.OrderBy(c => c.Name, !request.HasOrderBy());
}

public class EntitiesByBaseFilterSpec<T> : Specification<T>
{
    public EntitiesByBaseFilterSpec(BaseFilter filter) =>
        Query.SearchBy(filter);
}
> public class MachineDto : IDto
> {
>     public Guid Id { get; set; }
>     public string Name { get; set; } = default!;
>     public Guid MachineGroupId { get; set; }
>     public string MachineGroupName { get; set; } = default!;
>     public string EquipmentNo{ get; set; } = default!;
> }
> 
> public class MachineGroup 
> {
>     public Guid Id { get; set; }
>     public string Name { get; set; } = default!;
>    
> }







> If I got it right, specification and EF Core should handle your request already:
> 
> ```cs
> // Assuming Material having a property Type of type MaterialType
> 
> internal sealed class MaterialByMaterialTypeNameSpecification : Specification<Material>
> {
>     internal MaterialByMaterialTypeNameSpecification(string name)
>     {
>         // Deal with an empty name
>         ArgumentException.ThrowIfNullOrEmpty(name);
> 
>         Query.Where(material => material.Type.Name == name)
>     }
> }
> 
> // Somewhere in your code
> {
>     private readonly IRepositoryBase<Material> _repository;
>  
>     // ...
> 
>     public void Bar(CancellationToken cancellationToken = default)
>     {
>         var specification = new MaterialByMaterialTypeNameSpecification("Foo");
>         List<Material> materials = await _repository.ListAsync(specification, cancellationToken);
>         // Do something with the returned materials
>     }
> }
> ```

ertugrulkaya avatar May 22 '24 11:05 ertugrulkaya

Hi...

I'm afraid but I don't understand what you are looking for... In addition there is no SearchBy property nor method in Query property of Specification so I assume you have some extension methods somewhere...

I'd love to help but provide the complete code (regarding the issue, obviously) or a PoC repo

enrij avatar May 22 '24 15:05 enrij

Ok. I will provide a repo tomorrow .

Thank you very much

iOS için Outlookhttps://aka.ms/o0ukef uygulamasını edinin


Gönderen: Enrico Barbieri @.> Gönderildi: Wednesday, May 22, 2024 6:44:06 PM Kime: ardalis/Specification @.> Bilgi: ertugrulkaya @.>; Author @.> Konu: Re: [ardalis/Specification] Search in result dto object instead of db class (Issue #394)

Hi...

I'm afraid but I don't understand what you are looking for... In addition there is no SearchBy property nor method in Query property of Specification so I assume you have some extension methods somewhere...

I'd love to help but provide the complete code (regarding the issue, obviously) or a PoC repo

— Reply to this email directly, view it on GitHubhttps://github.com/ardalis/Specification/issues/394#issuecomment-2125120486, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AEBU57PQGPJ34IW4B3IKNZTZDS4MNAVCNFSM6AAAAABG4HW65CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRVGEZDANBYGY. You are receiving this because you authored the thread.Message ID: @.***>

ertugrulkaya avatar May 22 '24 15:05 ertugrulkaya

Is this issue still relevant? I'm closing the issue. Please feel free to re-open it if you have any more questions. And please provide a reproducible sample.

fiseni avatar Sep 10 '24 16:09 fiseni