我有一个项目,我在其中按照 DDD 方法使用 Asp.Net Core 2.0。因此,我拥有拥有实体和值对象的聚合根,这些实体和值对象配置为拥有的类型,这是 EF Core 2.0 中的一项新功能。我添加了一个此类配置的示例以更好地阐明。
领域:
public class Address : ValueObject
{
public Address(string street, int number){
Street = street;
Number = number;
}
public string Street {get; private set;}
public int Number {get; private set;}
}
public class Company : AggregateRoot
{
public Address Address {get; private set;}
// Other properties...
// Value objects are immutables so I'm only able to replace it with a new object.
public void UpdateAddress(string street, int number){
Address …Run Code Online (Sandbox Code Playgroud) 我正在使用Autofac作为默认DI 在Asp.net Core中开发一个应用程序,并且在集成测试中,我需要一些服务,这些服务在安装要注入的Autofac之前通过创建a 然后调用该方法以能够获得服务需要,就像下面的代码:IServiceScopeFactoryCreateScope()
public void ExampleOfAspNetServiceScope(){
var scopeFactory = _testServer.Host.Services.GetRequiredService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope()){
var service = scope.ServiceProvider.GetService<ISomeService>();
// Some stuff...
}
}
Run Code Online (Sandbox Code Playgroud)
在将Autofac安装到我的应用程序中之后,我在文档中找到了接口ILifeTimeScope,现在可以如下重写我的代码:
public void ExampleOfAutofacScope(){
var lifetimeScope = _testServer.Host.Services.GetRequiredService<ILifetimeScope>();
using (var scope = lifetimeScope.BeginLifetimeScope()){
var service = scope.Resolve<ISomeService>();
// Some stuff...
}
}
Run Code Online (Sandbox Code Playgroud)
我对两者都进行了集成测试,看来它们的表现相同。因此,我想知道两种方法之间的区别是什么。
提前致谢。