0

I want to extend the CatalogProduct model with my own model. I followed the CartModule example on GitHub. I think I did all the parts that the example had, but I still couldn't get it working.

Here are the items that I did:

  • Installed the VirtoCommerce.CatalogModule.Data from NuGet to the module that I created.
  • Created a MyProduct class, which inherit the CatalogProduct class.
  • Created MyProductEntity class, which inherit the ItemEntity, and I override the ToModel, FromModel, and Patch functions.
  • Created MyProductRepositoty class, which inherits the CatalogRepositoryImpl, and override the OnModelCreating function to tell which table I want to map to.
  • Modified the Module.cs file, and implemented the SetupDatabase(), Initialize(), and PostInitialize().

  • Here is what I put in for Initialize()

    _container.RegisterType<ICatalogRepository>(new InjectionFactory(c => new MyProductRepository(_connectionStringName, _container.Resolve<AuditableInterceptor>(), new EntityPrimaryKeyGeneratorInterceptor())));
    
  • Here is what I put in for PostInitialize()

    AbstractTypeFactory<CatalogProduct>.OverrideType<CatalogProduct, MyProduct>();
    AbstractTypeFactory<ItemEntity>.OverrideType<ItemEntity, MyProductEntity>();
    
  • Rebuilt my module, and restarted the IIS

  • I hit the localhost/admin/docs/VirtoCommerce.Catalog/v1 to see if the fields that I add to my model return from the Product definitions. It didn't.

Are there some steps I am missing? I actually got the migration working. It actually created the "MyProduct" table in the database.

Another question is that is it a good idea to add columns to the existing "Item" table? or is it advised to create a new table when extending the domain model?

Thank you all in advanced!

Tony
  • 21
  • 1
  • 3
  • It looked like the response from localhost/admin/docs/VirtoCommerce.Cart/v1 did return the fields that I added. Then I am wondering which model do I extend so that the response would return the fields I added from localhost/admin/docs/VirtoCommerce.Catalog/v1 – Tony Mar 08 '19 at 19:26

1 Answers1

0

The reason why you could not see changes in API is that besides domain model Catalog module also uses web model - e.g. for Product, and API controllers return these web model objects. That's the difference with Cart module in example.

In order to see the changes, you need to use your extended type for web model and extend converter.

As you can see, catalog module extensions not so straightforward in vc-platform 2x version. This will be mitigated in 3x (NET Core) platform version by eliminating web model.

Easier built-in way of extending Catalog objects functionality in platform 2x is by using dynamic properties (in UI, by code).

Another question is that is it a good idea to add columns to the existing "Item" table? or is it advised to create a new table when extending the domain model?

Recommended way is to create new table.

  • Thank you for the reply, Andrey. The response makes sense. I am wondering is there any example from the Virto github of how to extend the product converter? I notice that in the ProductConverter, the ToWebModel() and ToModuleModel() are extension methods of CatalogProduct and Product, so I tried creating my own converter and implemented both functions just like how the functions in the ProductConverter. However, I still couldn't the fields to return from the /docs/VirtoCommerce.Catalog/v1 API endpoint. – Tony Mar 11 '19 at 22:25
  • Tony, there is no example in VC docs. As you could find in [Converter](https://github.com/VirtoCommerce/vc-module-catalog/blob/dev/VirtoCommerce.CatalogModule.Web.Core/Converters/ProductConverter.cs#L16), there are virtual methods [webModel.FromModel](https://github.com/VirtoCommerce/vc-module-catalog/blob/dc733fe8dd51ea83338d6948dd4214ca76ca3af1/VirtoCommerce.CatalogModule.Web.Core/Model/Product.cs#L323). Similar for ToModel. You could override them in your Product web model with your fields filling. – Andrey Kozlov Mar 12 '19 at 15:53
  • Thank you for the reply Andrey. I will try the things you mentioned. Thanks! – Tony Mar 15 '19 at 17:19