Reputation: 5657
I'm writing a custom validation attribute for validating zip\postal codes. For it to work I absolutely need to know the selected country. But it turns out to be surprisingly difficult to do.
In most view models nop passes the country using its id, which means I need to decipher it. The way to do it would be to use CountryService
since it can translate the id to country name, but the problem is how do I actually do that.
If I were to create a new CountryService
object I would have to also create and pass a number of dependencies (CacheService object etc.) and it sounds like the wrong way of achieving the result. So how do I actually use a Nop Service outside a controller (in my case inside a custom Validation Attribute)?
Upvotes: 0
Views: 174
Reputation: 1088
You don't need to create the object but get an instance from the service locator.
Something like this:
var countryService= EngineContext.Current.Resolve<ICountryService>();
You can check any attribute in nopCommerce like WwwRequirementAttribute
and check how it uses the locator pattern.
Upvotes: 3