Pages - Menu

nopCommerce - Customization for Model

Scope

Due to business requirements on the category page, I need to extend and introduce some new fields to the CategoryModel.


Implementation

The way how nopCommerce designed architecture is that the commonly used classes are hinted by the keyword partial. This allows developers to extend the class in the same assembly without much effort. I previously stated that this is not a clean solution and I preferred an alternate method by using IoC.

I cannot however use IoC in this scenarios.

Model instantiation are handled inside the controllers, in fact inside the methods of the controllers!! This prevented me from using dependency injection for model instantiation.

The models in nopCommerce are not inherited to any interface. Without polymorphism, it is difficult to extend objects.


Partial class didn't take me much effort to extend and fairly easy to use. The drawback is I am ending up with many loose files in the project. This is however a feasible solution.

Is this an Anti-Pattern?

Nop.Web is an MVC project that has the model, view and controllers inside. Views are meant to be dirty, as they are the UI. Thus, the project is meant to be and would be dirty.

The truth is if you are using nopCommerce as a platform for multi-tenant websites, there are some clever ways of doing this such as controller factory. It gives the controllers and models a separation from the project.




nopCommerce - EF Configuration for SQL Composite Key

It is a common practice in the fashion industry that they have designer colors like crimson or scarlet for their fashion piece, but the majority of the consumers would only use common color name like red.

I build some custom SQL tables in nopCommerce by using Entity Framework Code First.



The following is the SQL tables, where color has a field IsCore to indicate if it is a core color, and ColorMapping has a composite key that does the mapping. Fairly straight forward.


In nopCommerce, I built a custom admin screen that does the mapping. For those who are curious, the titles are dynamically pulled from the core colors in the table with IsCore flag set to true. This is similar logics I get from the Shipping Method Restrictions screen.



So far so good. One thing that got me though is the EF Configuration.

Trying random things will get different kind of errors.





How about try to copy from the ShippingMethodMap? (It is located in Nop.Data\Mapping\Shipping\) Afterall, it has the same SQL structure. The .HasMany .WithMany approach were a bit messy and didn't quite well for me.

The simplest approach seems to work for me. In my ColorMappingMap, I created a composite key and ignore the field Id. Remember, we don't have Id in the SQL table.



While on the topic of fashion industry. I just received my invitations to the Mercedes-Benz Fashion Week Australia SS 14/15 in April. Who is going?




nopCommerce - Where is the DB Connection String?

Database Connection String

This maybe a frequently asked question in nopCommerce. Where do you change the connection string for database? Web.config has been the known location for many .Net web developers, but nopCommerce takes a different approach.

The string is stored in a plain text file called Settings.txt located in App_Data, not so obvious, but once you find it, it is obvious. :)


But Why??

As to why not use the web.config? That would be a question for the core team. Why not use web.config and take advantage of the web.config transformation to easily manage the differences between development, staging and live environments? I want to know the answer too!!



nopCommerce Plugin - Occasional "Temporary ASP.NET Files" Error


During the development of nopCommerce plugin, I am getting this not so useful null reference error. If you look careful enough though, this not so useful error did give me some hints though.

NullReferenceException: Object reference not set to an instance of an object.]
   ASP.<>c__DisplayClass1.<GenerateSubCategories>b__0(TextWriter __razor_helper_writer) in c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\683926bd\eece36df\App_Web_ta.plugin.widgets.megamenu.views.widgetsmegamenu.megamenu.cshtml.4107764b.if7iazw3.0.cs:0

It is complaining about an object not found in the temporary folder. Occasionally the temporary folder is outdated and Visual Studio is not able to clear during recompilation. I manually deleted the temporary folder, recompile and everything work fine again.

nopCommerce - Register route for admin controller

In nopCommerce, if you ever create a new or customize a controller in admin and getting 404 not found error? Read on...

With the approach of inheritance of customized controller, it is essential to register a new route for the new controller.

As a developer, it is natural to look at the RouteProvider.cs in the Admin project.



The original Nop Code looks like this which seems promising, but it doesn't do it.



The actual place to look at is hinted by the first image. The correct place to look at should be the Nop.Admin.AdminAreaRegistration.cs.


You can add your routes in here. In my example, it is registering a route for PictureController.


FileInfo.MoveTo() does not overwrite. Surprising or Interesting?

FileInfo.CopyTo() has an overload method that allow you to overwrite if destination file already exists. It makes sense as you sometimes want to overwrite the file.

Similarly you would expect that is made available to FileInfo.MoveTo() but it is not, no big deal as we understand just because it is Microsoft .Net Framework, doesn't mean everything is available to you.

What I found funny though is the following comments by Microsoft.


Ref: https://connect.microsoft.com/VisualStudio/feedback/details/321351/fileinfo-moveto-show-allow-file-overwrite

Did they really sound surprised by such a feature?

I wouldn't be surprised if I am asked to do overwrite in Windows Explorer.


Solutions

Without using much brain power, this is natural to me.


It was actually interesting to see an interesting comments describing this feature as interesting.


FileSystemWatcher - The process cannot access the file 'filepath' because it is being used by another process.

FileSystemWatcher

"The process cannot access the file 'filepath' because it is being used by another process."

One of the famous error of using FileSystemWatcher is the file access issue. It happens because the FileSystemWatcher.Created event is raised and the file copy is still in progress. ie. The event is raised when the watcher is notified with new file creating, but the file is not yet completed with its creation. Make sense?

In the ideal world, the created event should be raised when the object is "created" but it is not. There are many ways to do handle it and has been written by any people. Without spending too much time on this small matter (from my bigger business requirements), I have written an extension method to do it. Simple but effective.


And we will use it like this. In the event handler, we will check if the file is usable before we copy.





Possible Improvement

There are 2 ways I see this. One way to improve this is create your own event handler that will truly raised when an object is "created" completely. That's probably what most people expected.

However, if you go down to my quick solution, you may want to put in a retry count inside the while loop so it will exit gracefully after a number of tries. This is now an infinite loop afterall!!!