Skip to content

QuickStart

Felix edited this page Jun 9, 2022 · 6 revisions

QuickStart

This example easily show how to use RefaceCore.Modulazrization .

There are 6 steps to implement this example

  1. Create an empty project
  2. Add nuget package
  3. Create a class as a module definition
  4. Create a test interface and it's implement class
  5. Write code for startup
  6. Run

Now let's do it ....

Create an empty project

Create a ConsoleApp and specify the framework version is .Net Core 3.1.

Add nuget package

The following command will setup RefaceCore.Modularization to your project.

Install-Package RefaceCore.Modularization -Version 3.1.0

You can find the lastest version here

Create a class as a Module Definition

here is the explanation of Module Dfinition.

Create a class which is named QuickStartModule .

Adding the following namespace at the top of this file

using RefaceCore.Modularization;
using RefaceCore.Modularization.Attributes;
using RefaceCore.Modularization.Modules;

Let QuickStartModule extend from Module

Module is from RefaceCore.Modularization.Modules

public class QuickStartModule : Module
{
}

In order to use all features that were provided by RefaceCore.Modularization , we need to append dependency to QuickStartModule , like this :

[UseModule(
    typeof(BasicModule)
)]
public class QuickStartModule : Module
{
}

BasicModule include all features that were implements in RefaceCore.Modularization

Create a test interface and it's implement class

To demonstrate the feature of autometic registion, we need to create an interface and an implementation class .

In this example, we create an interface named IGreeting , and an implementation class named DefaultGretting , code like above:

// IGreeting.cs
namespace QuickStart.Services
{
    public interface IGreeting
    {
        void SayHello();
    }
}
// DefaultGreeting.cs
using RefaceCore.Modularization.Attributes;
using System;

namespace QuickStart.Services
{
    [RegisterAs(typeof(IGreeting))]
    public class DefaultGreeting : IGreeting
    {
        public void SayHello()
        {
            Console.WriteLine("HelloWorld!");
        }
    }
}

We noticed that the class DefaultGreeting was attributed RegsiterAs , This ATTRIBUTE will tell the framework , "I'm the service of IGreeting , Please help me to register into IOC-Container"

Write code for startup

Everything was prepaired except the code for startup .

There are 2 steps for startup

  1. Start all modules
  2. Custom your code when every module was started

first of all, let's write the code for Start all modules

Open the Program.cs , write the following code in the Main

static void Main(string[] args)
{
    IAppStarter starter = new SimpleAppStarter<OnStarted>(startArguments: args);
    starter.Start<QuickStartModule>();
}

Secondly , let's custom your code :

create a Class named OnStarted , implement from IOnStartedListener , name write your custom code in OnStart method , like this :

public class OnStarted : IOnStartedListener
{
    private readonly IGreeting greeting;

    public OnStarted(IGreeting greeting)
    {
        this.greeting = greeting;
    }

    void IOnStartedListener.OnStarted(object startArguments)
    {
        this.greeting.SayHello();
    }
}

Run

Now, everything was finished , let'u run the Program , you will find "HelloWorld" was shown in the Console .

It shows that the features of RefaceCore.Modulazrization were actived , and the component DefaultGreeint was registered into IOC-Container.


Related Links: