Я хочу создать базовый класс репозитория, каждая служба должна наследовать от этой основы.public class BaseService : IBaseService
where TEntity : Entity
where TAddDto : class
where TUpdateDto : class
where TEntityDto : class
{
private readonly IFreeSql _fsql;
public BaseService(IFreeSql fsql)
{
_fsql = fsql;
}
public virtual async Task AddAsync(TAddDto input)
{
var addEntity = input.Adapt();
await _fsql.Insert(addEntity).ExecuteAffrowsAsync();
return ResultOutput.Ok();
}
public virtual async Task UpdateAsync(TUpdateDto input)
{
var property = typeof(TUpdateDto).GetProperty("Id");
if (property == null)
{
throw new ArgumentException("Id is not exist");
}
var obj = property.GetValue(input);
await _fsql.Update(input)
.Where(LambdaHelper.GetPropertyNameExpression("Id", obj))
.ExecuteAffrowsAsync();
return ResultOutput.Ok();
}
public virtual async Task GetAsync(long id)
{
var dto = await _fsql.Select()
.Where(a => a.Id == id)
.FirstAsync();
return ResultOutput.Ok();
}
public virtual async Task FakeDeleteAsync(long id)
{
int rows = await _fsql.Update()
.Set(LambdaHelper.GetPropertyNameMemberExpression("IsDeleted", true), true)
.Where(a => a.Id == id)
.ExecuteAffrowsAsync();
return rows > 0 ? ResultOutput.Ok() : ResultOutput.NotOk();
}
public virtual async Task DeleteAsync(long id)
{
int rows = await _fsql.Delete()
.Where(a => a.Id == id)
.ExecuteAffrowsAsync();
return rows > 0 ? ResultOutput.Ok() : ResultOutput.NotOk();
}
}
}
Это интерфейс ibaseservice :
public interface IBaseService
where TEntity : Entity
where TAddDto : class
where TUpdateDto : class
where TEntityDto : class
{
Task AddAsync(TAddDto input);
Task UpdateAsync(TUpdateDto input);
Task GetAsync(long id);
Task FakeDeleteAsync(long id);
Task DeleteAsync(long id);
}
Я создаю InternationService , который наследует от BaseService public class InternationalService: BaseService, IInternationalService
{
public InternationalService(IFreeSql fsql):base(fsql)
{
}
}
Интерфейс iinternationalservice выглядит следующим образом:
public interface IInternationalService: IBaseService
{
}
I Inject Service в программу.// Inject Generic Service
builder.Services.AddScoped(typeof(IBaseService), typeof(BaseService));
// Inject the end name Service
var types = Assembly.Load("Xin.Service")
.GetExportedTypes()
.Where(a => a.Name.EndsWith("Service"));
foreach (var type in types)
{
var interfaces = type.GetInterfaces();
foreach (var baseType in interfaces)
{
builder.Services.AddScoped(baseType, type);
}
}
Я хочу, чтобы каждая служба унаследовала от BaseService , поэтому я могу игнорировать базовое добавление обновления Delete ... код, но я не могу запустить код.
Можете ли вы мне помочь? Спасибо
Когда я запускаю приложение, приложение не работает, ошибка была < /p>
Application terminated unexpectedly
System.ArgumentException: Cannot instantiate implementation type 'Xin.Service.International.IInternationalService' for service type 'Xin.Service.Base.IBaseService`4[Xin.Model.System.InternationalEntity,Xin.Service.International.Dto.InternationalAddInput,Xin.Service.International.Dto.InternationalUpdateInput,Xin.Service.International.Dto.InternationalDto]'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.Populate()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory..ctor(ICollection`1 descriptors)
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
at Xin.Admin.WebApi.Program.Main(String[] args) in E:\home\NetCore\code\Xin.Admin\Xin.Admin.WebApi\Program.cs:line 22
Подробнее здесь: https://stackoverflow.com/questions/797 ... eric-error