vendredi 27 mai 2016

E-commerce > Improving adding to cart experience

I'm in the process of building a webshop and i'm trying to optimize user experience piece by piece.

In most webshops when you add an item to the cart the shop a) reloads the screen and adds the item b) uses ajax to add item and wait for response of the server.

I'm thinking about this: When the user adds an item, the user immidiatly gets a response on screen ( item xx is added to cart + nice subtle animation ), without waiting for a server response. To me this would give the user a more fluid shopping experience.

But what about an error? Well, if the server response is an error, i could present that information to the user. Since errors should not happen often (if it did, the code needs to be improved), i think this is an acceptable trade-off.

What do you think? Are there problems i'm not aware off?

Is it best practice to design Spring Application in such a way that we need to create same context multiple times?

I am going to start a new project using Spring framework. As I dont have much experience in Spring I need your help to sort out few confusions.

Lets look at use case

My application uses Spring integration framework. The core functionality of my app is,

  1. I need to poll multiple directories from file system,
  2. read the files(csv mostly),
  3. process some operations on them and insert them to database.

Currently I have set up spring integration flow for it. Which has inbound-chaneell-adapter for polling and then file traverse through the channels and at the end inserted into database.

My concerns are

  1. Number of directories application supposed to poll will be decided at runtime. Hence I need to create inbound-chanell-adapter at runtime (as one chanell adapter can poll only one directory at once) and cant define them statically in my spring context xml (As I dont know the how many I need).

  2. Each directory has certain properties which should be applied to the file while processing.(While going through the integration flow)

  3. So right now what I am doing is I am loading new ClassPathXmlApplicationContext("/applicationContext.xml"); for each directory. And cache the required properties in that newly created context. And use them at the time of processing (in <int:service-activator>).

Drawbacks of current design

  1. Separate context is created for each directory.
  2. Unnecessary beans are duplicated. (Database session factories and like)

So is there any way to design the application in such a way that context will not be duplicated. And still I can use properties of each directory throughout the integration flow at the same time???

Thanks in advance.

is this a decorator pattern (PHP)?

I was watching a Lynda.com PHP Design Patterns tutorial when the tutor presented this solution to a challenge:

index.php:

include_once 'decorator.php';

$object = new Decorator();
$object->sentence = "This is a sample sentence that we're going to   manipulate in the Decorator.";

// This should output: this is a sample sentence that we're going to     manipulate in the decorator.
echo $object->lower();

// This should output: THIS IS A SAMPLE SENTENCE THAT WE'RE GOING TO     MANIPULATE IN THE DECORATOR.
echo $object->uppercase();

decorator.php:

class Decorator
{
        public $sentence = '';

    public function lower()
{
    return strtolower($this->sentence);
}

public function uppercase()
{
    return strtoupper($this->sentence);
}

Why is this a decorator pattern? All I see is the instantiation of an object and accessing two of the objects methods.

powershell replace data in files sub directory (ok) only if line begins with some specific string (ko)

it s ok to replace a string in all files sub directory with this :

initial in line : 02#APPLICATION/PDF;name="P31F9B000A00001.pdf"* replace by : # P31F9B000A00001 can be anything

with this pattern :

$array = @("D:\cebi")
$myRegExp1 = [regex]"#APPLICATION/PDF;name=""(.*).pdf""\*"
$myRegToReplace1 = "#"
$myRegExp2 = [regex]"\*ClasseAutomatique\*2016\*"
$myRegToReplace2 = "*ClasseAutomatique*2016*Faux*"
foreach ($element in $array) {
    Get-ChildItem $element  -Recurse -Include "*.DAT" | foreach-object {Copy-Item $_.fullname ($_.fullname+".bak") -recurse } 
    gci $element -r -include "*.DAT"| foreach-object { $a = $_.fullname; ( get-content $a ) |  foreach-object { $_ -replace $myRegExp1,$myRegToReplace1 -replace $myRegExp2,$myRegToReplace2  }  | set-content $a } 

}

But now, i would like do replace a string only for the lines begins by a specific string in bold, it can be anything

i want to replace string "ClasseAutomatique" by "ok_man" only if line begins by "02#APPLICATION/PDF;name=" intial string :

02#APPLICATION.PDF;name="P31F9B000A00001.pdf"-anything_data.2016.##_ClasseAutomatique_2016__##P31F9B000A00001****.pdf 01#APPLICATION.PDF;name="**P31F9B123D02651.pdf"-anything_data.2016.##_ClasseAutomatique_2016__##**P31CKB000D00002****.pdf

Result would be :

02#APPLICATION.PDF;name="P31F9B000A00001.pdf"-anything_data.2016.##_ok_man_2016__##P31F9B000A00001.pdf 01#APPLICATION.PDF;name="P31F9B123D02651.pdf"-anything_data.2016.##_ClasseAutomatique_2016__##P31CKB000D00002.pdf

i try this :

$myRegExp3 = [regex]"^(02#APPLICATION/PDF;name=(.).pdf)-(.).2016.##_(ClasseAutomatique)_2016__##(.*).pdf"

$myRegToReplace3 = "$1-$2.2016.##_ok_man_2016__##$4.pdf"

but nothing

Referencing several models from one model class- Bad practice?

I want to obtain all notifications for current user with this code:

current_user.follows.each do |follow|
    follow.followable.open_notifications.each do |notification|
        if !notification.reads.include?(current_user)
            @open_notifications += notification 
        end
    end
end

So far I had this code in my controller however I know this kind of logic should be place in model class. After moving code:

OpenNotification Controller:

OpenNotification.fetch_unread(current_user) 

OpenNotification Model:

def self.fetch_unread(user)
    @open_notifications = []
    user.follows.each do |follow|
        follow.followable.open_notifications.each do |notification|
            if !notification.reads.include?(user)
                @open_notifications += notification 
            end
        end
    end
    @open_notifications
end

EDIT:

Classes involved:

  • User
  • Follow - who(user) follows what(followable)
  • Followable(polymorphic - may be User, Event or a Place)
  • OpenNotification - stores information about the change of followable object
  • Read - who read which notification (user_id and open_notification_id)

User:

has_many :follows, class_name: 'Follow',
                 source: :user

has_many :follows_as_fallowable, 
                class_name: 'Follow',
                as: :followable

has_many :followers, through: :follows_as_fallowable,
                   source: :user

Event:

has_many :follows, as: :followable
has_many :followers, through: :follows,
                         source: :user

has_many :open_notifications, as: :followable  

OpenNotification:

belongs_to :followable, polymorphic: true
has_many :reads

Reads:

belongs_to :user
belongs_to :open_notification


My question is if it is a good practice to refer to multiple classes from the class responsible for one particular resource?
If this is not a good practice, how the code should be refactored?

Design base class for derived classes with functions having different signatures

class B {
public:
    virtual void foo(?);
}
class D1 : public B {
public:
    void foo(T1*);
}
class D2 : public B {
public:
    void foo(T2*);
}

B* b1 = new D1();
B* b2 = new D2();

T1 and T2 may not be related.

How do I design the classes such that called b1 and b2 call the correct foo with correct type?

Can use upto c++14.

Passing around parsed arguments makes no fun

I use arparse in Python to parse arguments from the command line:

def main():
    parser = argparse.ArgumentParser(usage=usage)
    parser.add_argument('-v', '--verbose', dest='verbose', action='store_true')
    parser.add_argument(...)
    args = parser.parse_args()

I use object args only in few places of the code.

There are three methods and the call stack looks like this

def first_level(args):
    second_level()

def second_level():
    third_level()

def third_level():
    ### here I want to add some logging if args.verbose is True

I want to add some logging to third_level().

I don't like to change the signature of the method second_level().

How can I make the arg object available in third_lelvel()?

I could store arg as global variable, but I was told to not use global variables in a developer training some years ago ....

What is common way to handle this?