Skip to content

ahmetcandan/CodeNet_Example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logo


CodeNet

CodeNet.Core

CodeNet.Core is a .Net library.

Installation

Use the package manager npm to install CodeNet.Core

dotnet add package CodeNet.Core

Usage

appSettings.json

{
  "Application": {
    "Name": "Customer",
    "Title": "StokTakip | Customer API",
    "Version": "v1.0"
  },
  "JWT": {
    "ValidAudience": "http://codenet",
    "ValidIssuer": "http://login.codenet",
    "PublicKeyPath": "public_key.pem"
  }
}

program.cs

using CodeNet.Core.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddCodeNet("Application");
builder.AddAuthenticationWithAsymmetricKey("JWT");
//...

var app = builder.Build();
app.UseCodeNet(builder.Configuration, "Application");
//...
app.Run();

CodeNet.Elasticsearch

CodeNet.Elasticsearch is a .Net library.

Installation

Use the package manager npm to install CodeNet.Elasticsearch

dotnet add package CodeNet.Elasticsearch

Usage

appSettings.json

{
  "Elasticsearch": {
    "Hostname": "localhost:9200",
    "Username": "elastic",
    "Password": "password"
  }
}

program.cs

using CodeNet.Elasticsearch.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddElasticsearch("Elasticsearch");
//...

var app = builder.Build();
//...
app.Run();

Repository

public class TestElasticRepository : ElasticsearchRepository<ElasticModel>
{
    public TestElasticRepository(ElasticsearchDbContext dbContext) : base(dbContext)
    {
        //...
    }
}

Model

[IndexName("Test")]
public class ElasticModel : IElasticsearchModel
{
    public Guid Id { get; set; }
    public DateTime Date { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

CodeNet.EntityFramework

CodeNet.EntityFramework is a .Net library.

Installation

Use the package manager npm to install CodeNet.EntityFramework

dotnet add package CodeNet.EntityFramework

Usage

appSettings.json

{
  "ConnectionStrings": {
    "SqlServer": "Data Source=localhost;Initial Catalog=TestDB;TrustServerCertificate=true"
  }
}

program.cs

using CodeNet.EntityFramework.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddDbContext<CustomerDbContext>("SqlServer");
//or
builder.AddDbContext<CustomerDbContext>(options => options.UseSqlServer(builder.Configuration, "SqlServer"));
//...

var app = builder.Build();
//...
app.Run();

DbContext

public partial class CustomerDbContext(DbContextOptions<CustomerDbContext> options) : DbContext(options)
{
    public virtual DbSet<Model.Customer> Customers { get; set; }
    public virtual DbSet<Employee> Employees { get; set; }
}

Repository

public class CustomerRepository(CustomerDbContext context, IIdentityContext identityContext) : 
    TracingRepository<Model.Customer>(context, identityContext), ICustomerRepository
{
}

Repository Usage

public class CustomerService(ICustomerRepository CustomerRepository, IAutoMapperConfiguration Mapper) : BaseService, ICustomerService
{
    public async Task<CustomerResponse> CreateCustomer(CreateCustomerRequest request, CancellationToken cancellationToken)
    {
        var model = Mapper.MapObject<CreateCustomerRequest, Model.Customer>(request);
        var result = await CustomerRepository.AddAsync(model, cancellationToken);
        await CustomerRepository.SaveChangesAsync(cancellationToken);
        return Mapper.MapObject<Model.Customer, CustomerResponse>(result);
    }

    public async Task<CustomerResponse> DeleteCustomer(int customerId, CancellationToken cancellationToken)
    {
        var result = await CustomerRepository.GetAsync([customerId], cancellationToken);
        CustomerRepository.Remove(result);
        await CustomerRepository.SaveChangesAsync(cancellationToken);
        return Mapper.MapObject<Model.Customer, CustomerResponse>(result);
    }

    public async Task<CustomerResponse?> GetCustomer(int customerId, CancellationToken cancellationToken)
    {
        var result = await CustomerRepository.GetAsync([customerId], cancellationToken) ?? throw new UserLevelException("01", "Kullanıcı bulunamadı!");
        return Mapper.MapObject<Model.Customer, CustomerResponse>(result);
    }

    public async Task<CustomerResponse> UpdateCustomer(UpdateCustomerRequest request, CancellationToken cancellationToken)
    {
        var result = await CustomerRepository.GetAsync([request.Id], cancellationToken);
        result.Code = request.Code;
        result.Description = request.Description;
        result.Name = request.Name;
        result.No = request.No;
        CustomerRepository.Update(result);
        await CustomerRepository.SaveChangesAsync(cancellationToken);
        return Mapper.MapObject<Model.Customer, CustomerResponse>(result);
    }
}

CodeNet.EntityFramework.InMemory

CodeNet.EntityFramework.InMemory is a .Net library.

Installation

Use the package manager npm to install CodeNet.EntityFramework.InMemory

dotnet add package CodeNet.EntityFramework.InMemory

Usage

program.cs

using CodeNet.EntityFramework.InMemory.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddInMemoryDB("DatabaseName");
//...

var app = builder.Build();
//...
app.Run();

CodeNet.EntityFramework.MySQL

CodeNet.EntityFramework.MySQL is a .Net library.

Installation

Use the package manager npm to install CodeNet.EntityFramework.MySQL

dotnet add package CodeNet.EntityFramework.MySQL

Usage

appSettings.json

{
  "ConnectionStrings": {
    "MySQL": "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"
  }
}

program.cs

using CodeNet.EntityFramework.MySQL.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddMySQL("MySQL");
//...

var app = builder.Build();
//...
app.Run();

CodeNet.EntityFramework.Oracle

CodeNet.EntityFramework.Oracle is a .Net library.

Installation

Use the package manager npm to install CodeNet.EntityFramework.Oracle

dotnet add package CodeNet.EntityFramework.Oracle

Usage

appSettings.json

{
  "ConnectionStrings": {
    "Oracle": "Data Source=MyOracleDB;Integrated Security=yes;"
  }
}

program.cs

using CodeNet.EntityFramework.Oracle.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddOracle("Oracle");
//...

var app = builder.Build();
//...
app.Run();

CodeNet.EntityFramework.PostgreSQL

CodeNet.EntityFramework.PostgreSQL is a .Net library.

Installation

Use the package manager npm to install CodeNet.EntityFramework.PostgreSQL

dotnet add package CodeNet.EntityFramework.PostgreSQL

Usage

appSettings.json

{
  "ConnectionStrings": {
    "PostgreSQL": "User ID=root;Password=myPassword;Host=localhost;Port=5432;Database=myDataBase;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;"
  }
}

program.cs

using CodeNet.EntityFramework.PostgreSQL.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddNpgsql("PostgreSQL");
//...

var app = builder.Build();
//...
app.Run();

CodeNet.EntityFramework.Sqlite

CodeNet.EntityFramework.Sqlite is a .Net library.

Installation

Use the package manager npm to install CodeNet.EntityFramework.Sqlite

dotnet add package CodeNet.EntityFramework.Sqlite

Usage

appSettings.json

{
  "ConnectionStrings": {
    "Sqlite": "Data Source=mydb.db;Version=3;"
  }
}

program.cs

using CodeNet.EntityFramework.Sqlite.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddSqlite("Sqlite");
//...

var app = builder.Build();
//...
app.Run();

CodeNet.ExceptionHandling

CodeNet.ExceptionHandling is a .Net library.

Installation

Use the package manager npm to install CodeNet.ExceptionHandling

dotnet add package CodeNet.ExceptionHandling

Usage

appSettings.json

{
  "DefaultErrorMessage": {
	"MessageCode": "EX0001",
	"Message": "An unexpected error occurred!"
  }
}

program.cs

using CodeNet.ExceptionHandling.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddDefaultErrorMessage("DefaultErrorMessage")
//...

var app = builder.Build();
//...
app.UseExceptionHandling(); //This should be used last.
app.Run();

Example Error Message

{
  "MessageCode": "EX0001",
  "Message": "An unexpected error occurred!"
}

CodeNet.HealthCheck

CodeNet.HealthCheck is a .Net library.

Installation

Use the package manager npm to install CodeNet.HealthCheck

dotnet add package CodeNet.HealthCheck

Usage

program.cs

using CodeNet.HealthCheck.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
    .AddCodeNetHealthCheck();
//...

var app = builder.Build();
app.UseHealthChecks("/health");
//...
app.Run();

CodeNet.HealthCheck.Elasticsearch

CodeNet.HealthCheck.Elasticsearch is a .Net library.

Installation

Use the package manager npm to install CodeNet.HealthCheck.Elasticsearch

dotnet add package CodeNet.HealthCheck.Elasticsearch

Usage

program.cs

using CodeNet.HealthCheck.Elasticsearch.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
    .AddElasticsearchHealthCheck();
//...

var app = builder.Build();
app.UseHealthChecks("/health");
//...
app.Run();

CodeNet.HealthCheck.EntityFramework

CodeNet.HealthCheck.EntityFramework is a .Net library.

Installation

Use the package manager npm to install CodeNet.HealthCheck.EntityFramework

dotnet add package CodeNet.HealthCheck.EntityFramework

Usage

program.cs

using CodeNet.HealthCheck.EntityFramework.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
    .AddEntityFrameworkHealthCheck();
//...

var app = builder.Build();
app.UseHealthChecks("/health");
//...
app.Run();

CodeNet.HealthCheck.MongoDB

CodeNet.HealthCheck.MongoDB is a .Net library.

Installation

Use the package manager npm to install CodeNet.HealthCheck.MongoDB

dotnet add package CodeNet.HealthCheck.MongoDB

Usage

program.cs

using CodeNet.HealthCheck.MongoDB.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
    .AddMongoDbHealthCheck();
//...

var app = builder.Build();
app.UseHealthChecks("/health");
//...
app.Run();

CodeNet.HealthCheck.RabbitMQ

CodeNet.HealthCheck.RabbitMQ is a .Net library.

Installation

Use the package manager npm to install CodeNet.HealthCheck.RabbitMQ

dotnet add package CodeNet.HealthCheck.RabbitMQ

Usage

program.cs

using CodeNet.HealthCheck.RabbitMQ.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
    .AddRabbitMqHealthCheck(builder, "RabbitMQ");
//...

var app = builder.Build();
app.UseHealthChecks("/health");
//...
app.Run();

CodeNet.HealthCheck.Redis

CodeNet.HealthCheck.Redis is a .Net library.

Installation

Use the package manager npm to install CodeNet.HealthCheck.Redis

dotnet add package CodeNet.HealthCheck.Redis

Usage

program.cs

using CodeNet.HealthCheck.Redis.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
    .AddRedisHealthCheck();
//...

var app = builder.Build();
app.UseHealthChecks("/health");
//...
app.Run();

CodeNet.HttpClient

CodeNet.HttpClient is a .Net library.

Installation

Use the package manager npm to install CodeNet.HttpClient

dotnet add package CodeNet.HttpClient

Usage

program.cs

using CodeNet.HttpClient.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddHttpClient();
//...

var app = builder.Build();
//...
app.Run();

CodeNet.Identity

This is a .Net library.

Installation

Use the package manager npm to install CodeNet.Identity

dotnet add package CodeNet.Identity

Usage

appSettings.json

{
  "Application": {
    "Name": "Identity",
    "Title": "CodeNet | Identity API",
    "Version": "v1.0"
  },
  "ConnectionStrings": {
    "SqlServer": "Data Source=localhost;Initial Catalog=CodeNet;TrustServerCertificate=true"
  },
  "Identity": {
    "ValidAudience": "http://code.net",
    "ValidIssuer": "http://login.code.net",
    "ExpiryTime": 5.0,
    "PublicKeyPath": "public_key.pem",
    "PrivateKeyPath": "private_key.pem"
  }
}

program.cs

using CodeNet.Core.Extensions;
using CodeNet.EntityFramework.Extensions;
using CodeNet.Identity.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddNetCore("Application")
       .AddAuthenticationWithAsymmetricKey("Identity")
       .AddIdentityWithAsymmetricKey(options => options.UseSqlServer(builder.Configuration, "SqlServer"), "Identity");
       

var app = builder.Build();
//...
app.Run();

CodeNet.Logging

CodeNet.Logging is a .Net library.

Installation

Use the package manager npm to install CodeNet.Logging

dotnet add package CodeNet.Logging

Usage

program.cs

using CodeNet.Logging.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.AddLogging("Logging");
//...

var app = builder.Build();
app.UseLogging();
//...
app.Run();

CodeNet.MakerChecker

CodeNet.MakerChecker is a .Net library.

Installation

Use the package manager npm to install CodeNet.MakerChecker

dotnet add package CodeNet.MakerChecker

Usage

appSettings.json

{
  "ConnectionStrings": {
    "SqlServer": "Data Source=localhost;Initial Catalog=TestDB;TrustServerCertificate=true"
  }
}

program.cs

var builder = WebApplication.CreateBuilder(args);
builder.AddMakerChecker(options => options.UseSqlServer(builder.Configuration, "SqlServer"), "Identity");
//...

var app = builder.Build();
//...
app.Run();

Example Model

public class TestTable : MakerCheckerEntity
{
    public int Id { get; set; }
    public required string Name { get; set; }
}

Repository

public class TestTableRepository(MakerCheckerDbContext dbContext, ICodeNetContext identityContext) : MakerCheckerRepository<TestTable>(dbContext, identityContext)
{
}

CodeNet.Mapper

CodeNet.Mapper is a .Net library.

Installation

Use the package manager npm to install CodeNet.Mapper

dotnet add package CodeNet.Mapper

Usage

program.cs

using CodeNet.Mapper.Module;

var builder = WebApplication.CreateBuilder(args);
builder.AddMapper();
//...

var app = builder.Build();
//...
app.Run();

CodeNet.MongoDB

CodeNet.MongoDB is a .Net library.

Installation

Use the package manager npm to install CodeNet.MongoDB

dotnet add package CodeNet.MongoDB

Usage

appSettings.json

{
  "MongoDB": {
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "CodeNet"
  }
}

program.cs

var builder = WebApplication.CreateBuilder(args);
builder.AddMongoDB("MongoDB");
//...

var app = builder.Build();
//...
app.Run();

Sample Repositoriy

public class SampleRepository(MongoDBContext dbContext) : BaseMongoRepository<KeyValueModel>(dbContext), ISampleRepository
{
    //...
}

KeyValueModel

[CollectionName("KeyValue")]
public class KeyValueModel : BaseMongoDBModel
{
    public string Key { get; set; }
    public string Value { get; set; }
}

CodeNet.Parameters

CodeNet.Parameters is a .Net library.

Installation

Use the package manager npm to install CodeNet.Parameters

dotnet add package CodeNet.Parameters

Usage

appSettings.json

{
  "ConnectionStrings": {
    "SqlServer": "Data Source=localhost;Initial Catalog=TestDB;TrustServerCertificate=true"
  }
}

program.cs

var builder = WebApplication.CreateBuilder(args);
builder.AddParameters(options => options.UseSqlServer(builder.Configuration, "SqlServer"), "Identity");
//...

var app = builder.Build();
//...
app.Run();

CodeNet.RabbitMQ

CodeNet.RabbitMQ is a .Net library.

Installation

Use the package manager npm to install CodeNet.RabbitMQ

dotnet add package CodeNet.RabbitMQ

Usage

appSettings.json

{
  "RabbitMQ": {
    "Hostname": "localhost",
    "Username": "guest",
    "Password": "guest",
    "Exchange": "",
    "RoutingKey": "RoutingKey",
    "Queue": "QueueName",
    "Durable": false,
    "AutoDelete": false,
    "Exclusive": false,
    "AutoAck": true
  }
}

program.cs

using CodeNet.Core.Extensions;
using CodeNet.RabbitMQ.Extensions;
using CodeNet.RabbitMQ.Module;
using ExampleApi.Models;

var builder = WebApplication.CreateBuilder(args);
builder
    .AddRabbitMQProducer("RabbitMQ")
    .AddRabbitMQConsumer("RabbitMQ");
//...

var app = builder.Build();
app.UseRabbitMQConsumer<QueueModel>();
//...
app.Run();

Usage Producer

public class MessageProducer(IRabbitMQProducerService<QueueModel> Producer)
{
    public async Task<ResponseBase> Send(MessageProducerRequest request, CancellationToken cancellationToken)
    {
        Producer.Publish(request.Data);
        return new ResponseBase("200", "Successfull");
    }
}

Usage Consumer

public class MessageConsumerHandler : IRabbitMQConsumerHandler<KeyValueModel>
{
    public void Handler(ReceivedMessageEventArgs<KeyValueModel> args)
    {
        Console.WriteLine($"MessageId: {args.MessageId}, Value: {args.Data.Value}");
    }
}

CodeNet.Redis

CodeNet.Redis is a .Net library.

Installation

Use the package manager npm to install CodeNet.Redis

dotnet add package CodeNet.Redis

Usage

appSettings.json

{
  "Redis": {
    "Hostname": "localhost",
    "Port": 6379
  }
}

program.cs

using CodeNet.Container.Extensions;
using CodeNet.Core.Extensions;
using CodeNet.Redis.Extensions;
using CodeNet.Redis.Module;

var builder = WebApplication.CreateBuilder(args);
builder
    .AddRedisDistributedCache("Redis")
    .AddRedisDistributedLock("Redis");
//...

var app = builder.Build();
app.UseDistributedCache()
    .UseDistributedLock();
//...
app.Run();

Usage Lock

using CodeNet.Core.Models;
using CodeNet.Redis.Attributes;
using Microsoft.AspNetCore.Mvc;
using StokTakip.Customer.Abstraction.Service;
using StokTakip.Customer.Contract.Request;
using StokTakip.Customer.Contract.Response;

namespace StokTakip.Customer.Api.Controllers;

[ApiController]
[Route("[controller]")]
public class CustomersController(ICustomerService customerService) : ControllerBase
{
    [HttpGet("{customerId}")]
    [Lock]
    [ProducesResponseType(200, Type = typeof(CustomerResponse))]
    [ProducesDefaultResponseType(typeof(ResponseMessage))]
    public async Task<IActionResult> GetPersonel(int customerId, CancellationToken cancellationToken)
    {
        return Ok(await customerService.GetCustomer(customerId, cancellationToken));
    }

    //...
}

Usage Cache

using CodeNet.Core.Models;
using CodeNet.Redis.Attributes;
using Microsoft.AspNetCore.Mvc;
using StokTakip.Customer.Abstraction.Service;
using StokTakip.Customer.Contract.Request;
using StokTakip.Customer.Contract.Response;

namespace StokTakip.Customer.Api.Controllers;

[ApiController]
[Route("[controller]")]
public class CustomersController(ICustomerService customerService) : ControllerBase
{
    [HttpGet("{customerId}")]
    [Cache(10)]
    [ProducesResponseType(200, Type = typeof(CustomerResponse))]
    [ProducesDefaultResponseType(typeof(ResponseMessage))]
    public async Task<IActionResult> GetPersonel(int customerId, CancellationToken cancellationToken)
    {
        return Ok(await customerService.GetCustomer(customerId, cancellationToken));
    }

    //...
}

Skils

dot-net logo csharp logo microsoftsqlserver logo oracle logo mysql logo postgresql logo sqlite logo mongodb logo redis logo rabbitmq logo

Contact


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published