rcnespoli
rcnespoli
CC#
Created by rcnespoli on 4/26/2024 in #help
JSON deserialize
I'm receiving from API a "json" string that have double quotes in value, something like this
var response = "{"details": "This is the "result" of the details"}"
var response = "{"details": "This is the "result" of the details"}"
Is there a way that can I deserialize to an object?
12 replies
CC#
Created by rcnespoli on 3/28/2024 in #help
List extension
I created this extesion
public static class ListUtilExtension
{
public static void AddOptional<T>(this List<T> list, T? item)
{
if (item is null)
{
return;
}
list.Add(item);
}
}
public static class ListUtilExtension
{
public static void AddOptional<T>(this List<T> list, T? item)
{
if (item is null)
{
return;
}
list.Add(item);
}
}
In my test, to string worked
[Fact]
[Description("Should not add null value in list")]
public void ShouldNotAddNullValue()
{
// Setup
var list = new List<string>();

// Exercise
list.AddOptional(null);

// Verify
Assert.Empty(list);
}
[Fact]
[Description("Should not add null value in list")]
public void ShouldNotAddNullValue()
{
// Setup
var list = new List<string>();

// Exercise
list.AddOptional(null);

// Verify
Assert.Empty(list);
}
But when I try with int, not worked
[Fact]
[Description("Should add value after tried to add null value in list")]
public void ShouldAddValueAfterTryAddNullValue()
{
// Setup
const int expectedCountInList = 2;
var list = new List<int>();
var firstIntToAdd = Random.Shared.Next(1, int.MaxValue);
var secondIntToAdd = Random.Shared.Next(1, int.MaxValue);
list.AddOptional(firstIntToAdd);
list.AddOptional(null);

// Exercise
list.AddOptional(secondIntToAdd);

// Verify
Assert.Equal(expectedCountInList, list.Count);
}
[Fact]
[Description("Should add value after tried to add null value in list")]
public void ShouldAddValueAfterTryAddNullValue()
{
// Setup
const int expectedCountInList = 2;
var list = new List<int>();
var firstIntToAdd = Random.Shared.Next(1, int.MaxValue);
var secondIntToAdd = Random.Shared.Next(1, int.MaxValue);
list.AddOptional(firstIntToAdd);
list.AddOptional(null);

// Exercise
list.AddOptional(secondIntToAdd);

// Verify
Assert.Equal(expectedCountInList, list.Count);
}
Show to me: Argument type 'null' is not assignable to parameter type 'int' What I did wrong?
7 replies
CC#
Created by rcnespoli on 2/19/2024 in #help
How can I decode Avro Message that have an embedded schema and after the message content?
How can I decode Avro Message that have an embedded schema and after the message content. I receive from kafka as byte[], and if if transform to string it looks like
Objavro.schema�~{WHOLE SCHEMA}l�G����ߐ��N ��K�
���INSERTaddress_mmastermasters"NmeStreamIdentity&2024-02-19 09:36:41&2024-02-19 09:36:41&2024-02-19 09:36:41���������COUNTRYMYCITY123MYCITYMYADDRESS,TMY ADDRESSAAA000AA������Tc2c80c7d-b46f-4eb6-b001-2ec4bddd8c95_PAD01Hc2c80c7d-b46f-4eb6-b001-2ec4bddd8c95��INSERTparty_add_mmastermasters"NmeStreamIdentity&2024-02-19 09:36:41&2024-02-19 09:36:41&2024-02-19 09:36:41������������Primaryޗ�&2022-08-17 10:09:38ActiveH1a66d9db-fd57-4342-a445-4796b4c2bffdl�G����ߐ��N ��K
Objavro.schema�~{WHOLE SCHEMA}l�G����ߐ��N ��K�
���INSERTaddress_mmastermasters"NmeStreamIdentity&2024-02-19 09:36:41&2024-02-19 09:36:41&2024-02-19 09:36:41���������COUNTRYMYCITY123MYCITYMYADDRESS,TMY ADDRESSAAA000AA������Tc2c80c7d-b46f-4eb6-b001-2ec4bddd8c95_PAD01Hc2c80c7d-b46f-4eb6-b001-2ec4bddd8c95��INSERTparty_add_mmastermasters"NmeStreamIdentity&2024-02-19 09:36:41&2024-02-19 09:36:41&2024-02-19 09:36:41������������Primaryޗ�&2022-08-17 10:09:38ActiveH1a66d9db-fd57-4342-a445-4796b4c2bffdl�G����ߐ��N ��K
26 replies
CC#
Created by rcnespoli on 10/15/2023 in #help
✅ Dapper relation tables
In my domain I have this class
public class UserModel : BaseModel
{
public required string Name { get; set; }
public required string Password { get; set; }
public required string Email { get; set; }
public required string TaxId { get; set; }
public bool IsActive { get; set; } = false;
public required List<int> RelatedCompanies { get; set; }
}
public class UserModel : BaseModel
{
public required string Name { get; set; }
public required string Password { get; set; }
public required string Email { get; set; }
public required string TaxId { get; set; }
public bool IsActive { get; set; } = false;
public required List<int> RelatedCompanies { get; set; }
}
Each user can have multiplies companies, so I have this SQL statement
public class UserTableQueries
{
private const string UserTableName = "tiss.user";
private const string UserCompanyTableName = "tiss.user_company";

public string GetUserByTaxId() =>
$@"
SELECT
{UserTableName}.id as Id,
{UserTableName}.name as Name,
{UserTableName}.tax_id as TaxId,
{UserTableName}.password as Password,
{UserTableName}.email as Email,
{UserTableName}.active as IsActive,
{UserTableName}.created_at as CreatedAt,
{UserTableName}.updated_at as UpdatedAt,
{UserTableName}.updated_at as UpdatedAt,
{UserCompanyTableName}.company_id as CompanyId
FROM {UserTableName}
LEFT JOIN {UserTableName} ON {UserTableName}.id = {UserCompanyTableName}.user_id
WHERE
{UserTableName}.tax_id = (@TaxId);
";
}
public class UserTableQueries
{
private const string UserTableName = "tiss.user";
private const string UserCompanyTableName = "tiss.user_company";

public string GetUserByTaxId() =>
$@"
SELECT
{UserTableName}.id as Id,
{UserTableName}.name as Name,
{UserTableName}.tax_id as TaxId,
{UserTableName}.password as Password,
{UserTableName}.email as Email,
{UserTableName}.active as IsActive,
{UserTableName}.created_at as CreatedAt,
{UserTableName}.updated_at as UpdatedAt,
{UserTableName}.updated_at as UpdatedAt,
{UserCompanyTableName}.company_id as CompanyId
FROM {UserTableName}
LEFT JOIN {UserTableName} ON {UserTableName}.id = {UserCompanyTableName}.user_id
WHERE
{UserTableName}.tax_id = (@TaxId);
";
}
The query result could return multiply rows due UserCompanyTable, that have relation between user and companies On repository I'm using this way to get user with relation companies
4 replies
CC#
Created by rcnespoli on 7/19/2023 in #help
❔ Builder with generics
Is possible create a builder that have classes with different parameters? An example
public class CommandHandlerFactory
{
private readonly ICommandHandler<CreateGoogleAccountCommand> _googleAccountCommandHandler;
private readonly ICommandHandler<CreateYahooAccountCommand> _yahooAccountCommandHandler;

public CommandHandlerFactory(ICommandHandler<CreateGoogleAccountCommand> googleAccountCommandHandler, ICommandHandler<CreateYahooAccountCommand> yahooAccountCommandHandler)
{
_googleAccountCommandHandler = googleAccountCommandHandler;
_yahooAccountCommandHandler = yahooAccountCommandHandler;
}

public ICommandHandler<T> Build<T>(ProviderAvaiablesEnum providers) where T : ICommand
{

var builder = new Dictionary<ProviderAvaiablesEnum, ICommandHandler<T>>
{
{ ProviderAvaiablesEnum.Google, _googleAccountCommandHandler },
{ ProviderAvaiablesEnum.Yahoo _yahooAccountCommandHandler },
};
return builder[providers];
}
}

// Command Handler interface
public interface ICommandHandler<in T> where T : ICommand
{
Task<CommandResult> Execute(T command, CancellationToken cancellationToken = default);
}

// Command interface
public interface ICommand
{
public ProviderAvaiablesEnum ProviderAvaiables { get; set; }
}

// Yahoo interface
public class CreateYahooAccountCommand : ICommand
{
public ProviderAvaiablesEnum ProviderAvaiables { get; set; } = ProviderAvaiablesEnum.Yahoo
....
}
public class CommandHandlerFactory
{
private readonly ICommandHandler<CreateGoogleAccountCommand> _googleAccountCommandHandler;
private readonly ICommandHandler<CreateYahooAccountCommand> _yahooAccountCommandHandler;

public CommandHandlerFactory(ICommandHandler<CreateGoogleAccountCommand> googleAccountCommandHandler, ICommandHandler<CreateYahooAccountCommand> yahooAccountCommandHandler)
{
_googleAccountCommandHandler = googleAccountCommandHandler;
_yahooAccountCommandHandler = yahooAccountCommandHandler;
}

public ICommandHandler<T> Build<T>(ProviderAvaiablesEnum providers) where T : ICommand
{

var builder = new Dictionary<ProviderAvaiablesEnum, ICommandHandler<T>>
{
{ ProviderAvaiablesEnum.Google, _googleAccountCommandHandler },
{ ProviderAvaiablesEnum.Yahoo _yahooAccountCommandHandler },
};
return builder[providers];
}
}

// Command Handler interface
public interface ICommandHandler<in T> where T : ICommand
{
Task<CommandResult> Execute(T command, CancellationToken cancellationToken = default);
}

// Command interface
public interface ICommand
{
public ProviderAvaiablesEnum ProviderAvaiables { get; set; }
}

// Yahoo interface
public class CreateYahooAccountCommand : ICommand
{
public ProviderAvaiablesEnum ProviderAvaiables { get; set; } = ProviderAvaiablesEnum.Yahoo
....
}
Compiler error, Argument type 'Account.Importer.Domain.Commands.Contracts.ICommandHandler<Account.Importer.Domain.Commands.Contracts.CreateGoogleAccountCommand>' is not assignable to parameter type 'Account.Importer.Domain.Commands.Contracts.ICommandHandler<T>'
9 replies
CC#
Created by rcnespoli on 7/19/2023 in #help
❔ How can I create a command handler factory, with this structure that I have
// Comand Handler interface
public interface ICommandHandler<in T> where T : ICommand
{
Task<CommandResult> Execute(T command);
}

// Command Interface
public interface ICommand
{
public string MyProperty { get; set; }

}

// MyCommand class
public class MyCommand : ICommand
{
public public string MyProperty { get; set; }
}

// MyCommand handle
public class MyCommandHandler : ICommandHandler<MyCommand>
{
public async Task<CommandResult> Execute(MyCommand command)
{

}
}

// Command Factory
public class CommandHandlerFactory
{
private readonly ICommandHandler<MyCommand> _myCommand;

public CommandHandlerFactory(ICommandHandler<MyCommand> myCommand)
{
_myCommand = myCommand;
}

public ICommandHandler<?> Build(MyEnum myEnum)
{
var builder = new Dictionary<MyEnum, ICommandHandler<?>>
{
{ LineOfBusiMyEnumessEnum.MyEnumValue, _myCommand }
};
return builder[myEnum];
}
}
// Comand Handler interface
public interface ICommandHandler<in T> where T : ICommand
{
Task<CommandResult> Execute(T command);
}

// Command Interface
public interface ICommand
{
public string MyProperty { get; set; }

}

// MyCommand class
public class MyCommand : ICommand
{
public public string MyProperty { get; set; }
}

// MyCommand handle
public class MyCommandHandler : ICommandHandler<MyCommand>
{
public async Task<CommandResult> Execute(MyCommand command)
{

}
}

// Command Factory
public class CommandHandlerFactory
{
private readonly ICommandHandler<MyCommand> _myCommand;

public CommandHandlerFactory(ICommandHandler<MyCommand> myCommand)
{
_myCommand = myCommand;
}

public ICommandHandler<?> Build(MyEnum myEnum)
{
var builder = new Dictionary<MyEnum, ICommandHandler<?>>
{
{ LineOfBusiMyEnumessEnum.MyEnumValue, _myCommand }
};
return builder[myEnum];
}
}
I'm stuck with the build's return method on CommandHandlerFactory
4 replies
CC#
Created by rcnespoli on 3/26/2023 in #help
❔ Could I create two folder using dotnet new webapi?
if I have this scenario, For example: src/Club.Api, src/Club.Infra, src/Club.Test and I want create src/Player.Api, src/Player.Infra, src/Player.Test, could I create the src/Player.Api and src/Club using command: dotnet new webapi on both folders? Those will "linked" to same solution file
6 replies
CC#
Created by rcnespoli on 2/15/2023 in #help
❔ Debugging
5 replies