简体   繁体   中英

Automapper Resolver without source

I am currently using a resolver to add category nav links to my base page model as such:

public sealed class CategoryNavigationResolver : IMemberValueResolver<object, object, string, ICollection<ProductCategoryModel>>
{
    public ICollection<ProductCategoryModel> Resolve(object source, object destination, string sourceMember, ICollection<ProductCategoryModel> destMember, ResolutionContext context)
    {
        IProductCategoryHandler productCategoryHandler = DependencyResolver.Current.GetService<IProductCategoryHandler>();
        return productCategoryHandler.GetCategoryNavigation();
    }
}

and the mapping:

CreateMap<BasePage, BasePageModel>()
    .IncludeBase<BaseEntity, BaseModel>()
    .ForMember(dest => dest.ProductCategoryLinks, opt => opt.ResolveUsing<CategoryNavigationResolver, string>(src => src.Alias));

However I do not need to pass the alias in as I don't really need a source for the resolver. Is this the correct way to do this or is there a type of resolver where I do not need to pass in an argument?

I have tried using

            .ForMember(dest => dest.ProductCategoryLinks, opt => opt.UseValue(productCategoryHandler.GetCategoryNavigation()));

moving the handler into the profile, but that gave me a stack overflow error

However I do not need to pass the alias in as I don't really need a source for the resolver. Is this the correct way to do this or is there a type of resolver where I do not need to pass in an argument?

You can ignore passing parameters when using custom value resolvers. But you need to implement IValueResolver instead of IMemberValueResolver .

Let say you still use the same name for the class implementing IValueResolver then you can just use this:

opt => opt.ResolveUsing<CategoryNavigationResolver>()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM