-
-
Notifications
You must be signed in to change notification settings - Fork 12
Quick Guide
Getting started with Nano is easy.
Simply, follow the step-by-step guide below.
1.1 Create a solution.
1.2 Create a new .NET Core 2.0+ web project.
1.3 Optionally, add docker-compose project (recommended).
1.4 Add NanoCore Nuget package to the project.
2.1 Open Startup.cs
, and replace its contents with following.
using Microsoft.Extensions.Configuration;
using Nano.App;
namespace Org.Service
{
public class Application : DefaultApplication
{
public Application(IConfiguration configuration)
: base(configuration)
{ }
}
}
2.2 Next, open program.cs
and replace it's contents with the following.
using Microsoft.AspNetCore.Hosting;
using Nano.App;
namespace Org.Service
{
public class Program
{
public static void Main()
{
DefaultApplication
.ConfigureApp<Application>()
.ConfigureServices(x =>
{
})
.Build()
.Run();
}
}
}
2.3 Last, open appsettings.json
and replace it's contents with the following.
Alter the values accordingly, see Configuration for details about the variables and their impact. Also, don't forget to alter any environment override files.
See default appsettings.json
here.
3.1 Modify, the Main()
method in program.cs
, by adding a dependency registration for the ILoggingProvider
. In this case the Serilog provider is used.
DefaultApplication
.ConfigureApp<Application>()
.ConfigureServices(x =>
{
x.AddLogging<SerilogProvider>();
})
.Build()
.Run();
4.1 Add a new class, and derive from DefaultDbContext
, which implements DbContext
from Entity Framework, as shown below.
using System;
using Microsoft.EntityFrameworkCore;
using Nano.Data.Models.Mappings.Extensions;
using Nano.Services.Data;
namespace Org.Service.Data
{
public class MyDbContext : DefaultDbContext
{
public MyDbContext(DbContextOptions options)
: base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
4.2 Modify, the Main()
method in program.cs
, by adding a dependency registration for the IDataProvider
and MyDbContext
. In this case the MySQL provider is used alongside the data context.
DefaultApplication
.ConfigureApp<Application>()
.ConfigureServices(x =>
{
x.AddLogging<SerilogProvider>();
x.AddDataContext<MySqlProvider, MyDbContext>();
})
.Build()
.Run();
5.1 Modify, the Main()
method in program.cs
, by adding a dependency registration for the IEventingProvider
. In this case EasyNetQProvider
is used.
DefaultApplication
.ConfigureApp<Application>()
.ConfigureServices(x =>
{
x.AddLogging<SerilogProvider>();
x.AddDataContext<MySqlProvider, MyDbContext>();
x.AddEventing<EasyNetQProvider>();
})
.Build()
.Run();
5.2 Create a class for the exchange.
namespace Org.Service.Models.Events
{
public class MyEvent
{
public virtual string PropertyOne { get; set; }
}
}
5.3 Publishing the event can be done from anywhere within the controller actions. The IEventing
interface is injected into controller constructors.
this.Eventing.Publish(@event);
5.4 Subscribing to the event is done by creating a class implementing the IEventHandler
interface, as shown below.
using System;
using Nano.Eventing.Interfaces;
using Nano.Services.Interfaces;
using Org.Service.Models.Events;
using Serilog;
namespace Org.Service.Eventing.Handlers
{
public class MyEventHandler : IEventingHandler<MyEvent>
{
protected virtual ILogger Logger { get; }
protected virtual IService Service { get; }
public MyEventHandler(ILogger logger, IService service)
{
this.Logger = logger;
this.Service = service;
}
public void Callback(MyEvent @event)
{
this.Logger.Information("Callback Invoked.");
}
}
}
6.1 Create Model.
using System.ComponentModel.DataAnnotations;
using Nano.Models;
namespace Org.Service.Models
{
public class MyEntity : DefaultEntity
{
[Required]
[MaxLength(255)]
public virtual string PropertyOne { get; set; }
}
}
6.2 Create Model Mapping.
using System;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Nano.Data.Models.Mappings;
using Org.Service.Models;
namespace Org.Service.Data.Models.Mappings
{
public class MyEntityMapping : DefaultEntityMapping<MyEntity>
{
public override void Map(EntityTypeBuilder<MyEntity> builder)
{
base.Map(builder);
builder
.Property(x => x.PropertyOne)
.HasMaxLength(255)
.IsRequired();
}
}
}
6.3 Add MyEntity
and MyEntityMapping
to the OnModelCreating(...)
method of the MyDbContext
, as shown below.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder
.AddMapping<MyEntity, MyEntityMapping>()
}
6.4 Add more properties, entities and mappings.
7.1 Create Query Criteria.
using DynamicExpression;
using Nano.Models.Criterias;
namespace Org.Service.Models.Criterias
{
public class MyQueryCriteria : DefaultQueryCriteria
{
public virtual string PropertyOne { get; set; }
public override CriteriaExpression GetExpression<TEntity>()
{
var filter = base.GetExpression<TEntity>();
if (this.PropertyOne != null)
filter.StartsWith("PropertyOne", this.PropertyOne);
return filter;
}
}
}
7.2 Create Controller.
using Microsoft.Extensions.Logging;
using Nano.Eventing.Interfaces;
using Nano.Services.Interfaces;
using Nano.Web.Controllers;
using Org.Service.Models;
using Org.Service.Models.Criterias;
namespace Org.Service.Controllers
{
public class MyController : DefaultController<MyEntity, MyQueryCriteria>
{
public MyController(ILogger logger, IService service, IEventing eventing)
: base(logger, service, eventing)
{ }
}
}
7.3 Add more criterias, controllers and actions. Optionally, override base class methods for extended functionality or add additional actions as needed.
8.1 The implementation is complete.
8.2 See full Nano solution implementation, with docker-compose, here