Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search in result dto object instead of db class #394

Open
ertugrulkaya opened this issue Apr 27, 2024 · 4 comments
Open

Search in result dto object instead of db class #394

ertugrulkaya opened this issue Apr 27, 2024 · 4 comments

Comments

@ertugrulkaya
Copy link

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 ?

@enrij
Copy link

enrij commented May 3, 2024

If I got it right, specification 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
    }
}

@ertugrulkaya
Copy link
Author

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
>     }
> }
> ```

@enrij
Copy link

enrij commented May 22, 2024

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

@ertugrulkaya
Copy link
Author

ertugrulkaya commented May 22, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants