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

Reuse GridifyMapper #253

Open
alirezanet opened this issue Feb 17, 2025 · 1 comment
Open

Reuse GridifyMapper #253

alirezanet opened this issue Feb 17, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@alirezanet
Copy link
Owner

Discussed in #251

Originally posted by Brandejs February 17, 2025
Hello,

First thanks for this project, it is really useful.

I am curious if it is possible to use an existing GridifyMapper and apply it to a "complex object". For example, I have an object Address. And I want to allow filtering only over some properties. The object is used in multiple entities, so I would like to restrict the given property on all entities. Similar to AutoMapper, where a DTO can be embedded and the correct map is used for it.

public class Address
{
    public string City { get; set; }
    public string Country { get; set; }
    public string Secret { get; set; }
}

public class User
{
    public string Email { get; set; }
    public Address Address { get; set; }
    // .... 
}

public class Company
{
    public string Name { get; set; }
    public Address Address { get; set; }
    // .... 
}


public class AddressGridifyMapper : GridifyMapper<Address>
{
    public AddressGridifyMapper()
    {
        AddMap("City", q => q.City);
        AddMap("Country", q => q.Country);
        
        // This field should not be allowed to filter
        // RemoveMap(nameof(Address.Secret))
    }
}

public class UserGridifyMapper : GridifyMapper<User>
{
    public UserGridifyMapper()
    {
        AddMap("Email", q => q.Email);
        
        // Reuse Mapper from AddressGridifyMapper
        AddMap("Address",  q => q.Address);
    }
}

public class CompanyGridifyMapper : GridifyMapper<Company>
{
    public CompanyGridifyMapper()
    {
        AddMap("CompanyName",  q => q.Name);
        
        // Reuse Mapper from AddressGridifyMapper
        AddMap("Address",  q => q.Address);
    }
}
```</div>
@alirezanet alirezanet added the enhancement New feature or request label Feb 17, 2025
@Brandejs
Copy link

Maybe there is also no need for autodiscovery etc..
it can be simple

public class CompanyGridifyMapper : GridifyMapper<Company>
{
    public CompanyGridifyMapper()
    {
        AddMap("CompanyName",  q => q.Name);
        
        AddMap<AddressGridifyMapper>("Address",  q => q.Address);
        // OR
        AddMap("Address",  q => q.Address, typeof(AddressGridifyMapper));
        // OR 
        AddMap("Address",  q => q.Address, new AddressGridifyMapper());
    }
}

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

No branches or pull requests

2 participants