How can I find a specific Specification Attribute Option by name?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hello,

I'm using nopCommerce 3.90 with source.

I'm developing a misc plugin. I have a need to find a specific Specification Attribute Option name in the SpecificationAttributeOption table.

Does anyone have any idea of how to do that in the plugin?

I see there are methods in the SpecificationAttributeService to get things by id, but nothing to do a name search.

Thanks,
Tony
6 years ago
There is no service method for it.  You can use LINQ to find it - e.g. for Attribute (not option)

var specificationAttributes = _specificationAttributeService
  .GetSpecificationAttributes()
  .Where(sa => sa.Name == saNameToFind)

But note that there is no uniqueness enforced for Name (in either Attributes or Attribute Options), so you should test for more than one or use FirstOrDefault()
6 years ago
Thanks for your help with that.  I will be studying up on query language.

Can you tell me how to define and initialize " _specificationAttributeService"?

I put this in the fields region:
private readonly ISpecificationAttributeService _specificationAttributeService;


and this in the constructor of my plugin class constructor body:
this._specificationAttributeService = new SpecificationAttributeService();


but it is giving the error: "There is no argument given that corresponds to the required formal parameter 'cacheManager' of 'SpecificationAttributeService.SpecificationAttributeService'(ICacheManager ... and other fields)'
"

Thanks,
Tony
6 years ago
You'll want to read up on dependency injection - nopCommerce utilizes "Autofac" -

http://docs.autofac.org/en/latest/
6 years ago
If I implement ISpecidicationAttributeService in my class, it gives the error "'LakesideImportManager' does not implement interface member ISpecidicationAttributeService.GetSpecificationAttributeById".

It gives that error for every method in the ISpecidicationAttributeService interface.

Should I include all of those methods in my class?

Thanks,
Tony
6 years ago
I tried copying in all of the methods from SpecificationAttributeService into my class, but that made it worse giving a bunch of other errors for things that it is looking for inside of each method.  I don't think that is the way to go.

In trying to learn, follow and understand the article pointed to by Adam, I did this.

I coded my class like this:

namespace Nop.Plugin.Misc.LakesideImport.ExportImport
{
    public class LakesideImportManager : ImportManager, ISpecificationAttributeService
     {


I added this in the fields region:
          private readonly ISpecificationAttributeService _specificationAttributeService;


I then put in this constructor:
          public LakesideImportManager(ISpecificationAttributeService specificationAttributeService)
          {
               this._specificationAttributeService = specificationAttributeService;
          }


I also put this in the DependencyRegistrar class:
               builder.RegisterType<SpecificationAttributeService>().As<ISpecificationAttributeService>();


The error I am getting is on the constructor.  It says "There is no argument given that corresponds to the required formal parameter 'productService' of ImportManager.ImportManager(IProductService, ... it lists all 25 interfaces)".

I don't know what else to do.  I'm trying to understand and use the information in that article and tried to relate it to what I want to accomplish.  I think part of the problem is that I am not only inheriting another class, but I am trying to implement an interface at the same time.

If there is anyone that has any idea of what else I need to do or take out or change, I would be gratefully appreciative of it.

Thanks,
Tony
6 years ago
Tony,

Your question is meant for individuals learning .NET and not nopCommerce specific.  
You'll need to learn what it means to implement an interface, and why that may not be relevant for you in this situation.

Look into inheriting and extending existing classes. Here are some good articles:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/inheritance

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/

https://stackoverflow.com/questions/15251167/how-to-extend-a-class-in-c


I'll continue to try to guide you in the right direction, but bear in mind, most of us went to school for years to learn this stuff!  It's not as simple as you might be expecting. :)

Good luck!
6 years ago
Adam,

Your guidance and information is very much appreciated.  So is all of the information and knowledge provided by other persons in these forums.

So, none of what I did is on the right track toward getting my task accomplished?

Thanks,
Tony
6 years ago
Carneno wrote:
Adam,

Your guidance and information is very much appreciated.  So is all of the information and knowledge provided by other persons in these forums.

So, none of what I did is on the right track toward getting my task accomplished?

Thanks,
Tony


Without knowing the purpose of what you're trying to accomplish it's hard to say; you'll want to research inheriting and extending a class instead of just implementing an existing interface.

Perhaps something like:


public interface IMyCustomSpecAttrService : ISpecificationAttributeService{
      //method stubs

}

public class MyCustomSpecAttrService : SpecificationAttributeService, IMyCustomSpecAttrService{
      //implementation of new (custom) methods and overriding existing methods
}


6 years ago
I'm not certain why you're implementing ISpecificationAttributeService at all, actually, if you just need to use it.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.