Overview

This framework contains all basic features. All others frameworks are based on this framework.



Features


Dependencies

Dependency injection is made via modules, you have to register your modules to the DependencyInjectorManager. Each module can specify which class to instanciate for an interface (or another class) or a key. Injection can be made depending on the caller specified when registering the class.


Environments

Environment Manager is made for store variables making them available everywhere into your application.


Factories

Factories helps you to instantiate your model object from dictionaries or maps or list of those. The default implementation is a basic mapping between keys of the dictionaries and properties of the model object. Of course you can customize this mapping by extending ModelProxy / DGFModelProxy.


Files

[iOS Only]

File manager is made for create, edit, read files from your bundle. You can also get information about files.


Indenters

Indenter is made for indent your JSON or XML files.


Introspections

Introspection manager contains many methods to proceed introspection into your classes.


Loggers

Loggers are made to define any output you desire for your log (console, file, webservice, ...).


Observables


Parsers


Repositories


Resolutions

[iOS Only]

Resources

[iOS Only]

Singletons


Transformers


Utils


Xml

[iOS Only]

Installation


Android


Integration is made via Maven, simple declare our maven repository in your gradle file and declare a dependencie to the framework you want. Here is a sample integration for DGFoundation :

            
repositories {
    maven {
        url 'http://maven.digipolitan.com/libraries-release/' }
}
dependencies {
    compile 'com.digipolitan.libraries:DGFoundation:1.0'
}
            
          


iOS


Cocoapods


1. You can integrate DGFoundation via CocoaPods, simply declare a dependency to the framework you want in your PodFile. Here is a sample integration for DGFoundation :

            
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '5.1.1'

pod 'DGFoundation'
            
          

2. Open the terminal, to browse to your project directory and type

            
pod install
            
          

3. You need to close your project and open the workspace generating by Cocoapod.



Nota : For more information about CocoaPods we suggest you to visit their website : http://cocoapods.org



Drag & Drop


1. You need to download the framework by clicking this link.
Then you have to drag and drop the framework into your project to use it.



Foundation - Step 1


Documentation


Dependency injection


Introduction

Dependency injection is made via modules, you have to register your modules to the DependencyInjectorManager. Each module can specify which class to instanciate for an interface (or another class) or a key. Injection can be made depending on the caller specified when registering the class.


Sample

Let’s say we want to create a base with characters with equipped weapons. We want to instantiate characters and by default, give them a weapon depending on their class. We’ll use dependency injection to realize that. Two different characters are enough for this sample, the Warrior and the Rogue, plus their respective weapons, an Axe and a Dagger


Creating the Activity

We use a very simple layout with two buttons to create our characters

            
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <Button
        android:id="@+id/create_warrior"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="Create Warrior" />

    <Button
        android:id="@+id/create_rogue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/create_warrior"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="Create Rogue" />

</RelativeLayout>
            
          

In the activity we have a simple list for our characters and the click listeners to create each class.

            
public class MainActivity extends FragmentActivity {
    private List<ICharacter> characters;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.create_warrior).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createCharacter(Warrior.class);
            }
        });

        findViewById(R.id.create_rogue).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createCharacter(Rogue.class);
            }
        });
    }

    private void createCharacter(Class<? extends ICharacter> clazz) {
        if (characters == null) {
            characters = new ArrayList<ICharacter>();
        }
        characters.add(DependencyInjectorHelper.inject(clazz));
    }
}
            
          

The interesting part here is :

            
characters.add(DependencyInjectorHelper.inject(clazz));
            
          

We call the DependencyInjectorHelper, an helper class to facilitate the use of the injector for injection, getting the DependencyInjectorManager shared instance for you and hiding some configuration methods used only to configure the manager at the startup.

Configuring the DependencyInjectionManager

Now we need to configure the manager and this is the role of the DependencyModule, you can add multiple modules to the manager, each module registering some classes for interfaces or other classes or keys.
Here comes the WeaponsModule

            
public class WeaponsModule extends DependencyModule {

    @Override
    public void setUp() {
        try {
            register(new IDependencyProvider() {
                @Override
                public Object provide(Map<String, Object> arg0) {
                    return new Axe();
                }
            }, IWeapon.class, Warrior.class);
            register(new IDependencyProvider() {
                @Override
                public Object provide(Map<String, Object> arg0) {
                    return new Dagger();
                }
            }, IWeapon.class, Rogue.class);
        } catch (Exception e) {
            LogManager.e(e);
        }
    }
}
            
          

As you can see we register two different classes for the interface IWeapon via the object DependencyProvider.
The provider allows you to do some work when an injection is required, here we create a provider for each weapon.
The first provider will return a new Axe when the user will ask for an IWeapon with a caller object of Warrior class.
The second will provide a Dagger if an IWeapon is requested by a Rogue object.

Now we need to add our module to the DependencyInjectorManager. We do this at the app startup in the MainActivity:

            
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bootstrap();
        setContentView(R.layout.activity_main);
        ...
            
          

Note that the bootstrap method here is created for the sample purpose, but if you use DGBootstrap there is a method bootstrap to override inside the DGApplication object.

The boostrap method

            
private void bootstrap() {
        DependencyInjectorManager manager = DependencyInjectorManager.getSharedInstance();
        manager.addModule(new WeaponsModule());
    }
            
          

We create our WeaponsModule and add it to the DependencyInjectorManager

Model classes

Now we have our activity that can create characters but we never injected weapons, this will be done in the AbstractCharacter class, when instantiated each character will ask a weapon to the injection manager

The interfaces :

            
public interface IWeapon {
    public int getDamages();
    public String getName();
}
            
          
            
public interface ICharacter {
    public String getName();
    public void setName(String name);
    public IWeapon getWeapon();
    public String getCharacterClass();
}
            
          

The abstract implementations

            
public abstract class AbstractWeapon implements IWeapon {

    public AbstractWeapon() {
        super();
        Log.d("AbstractWeapon", getName() + " created");
    }

    @Override
    public String getName() {
        return getClass().getSimpleName();
    }
}
            
          
            
public abstract class AbstractCharacter implements ICharacter {
    private IWeapon weapon;
    private String name;

    public AbstractCharacter() {
        this(null);
    }

    public AbstractCharacter(String name) {
        super();
        this.name = name;
        Log.d("AbstractCharacter", getCharacterClass() + " named " + getName() + " created");
        this.setWeapon(DependencyInjectorHelper.inject(IWeapon.class, this));
    }

    public String getCharacterClass() {
        return this.getClass().getSimpleName();
    }

    @Override
    public IWeapon getWeapon() {
        return weapon;
    }

    public void setWeapon(IWeapon weapon) {
        this.weapon = weapon;
        if (weapon != null) {
            Log.d("AbstractCharacter", weapon.getName() + " associated to " + getName());
        }
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }
}
            
          

As you can see when a character is created you should see a logcat entry notifying you the creation and the weapon equiped.
Another log will inform you of the instantiation of a weapon.
Take a look a this line

            
this.setWeapon(DependencyInjectorHelper.inject(IWeapon.class, this));
            
          

Here ask the injection of an IWeapon specifying this as caller, so the manager will receive a Warrior or a Rogue object as caller and will return the appropriate weapon for the character.

Now we need the final implementations of our basic model

            
public class Axe extends AbstractWeapon {

    public Axe() {
        super();
    }

    @Override
    public int getDamages() {
        return 25;
    }
}
            
          
            
public class Dagger extends AbstractWeapon {

    public Dagger() {
        super();
    }

    @Override
    public int getDamages() {
        return 20;
    }
}
            
          
            
public class Rogue extends AbstractCharacter {

    public static int count = 1;

    public Rogue() {
        this(null);
    }

    public Rogue(String name) {
        super(name == null ? "John " + Integer.toString(count++) : name);
    }
}
            
          
            
public class Warrior extends AbstractCharacter {
    public static int count = 1;

    public Warrior() {
        this(null);
    }

    public Warrior(String name) {
        super(name == null ? "Bob " + Integer.toString(count++) : name);
    }
}
            
          

These classes are not very important, you can do whatever you want with them, the idea is by extending AbstractCharacter, they will be given a weapon corresponding to their class.


Now you can use the dependency injection of DGFoundation. Note that you can find many different ways to use this, one of the most useful is to create two different modules, one for tablet application and one for phone application, then in each register your fragments implementations.


Then you do not need to bother which implementation you want when you develop your app, just ask for example, an instance of AbstractHomeFragment (extending Fragment, or AbstractFragment found in DGUserInterface) and show it. The only thing you have to do is to add the proper module depending on your actual configuration, and the EnvironmentManager in DGFoundation will help you to do that.