lundi 27 avril 2015

DDD Bounded Contexts with Onion Architecture

I have the following Onion Architecture framework.

  • Domain
    • Entities - For my domain entities
    • Interfaces - For my domain interfaces
    • Services - For my domain services
  • Infrastructure
    • Data - For Fluent NHibernate persistence
    • Interfaces - For infrastructure interfaces
    • Logging - Just an interface for logging in case I want to switch out my logging library to something else.
    • Dependency Resolution - Most of my IoC registrations goes in here.
  • Services
    • Interfaces - Application service interfaces goes in here, they will get implemented in the UI project.
  • Tests
    • Infrastructure Tests - For testing infrastructure services etc.
    • Domain Tests - For testing domain models and services
  • Web
    • UI - User interface project where I implement application services, user interface, etc ...

With Domain Driven Development one would identify the Bounded Contexts. Most of the literature on the internet states that each Bounded Context needs to be abstracted into their own project or namespace.

  1. Is my approach then incorrect for having all my Domain Models in one project and all my Domain Services in another project? Does it really matter not having different bounded context's in different namespaces or projects?
  2. If you have a Model A which is used my Bounded Context A, but Bounded Context B, Bounded Context C, etc also needs to use the exact same Model A, Do you allow them to use that exact same model, or do you create a new model for each Bounded Context?

I am new to DDD so sorry if this question is a dumb question. I find myself understanding something better if I ask a question and get a good explanation as an answer.

Any Help will be much appreciated.

What is the correct regular expression for finding specific pattern in these lines?

So I have one large file that contains a bunch of weather data. I have to allocate each line from the large file into its corresponding state file. So there will be a total of 50 new state files with their own data.

The large file contains ~1 million lines of records like this:

COOP:166657,'NEW IBERIA AIRPORT ACADIANA REGIONAL LA US',200001,177,553

Although the name of the station can vary and have different number of words.

This is the regular expression I am using:

Pattern p = Pattern.compile(".* ([A-Z][A-Z]) US.*"); 
Matcher m = p.matcher(line);

When I run my program there are still instances of lines in which the pattern could not be found.

This is my program:

package climate;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This program will read in a large file containing many stations and states,
 * and output in order the stations to their corresponding state file.
 * 
 * Note: This take a long time depending on processor. It also appends data to
 * the files so you must remove all the state files in the current directory
 * before running for accuracy.
 * 
 * @author Marcus
 *
 */

public class ClimateCleanStates {

    public static void main(String[] args) throws IOException {

        Scanner in = new Scanner(System.in);
        System.out
                .println("Note: This program can take a long time depending on processor.");
        System.out
                .println("It is also not necessary to run as state files are in this directory.");
        System.out
                .println("But if you would like to see how it works, you may continue.");
        System.out.println("Please remove state files before running.");
        System.out.println("\nIs the States directory empty?");
        String answer = in.nextLine();

        if (answer.equals("N")) {
            System.exit(0);
            in.close();
        }
        System.out.println("Would you like to run the program?");
        String answer2 = in.nextLine();
        if (answer2.equals("N")) {
            System.exit(0);
            in.close();
        }

        String[] statesSpaced = new String[51];

        File statefile, dir, infile;

        // Create files for each states
        dir = new File("States");
        dir.mkdir();


        infile = new File("climatedata.csv");
        FileReader fr = new FileReader(infile);
        BufferedReader br = new BufferedReader(fr);

        String line;
        System.out.println();

        // Read in climatedata.csv
        // Probably need to implement ClimateRecord class
        final long start = System.currentTimeMillis();
        while ((line = br.readLine()) != null) {
            // Remove instances of -9999

            if (!line.contains("-9999")) {



                        Pattern p = Pattern.compile("^.* ([A-Z][A-Z]) US.*$"); 
                        Matcher m = p.matcher(line);
                        String stateFileName = null;

                        if(m.find()){
                            //System.out.println(m.group(1));
                            stateFileName = m.group(1);
                        } else {
                            System.out.println("Could not find abbreviation");
                        }

                        /*
                        stateFileName = "States/" + stateFileName + ".csv";
                        statefile = new File(stateFileName);

                        FileWriter stateWriter = new FileWriter(statefile, true);
                        stateWriter.write(line + "\n");
                        // Progress reporting
                        System.out.printf("Writing [%s] to file [%s]\n", line,
                                statefile);
                        stateWriter.flush();
                        stateWriter.close();
                        */





            }
        }
        System.out.println("Elapsed " + (System.currentTimeMillis() - start) + " ms");
        br.close();
        fr.close();
        in.close();

    }

}

Factory Pattern to build many derived classes

I have a factory object ChallengeManager to generate instances of a Challenge object for a game I'm building. There are many challenges. The constructors for each Challenge class derivation are different, however there is a common interface among them, defined in the base class.

When I call manager.CreateChallenge(), it returns an instance of Challenge, which is one of the derived types.

Ideally, I would like to keep the code for the object construction inside the derived class itself, so all the code related to that object is co-located. Example:

class Challenge {}

class ChallengeA : Challenge {
  public static Challenge MakeChallenge() {
    return new ChallengeA();
  }
}

class ChallengeB : Challenge {
  public static Challenge MakeChallenge() {
    return new ChallengeB();
  }
}

Now, my ChallengeManager.CreateChallenge() call only needs to decide the class to call MakeChallenge() on. The implementation of the construction is contained by the class itself.

Using this paradigm, every derived class must define a static MakeChallenge() method. However, since the method is a static one, I am not able to make use of an Interface here, requiring it.

It's not a big deal, since I can easily remember to add the correct method signature to each derived class. However, I am wondering if there is a more elegant design I should consider.

What is the Meteor pattern for file inclusion called?

I really like the Meteorjs style of automagically importing every file in your project in a specified order. Does that style of coding have a name? Sandbox, openbox, open coding?

Detect interesting patterns machine learning

I am starting work with a machine learning project for data risk management. Assume we have 1 million unstructured text documents, with some meta data like Data Owner for each text.(We can do any assumption)

One of the goal is to detect interesting patterns. But I do not really know what does this requirement mean. Could anybody tell me this? or give some related key words? (not ask for implementation) it's a quite open question. Hope interesting ideas!!

Android - How to manage multiples user levels and types

I have project be used by different group then user.

For example: one group is secretary user, another is the admin user, medical user.

Currently my code this so below:

 if(App.getInstance().getCustomer().isAdmin())
        replaceFragment(R.id.view_main,new AdminFragment);
 if(App.getInstance().getCustomer().isMedical())
        replaceFragment(R.id.view_main,new MedicalFragment);

My question is, what is the way more sophisticated to accomplish this control

Design feedback

Can you please give me a feedback about this design implementation ?

Suppose i need to calculate a salary of an Employer, this salary is depending on something called : REGIME, the REGIME is either newRegime or OldRegime.

Suppose the calculation should be as following :

If (newRegime) then Salary = 10; If (oldRegime) then Salary = 30 if 30 is superior then newRegime.

I tought about this implementation :

An interface : SalaryBusiness

    public interface SalaryBusiness {

    double getSalary();
}

A Class SalaryCalculator :

public class SalaryCalculator implements SalaryBusiness {

// Some shared fileds, useful for the calculation....

// A public constructor
 public SalaryCalculator()
{
}

protected SalaryCalculator(Object anObject)
{

// the anObject is useful to init the shared fileds 
initFields(anObject)
}
//Method that creat a Regime 
public final SalaryCalculator getRegimeCalculator(Object anObject)
{
if (newRegimeCalculator.equals(anObject.getRegime))
{
return new newRegimeCalculator(anObject);
}else {

NewRegimeCalculator newRegimeCalcultor = new NewRegimeCalculator(anObject); 
OldRegimeCalculator oldRegimeCalculator = new OldRegimeCalculator(anObject);
oldRegimeCalculator.setTauxNr = newRegime.getSalary();
return oldRegimeCalculator
}

@override
public double getSalary()
{
return 0.0d;
}

}

// Child classes : NewRegimeCalculator / OldRegimeCalculator

    public class NewRegimeCalculator extends SalaryCalculator {

       // Special fileds...

       // Procted constructor 
       proctected NewRegimeCalculator(Object anObject) {
       super(anObject);
    }

    @override 
    public double getSalary()
    {
    double salary = 0.0d;
      //Traitements....
     return salary;
    }
}



public class OldRegimeCalculator extends SalaryCalculator {

       // Field holding the newRegime salary
       double tauxNr = 0.0d;
       // Special fileds...

       // Procted constructor 
       proctected OldRegimeCalculator(Object anObject) {
       super(anObject);
    }

    @override 
    public double getSalary()
    {
    double salary = 0.0d;
      //Traitements....
     if (tauxNr>salary)
     {
return tauxNr;
}
else {
return salary;
}
    }

// The Client using the design

public classWantToGetTheSalary {

Object anObject;
double salary;
   SalaryCalculator regimeCalculator = new SalaryCalculator().getRegimeCalculator(anObject);

//Here is our salary 
salary = regimeCalculator.getSalary();


}

I am sorry if this is hard to read, i would so greatful if you can give me some feedback, whats the prons & cons of this implementation ? is it worth to get on production ? is it a just crap code ? does it respect the SOLID Principles ?.... Thank you in advance :)