-
Notifications
You must be signed in to change notification settings - Fork 0
QuickStart
This example easily show how to use RefaceCore.Modulazrization .
There are 6 steps to implement this example
- Create an empty project
- Add nuget package
- Create a class as a module definition
- Create a test interface and it's implement class
- Write code for startup
- Run
Now let's do it ....
Create a ConsoleApp and specify the framework version is .Net Core 3.1.
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
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
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"
Everything was prepaired except the code for startup .
There are 2 steps for startup
- Start all modules
- 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();
}
}
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.