Hi I have a doubt about how implement correctly the followin code:
My Controller:
[Route("api/[controller]")]
[ApiController]
public class TerminalsController : ControllerBase
{
private readonly IServiceWrapper _service;
public TerminalsController(IServiceWrapper service)
{
_service = service;
}
[HttpPost]
public async Task<ActionResult> Post([FromBody] Message object)
{
try
{
Result result = await _service.Terminal.UpsertInfo(ternminalObject);
if (result.shopId != -1 || result.deviceId != -1 || result.companyId != -1)
{
return Ok(result);
}
else
{
return BadRequest("Can not save info from session on database");
}
}
catch (Exception ex)
{
return StatusCode(500, "Internal server error");
}
}
}
And the code of my serivice:
public class TerminalService : ITerminalService
{
private readonly IRepositoryWrapper _repository;
public TerminalService(IRepositoryWrapper repository)
{
_repository = repository;
}
public async Task<Result> UpsertInfo(SessionMessage ternminalObject)
{
try
{
// Do dark things....
return result;
}
catch (Exception ex)
{
throw ex;
}
}
}
The service use several things for insert or update in several db tables.
and, finally, my factory:
public class DbClientFactory<T>
{
private static Lazy<T> _factoryLazy = new Lazy<T>(
() => (T)Activator.CreateInstance(typeof(T)),
LazyThreadSafetyMode.ExecutionAndPublication);
public static T Instance
{
get
{
return _factoryLazy.Value;
}
}
}
The factory instace the service and the repositories.
Well, this code, fail and throw the error:
System.MissingMethodException: 'No parameterless constructor defined for type TerminalsController'
Ok, the only way that I Found was this: No parameterless constructor defined when accessing web service
I mean, put public TerminalService() : this(new RepositoryWrapper()) { }
in TermialService.
My doubt is, ?it this correct? I mean... I want that all go trow Interfaces, and I'm not sure if this "new RepositoryWrapper()" break this tarject
Im not sure if I make the question in the correct way... anyway, if you need more code, I will write here, I have written only what I considered necessary.
EDIT
This is my StartUp.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
SqlHelper.connectionString = Environment.GetEnvironmentVariable("CONNECTION_STRING");
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureCors();
services.AddMvc();
services.ConfigureServiceWrapper();
services.ConfigureRepositoryWrapper();
services.AddControllers().AddNewtonsoftJson();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCors("CorsPolicy");
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.All
});
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
The ConfigureRepositoryWrapper and the ConfigureServiceWrapper are in the ServiceExtensions.cs:
public static class ServiceExtensions
{
public static void ConfigureCors(this IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod());
});
}
public static void ConfigureRepositoryWrapper(this IServiceCollection services)
{
services.AddScoped<IRepositoryWrapper, RepositoryWrapper>();
}
public static void ConfigureServiceWrapper(this IServiceCollection services)
{
services.AddScoped<IServiceWrapper, ServiceWrapper>();
}
}
The implement of ServiceWrapper is:
public class ServiceWrapper : IServiceWrapper
{
private ITerminalService _terminal;
public ITerminalService Terminal {
get
{
if (_terminal == null)
{
_terminal = DbClientFactory<TerminalService>.Instance;
}
return _terminal;
}
}
}
Thanks!
See Question&Answers more detail:
os