Elementary by StarKRE - 5

Frameworks

Unity Framework

Unknown VersionApache License 2.0Updated 56 days agoCreated on May 17th, 2020
Go to source

Elementary. Unity Framework

GitHub license image Download

DESIGN YOUR ARCHITECTURE ELEMENTARY

How to start?

Create a GameObject on scene and add MainScript.cs:

using ElementaryFramework.Core;
using UnityEngine;

public sealed class MainScript : MonoBehaviour
{
    private void Start()
    {
        Elementary.Initialize();
    }
}

Create a Simple Game Architecture (Example)

image

I. Create The Root Application Class

  1. Create MyGame class
using ElementaryFramework.Core;
using UnityEngine;

[Using]
public sealed class MyGame : Element, IRootElement
{
    //OnCreate is called after constructor
    public override void OnCreate(IElementContext context)
    {
        base.OnCreate(context);
        Debug.Log("Hello world!");
    }
}
  • [Using] - MyGame class will be used as element instance by the framework.
  • Element - the base class of the context
  • IRootElement - a root element in the system. It is created automatically
  1. Play Unity

Console: Hello world!

✅ DONE!

image


II. Create The Client

  1. Create Client class
using ElementaryFramework.Core;
using UnityEngine;

//Let's provide our client by interface for the MyGame class:
public interface IClient : IElement
{
}

[Using]
public sealed class Client : Element, IClient
{
    public override void OnCreate(IElementContext context)
    {
        base.OnCreate(context);
        Debug.Log("Client is created!");
    }
}

  1. Update MyGame.cs
using ElementaryFramework.Core;

[Using]
public sealed class MyGame : Element, IRootElement
{
    public IClient client { get; private set; }
    
    //OnCreate is called after constructor
    public override void OnCreate(IElementContext context)
    {
        base.OnCreate(context);
        Debug.Log("Hello world!");
        this.client = this.CreateElement<IClient>(typeof(Client));
    }
}
  1. Play Unity

Console: Hello world!

Console: Client is created!

✅ DONE!

image

III. Create The Repository Layer

  1. Create abstract Repository class and provide Client
using ElementaryFramework.Core;

public abstract class Repository : Element
{
    protected IClient client { get; private set; }

    //OnPrepare is called after all elements are created 
    public override void OnPrepare()
    {
        base.OnPrepare();
        this.client = this.GetRoot<MyGame>().client; //Provide client from MyGame class
    }
}
  1. Create some implementations of Repository for example
using ElementaryFramework.Core;
using UnityEngine;

[Using]
public sealed class UserRepository : Repository
{
    public override void OnCreate(IElementContext context)
    {
        base.OnCreate(context);
        Debug.Log("UserRepository is created!");
    }
}

[Using]
public sealed class LevelsRepository : Repository
{
    public override void OnCreate(IElementContext context)
    {
        base.OnCreate(context);
        Debug.Log("LevelsRepository is created!");
    }
}
  1. Create RepositoryLayer for repositories
using ElementaryFramework.Core;

//Keeps all repositories
//They are created automatically
[Using]
public sealed class RepositoryLayer : ElementLayer<Repository> 
{
    //Returns a repository. We will use this method from interactors later.
    public T GetRepository<T>() where T : Repository
    {
        return this.GetElement<T>();
    }
}
  • ElementLayer - group of T elements.
  1. Update MyGame.cs
using ElementaryFramework.Core;

[Using]
public sealed class MyGame : Element, IRootElement
{
    public IClient client { get; private set; }

    public RepositoryLayer repositoryLayer { get; private set; }
    
    //OnCreate is called after constructor
    public override void OnCreate(IElementContext context)
    {
        base.OnCreate(context);
        Debug.Log("Hello world!");
        this.client = this.CreateElement<IClient>(typeof(Client));
        this.repositoryLayer = this.CreateElement<RepositoryLayer>(typeof(RepositoryLayer));
    }
}
  1. Play Unity

Console: Hello world!

Console: Client is created!

Console: UserRepository is created!

Console: LevelsRepository is created!

✅ DONE!

image

IV. Create The Domain Layer

  1. Create abstract Interactor class and provide RepositoryLayer
using ElementaryFramework.Core;

public abstract class Interactor : Element
{
    private RepositoryLayer repositoryLayer;

    public override void OnPrepare()
    {
        base.OnPrepare();
        this.repositoryLayer = this.GetRoot<MyGame>().repositoryLayer;
    }
    
    //We write this method to provide repositories for inherited interactor classes
    protected T GetRepository<T>() where T : Repository
    {
        return this.repositoryLayer.GetRepository<T>();
    }
}
  1. Create some implementations of Interactor for example
using ElementaryFramework.Core;
using UnityEngine;


[Using]
public sealed class UserInteractor : Interactor
{
    public override void OnPrepare()
    {
        base.OnPrepare();
        var userRepository = this.GetRepository<UserRepository>();
        Debug.Log(____ROOT____quot;User interactor get {userRepository.GetType().Name}");
    }
}

[Using]
public sealed class LevelsInteractor : Interactor
{
    public override void OnPrepare()
    {
        base.OnPrepare();
        var levelsRepository = this.GetRepository<LevelsRepository>();
        Debug.Log(____ROOT____quot;Levels interactor get {levelsRepository.GetType().Name}");
    }
}
  1. Update MyGame.cs
using System.Collections.Generic;
using ElementaryFramework.Core;
using UnityEngine;

[Using]
public sealed class MyGame : Element, IRootElement
{
    public IClient client { get; private set; }

    public RepositoryLayer repositoryLayer { get; private set; }
    
    public IEnumerable<Interactor> interactors { get; private set; }

    //OnCreate is called after constructor
    public override void OnCreate(IElementContext context)
    {
        base.OnCreate(context);
        Debug.Log("Hello world!");
        this.client = this.CreateElement<IClient>(typeof(Client));
        this.repositoryLayer = this.CreateElement<RepositoryLayer>(typeof(RepositoryLayer));
        this.interactors = this.CreateElements<Interactor>();
    }
}
  1. Play Unity

Console: Hello world!

Console: Client is created!

Console: UserRepository is created!

Console: LevelsRepository is created!

Console: User interactor get UserRepository

Console: Levels interactor get LevelsRepository

✅ ✅ ✅ COMPLETE!

image

Show all projects by StarKRE