Inhalte aufrufen

Profilbild

Sample code for an import process?


  • Bitte melden Sie sich an, um eine Antwort zu verfassen.
4 Antworten zu diesem Thema

#1 jbright

jbright

    Member

  • Members
  • PunktPunkt
  • 12 Beiträge

Geschrieben: 12 February 2016 - 15:45

This might be a question for the general forum, but it is about importing from an existing ecommerce database. 

 

I have a lot of products (1,000s) in a custom ecommerce implementation. In order to properly import all of the categories, products, and images, I want to write a small console app to import the data over, using the native SmartStore objects to add the data. I can look at the SmartStore.Data.Setup to get a general idea on how to do the basic inputs, but I'm sure that there's a lot of configuration setup required in order to make it just work. I'd rather create a separate console app because this is a one-time process to port the data over. Using the excel process isn't ideal because there's also lots of media that I need to move over. 

 

QUESTION: has anyone done this before OR can anyone point at a small boiler plate bit of code that would initialize a console app so that I can create products, categories, etc? Or if one of the unit tests is a good unit test to look at, that would be helpful as well. (Example: is ProductPersistenceTests a good place to start?)

 

Thanks in advance.

 

JB


  • robinmuk gefällt das

#2 jbright

jbright

    Member

  • Members
  • PunktPunkt
  • 12 Beiträge

Geschrieben: 12 February 2016 - 16:25

Okay, this is actually REALLY easy to do. The unit tests were a good place to start looking. Not being an EF fan (so I don't use it), it was really making sure that my connection was set up properly. I created a new console app and then:

 

  1. Make sure to target .net 4.5.1 to match
  2. Since I was using the existing solution, I just referenced SmartStore.Core and SmartStore.Data
  3. Using the solution level nuget manager, made sure that EF was added to my console app.
  4. Added the correct connection string to my app.config

Then it's as simple as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SmartStore.Data;
using SmartStore.Core.Domain.Catalog;
using SmartStore.Core.Domain.Media;

namespace Import
{
    class Program
    {
        static void Main(string[] args)
        {
            var context = new SmartObjectContext("EC");

            var product = new Product
            {
                Name = "Test Product name 1",
                CreatedOnUtc = new DateTime(2010, 01, 03),
                UpdatedOnUtc = new DateTime(2010, 01, 04),
            };

            context.Set<Product>().Add(product);
            context.SaveChanges();

            Console.ReadKey();
        }
    }
}

Here's the app.config

  <connectionStrings>
    <add name="EC" providerName="System.Data.SqlClient" connectionString="Data Source=YOURSERVER-OR-LOCALHOST;Initial Catalog=EC2;Integrated Security=True;Persist Security Info=False;Enlist=False;Pooling=True;Min Pool Size=1;Max Pool Size=100;Connect Timeout=15;User Instance=False"/>
  </connectionStrings>

Now this will let me build out my process to read from my custom ecommerce database (I had already built this process) and write the data into the smart database. THAT should be really, really easy.



#3 jbright

jbright

    Member

  • Members
  • PunktPunkt
  • 12 Beiträge

Geschrieben: 16 February 2016 - 02:58

Oh... BTW, if you're doing low level raw data importing like this, you have to keep in mind that the slugs that are built and put into UrlRecord. It would be much, much better to use the service layer BUT the services don't do this for you (plus I'm a inject user and was too lazy to figure out all of the DI I'd need to set up to get AutoFac to work). It seems like the controllers sometimes do a little extra work anyways. So I figured out my data import process by looking at the controller and the services to see what data I needed to load up. This is throw-away code so I don't care so much that I'm bypassing the services layer. This wouldn't pass a code review, but it's getting my store populated!

 

The more that I'm diving into SmartStore, the more that I like it. I've been waiting for something better to come along in the C#/.net world that used a modern stack. The last thing I need to figure out is USPS shipping ... Pretty good platform guys!


  • robinmuk gefällt das

#4 Murat Cakir

Murat Cakir

    SmartStore AG

  • Administrators
  • 1118 Beiträge

Geschrieben: 17 February 2016 - 20:39

TIPP: the best place to look for import related stuff is the ImportManager itself: https://github.com/s...Manager.cs#L140. Please also analyze code for product picture import: https://github.com/s...Manager.cs#L693.

 

Of course all this could have been easier with direct service method calls, which unfortunately - as you correctly stated above - is not possible from a console app (unless you setup the DI container).


  • jbright gefällt das

Murat Cakir
SmartStore AG


#5 jbright

jbright

    Member

  • Members
  • PunktPunkt
  • 12 Beiträge

Geschrieben: 17 February 2016 - 22:05

Yeah, the DI container looked to be a bit of work for a quick hack to load data. If it were Ninject, I'd probably tackle it as I know that framework well.

 

No worries. I did figure out a lot of what I needed to do. Thanks!