public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; private set; }
// This method gets called by the runtime.
// Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.
.AddDbContext<MyContext>(o => o.UseMySQL(
Configuration.GetConnectionString("MyConnection")));
}
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterGeneric(typeof(Repository<>))
.As(typeof(IRepository<>))
.InstancePerLifetimeScope();
// the below works (before adding the repos)
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.AssignableTo<IService>()
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
}
}
(通用) Repository.cs
public abstract class Repository<T> : IRepository<T>
where T : class
{
private readonly MyContext _context;
protected Repository(MyContext context)
{
_context = context;
}
public virtual string Add(T entity)
{
if (ValidateAdd(ref entity, out var message))
{
_context.Set<T>().Add(entity);
}
return message;
}
public virtual bool ValidateAdd(ref T entity, out string message)
{
message = default(string);
return true;
}
}
存储库的实现:
FooRepository.cs
// public interface IFooRepository: IRepository<Foo>
public class FooRepository: Repository<Foo>, IFooRepository
{
public FooRepository(MyContext context) : base(context)
{
}
public override bool ValidateAdd(ref Foo entity, out string message)
{
// just a hook for pre-insert stuff
message = "All foos shall fail add";
return false;
}
}
接下来是在服务或控制器中的使用方法。
FooService.cs
public class FooService: IFooService
{
private readonly IFooRepository _repository;
public FooService(IFooRepository repository)
{
_repository = repository;
}
public void DoSomethingThenAdd()
{
// some other business logic specific to this service
_repository.Add(new Foo()
{
Id = 1,
LastName = "Bar",
Name = "Foo"
});
}
}
问题:
我该如何进行这一切的连接... 我很难找到关于 MySQL + Ef 的适当文档,而且我凭直觉感觉这部分是“工作”的。但是,正如您在下面的错误日志中看到的那样,我的仓库注册有问题。
错误信息:
在特定注册激活期间发生错误。
有关详细信息,请参见内部异常。
注册: Activator = FooService(ReflectionActivator),Services = [MyApp.Services.Interfaces.IFooService,MyApp.Services.Interfaces.IService],Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,Sharing = Shared, Ownership = OwnedByLifetimeScope
---> 无法使用类型“MyApp.Services.FooService”上的“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到任何构造函数可用于调用可用服务和参数:
无法解析构造函数“Void .ctor(MyApp.Data.Interfaces.IFooRepository repository) ”中的参数“MyApp.Data.Interfaces.IFooRepository repository”。 (有关详细信息,请参见内部异常。)
---> Autofac.Core.DependencyResolutionException: 无法使用类型“MyApp.Services.FooService”上的“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到任何构造函数可用于调用可用服务和参数:
无法解析构造函数“Void .ctor(MyApp.Data.Interfaces.IFooRepository repository)”中的参数“MyApp.Data.Interfaces.IFooRepository repository”。