Kuno
Kuno
CC#
Created by Kuno on 6/20/2024 in #help
Converting from DTO to DomainModel
Thanks for explaining and reference, I will go deep into it
18 replies
CC#
Created by Kuno on 6/20/2024 in #help
Converting from DTO to DomainModel
I've never used projections, just don't know what is that. I have one more little question: "When query executed and data recieved, should I map it to TicketDomain and then TickeDomain to *DTO" ?
18 replies
CC#
Created by Kuno on 6/20/2024 in #help
Converting from DTO to DomainModel
I have a separate TicketDAO class, that uses library for working with MySQL, and I wrote something like this (but all of that in php)
class TicketDAO
{
private $database;
public function __construct(DataBase $database)
{
$this->database = $database;
}

public function getById($id)
{
$query = "SELECT * FROM tickets WHERE id = " . $id;
return $this->database->selectRow($query);
}

public function getAll(): array
{
$query = "SELECT * FROM tickets";
$result = $this->database->select($query);
return $result;
}

public function createTicket(createTicketDTO $dto): int | bool
{
$query = "INSERT INTO tickets (serial_number, ticket_descr, status, isPacked, device_id, user_id, mileage)
VALUES ({?},{?},{?},{?},{?},{?},{?})";
$result = $this->database->query($query, array(
$dto->getSerialNumber(),
$dto->getProblemDescription(),
$dto->getStatus()->toString(),
$dto->getCompleteness(),
$dto->getDeviceId(),
$dto->getUserId(),
$dto->getMileage()
));
return $result;
}
class TicketDAO
{
private $database;
public function __construct(DataBase $database)
{
$this->database = $database;
}

public function getById($id)
{
$query = "SELECT * FROM tickets WHERE id = " . $id;
return $this->database->selectRow($query);
}

public function getAll(): array
{
$query = "SELECT * FROM tickets";
$result = $this->database->select($query);
return $result;
}

public function createTicket(createTicketDTO $dto): int | bool
{
$query = "INSERT INTO tickets (serial_number, ticket_descr, status, isPacked, device_id, user_id, mileage)
VALUES ({?},{?},{?},{?},{?},{?},{?})";
$result = $this->database->query($query, array(
$dto->getSerialNumber(),
$dto->getProblemDescription(),
$dto->getStatus()->toString(),
$dto->getCompleteness(),
$dto->getDeviceId(),
$dto->getUserId(),
$dto->getMileage()
));
return $result;
}
18 replies
CC#
Created by Kuno on 6/20/2024 in #help
Converting from DTO to DomainModel
Okay, i got it, but there is another problem: "This DomainModel is truly heavy. For example, if I need to getTicketById(), and for mapping to any DTO I have to get entire DomainModel. However, I have several DTOs, that uses literally 3-4 fields from DomainModel"
18 replies
CC#
Created by Kuno on 3/17/2024 in #help
Logging in ASP.NET
Ok, thanks
53 replies
CC#
Created by Kuno on 3/17/2024 in #help
Logging in ASP.NET
Where it has to be then ?
53 replies
CC#
Created by Kuno on 3/17/2024 in #help
Logging in ASP.NET
i should get rid of it
53 replies
CC#
Created by Kuno on 3/17/2024 in #help
Logging in ASP.NET
tbh, I don't use this class at all 🙂
53 replies
CC#
Created by Kuno on 3/17/2024 in #help
Logging in ASP.NET
I got it
53 replies
CC#
Created by Kuno on 3/17/2024 in #help
Logging in ASP.NET
did you mean like:
c#
public class PostgreDB
{
public NpgsqlConnection Connection { get; set; }
private readonly IConfiguration _configuration;
private readonly ILogger<PostgreDB> _logger;
public PostgreDB(IConfiguration configuration, ILogger<PostgreDB> logger)
{
try
{
Connection = new NpgsqlConnection(_configuration.GetConnectionString("PostgreSQL"));
_logger = logger;
Connection.Open();
_logger.LogInformation($"[{DateTime.Now.TimeOfDay}] Connected to database");
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
}
}

public class RefresherService : BackgroundService, IRefresherService
{
private readonly ILogger<RefresherService> _logger;
private readonly PostgreDB _dbContext;
private readonly Aerodrome _aerodrome;
private readonly FlightRepository _flightRepository;
private readonly string _connectionString;

public RefresherService(ILogger<RefresherService> logger, PostgreDB dbContext)
{
_logger = logger;
_dbContext = dbContext;
_aerodrome = new Aerodrome();
_flightRepository = new(_dbContext.Connection, _aerodrome);
}
}
c#
public class PostgreDB
{
public NpgsqlConnection Connection { get; set; }
private readonly IConfiguration _configuration;
private readonly ILogger<PostgreDB> _logger;
public PostgreDB(IConfiguration configuration, ILogger<PostgreDB> logger)
{
try
{
Connection = new NpgsqlConnection(_configuration.GetConnectionString("PostgreSQL"));
_logger = logger;
Connection.Open();
_logger.LogInformation($"[{DateTime.Now.TimeOfDay}] Connected to database");
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
}
}

public class RefresherService : BackgroundService, IRefresherService
{
private readonly ILogger<RefresherService> _logger;
private readonly PostgreDB _dbContext;
private readonly Aerodrome _aerodrome;
private readonly FlightRepository _flightRepository;
private readonly string _connectionString;

public RefresherService(ILogger<RefresherService> logger, PostgreDB dbContext)
{
_logger = logger;
_dbContext = dbContext;
_aerodrome = new Aerodrome();
_flightRepository = new(_dbContext.Connection, _aerodrome);
}
}
53 replies
CC#
Created by Kuno on 3/13/2024 in #help
How to bind REST API and business logic ?
I already did that, thank you guys, problem is solved
6 replies
CC#
Created by Kuno on 3/17/2024 in #help
Logging in ASP.NET
I guess it's better to add IConfiguration in DbContext and let DI system to handle that
53 replies
CC#
Created by Kuno on 3/17/2024 in #help
Logging in ASP.NET
I don't use EFCore, my db_context is a regular connection to the postgre db and it's used for each repository
c#
public class DbContext
{
public NpgsqlConnection Connection { get; set; }
public DbContext(string _connectionString)
{
try
{
Connection = new NpgsqlConnection(_connectionString);
Connection.Open();
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
}
c#
public class DbContext
{
public NpgsqlConnection Connection { get; set; }
public DbContext(string _connectionString)
{
try
{
Connection = new NpgsqlConnection(_connectionString);
Connection.Open();
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
}
I injected IConfiguration inside my service controller to pull connection string out from there
c#
public class RefresherService : BackgroundService, IRefresherService
{
private readonly ILogger<RefresherService> _logger;
private readonly DbContext _dbContext;
private readonly Aerodrome _aerodrome;
private readonly FlightRepository _flightRepository;
private readonly string _connectionString;
private readonly IConfiguration _configuration;

public RefresherService(ILogger<RefresherService> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;

_connectionString = _configuration.GetConnectionString("PostgreSQL");
_dbContext = new(_connectionString);
_aerodrome = new Aerodrome();
_flightRepository = new(_dbContext.Connection, _aerodrome);
}
c#
public class RefresherService : BackgroundService, IRefresherService
{
private readonly ILogger<RefresherService> _logger;
private readonly DbContext _dbContext;
private readonly Aerodrome _aerodrome;
private readonly FlightRepository _flightRepository;
private readonly string _connectionString;
private readonly IConfiguration _configuration;

public RefresherService(ILogger<RefresherService> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;

_connectionString = _configuration.GetConnectionString("PostgreSQL");
_dbContext = new(_connectionString);
_aerodrome = new Aerodrome();
_flightRepository = new(_dbContext.Connection, _aerodrome);
}
53 replies
CC#
Created by Kuno on 3/17/2024 in #help
Logging in ASP.NET
for FlightRepository in this case
53 replies
CC#
Created by Kuno on 3/14/2024 in #help
Little Question about DB (postgres)
But why the 'status' field is bad to have ?
30 replies
CC#
Created by Kuno on 3/14/2024 in #help
Little Question about DB (postgres)
Did you mean, that query like SELECT * FROM [TABLENAME] WHERE status='arrived', may work incorrectly, cause it was executed between my updating intervals ?
30 replies
CC#
Created by Kuno on 3/14/2024 in #help
Little Question about DB (postgres)
for my task UPDATE [tablename] SET status = 'your value' WHERE status='arrived' AND scheduled_departure < now() is enough, at least so far
30 replies
CC#
Created by Kuno on 3/14/2024 in #help
Little Question about DB (postgres)
ok, i got it, thank you!
30 replies
CC#
Created by Kuno on 3/14/2024 in #help
Little Question about DB (postgres)
like auto-updating table
30 replies
CC#
Created by Kuno on 3/14/2024 in #help
Little Question about DB (postgres)
but can I apply "my conditions" to the whole table directly in database ?
30 replies