对不起,我不知道存储库模式,单元测试和orm工具.
我一直在研究单元测试和存储库模式,得出了一些结论,我想知道我是不对的.
存储库模式有助于在控制器中替换单元测试,例如使用它,对吧?因为创建上下文(在EF中)或会话(在NH中)更难,对吧?存储库本身未经过测试?为什么?
使用EntityFramework或NHibernate与存储库模式,如果我想测试我的存储库,我需要进行集成测试?因为如果我使用我的上下文/会话的假实现,我没有进行真正的测试?因为上下文/会话本身就是存储库(我的意思是它们实现了添加,删除,编辑,GetById,GetAll等的真实逻辑)?
使用EF或NH的存储库模式就像一个包装器?(不仅是一个包装器,我知道这是域的导入概念.)
.net nhibernate unit-testing domain-driven-design entity-framework
这是我的例子:
[TestMethod]
public void NewAction_should_return_IndexAction()
{
NewViewModel viewModel = new NewViewModel()
{
Name = "José Inácio Santos Silva",
Email = "joseinacio@joseinacio.com",
Username = "joseinacio"
};
//IsUserRegistered is used to validate Username, Username is unique.
_mockAuthenticationService.Setup(x => x.IsUserRegistered(viewModel.Username )).Returns(false);
//IsUserRegistered is used to validate Email, Email is unique.
_mockUsuarioRepository.Setup(x => x.GetUserByEmail(viewModel.Email));
_mockDbContext.Setup(x => x.SaveChanges());
_mockUsuarioRepository.Setup(x => x.Add(It.IsAny<User>()));
_userController = new UserController(_mockUsuarioRepository.Object, _mockDbContext.Object, _mockAuthenticationService.Object);
ActionResult result = _userController.New(viewModel);
result.AssertActionRedirect().ToAction("Index");
_mockAuthenticationService.VerifyAll();
_mockUsuarioRepository.VerifyAll();
_mockDbContext.VerifyAll();
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了一些教程,他们说每次测试我们应该只使用一个模拟.
但看看我的测试,它使用3次嘲讽 …
我正在使用Entity Framework构建存储库基类,其中所有实体存储库都将继承.我想DatabaseContext使用Ninject使用Dependency Injection 注入in基类.我认为构造函数注入是正确的方法,但在派生类I中使用构造函数注入执行此操作必须将参数传递给基类中的构造函数,我不想要它.因此,Setter Injection更合适吗?
这是我的代码:
public abstract class BaseRepository<TEntity> : IDisposable
where TEntity : class
{
private readonly DatabaseContext _databaseContext;
protected BaseRepository(DatabaseContext databaseContext)
{
this._databaseContext = databaseContext;
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中使用构造函数注入,在我的派生类中,我必须将databaseContext对象传递给基类,我不喜欢对我的所有派生类执行此操作:
public class UsuarioRepository : BaseRepository<IUsuario>,
IUsuarioRepository
{
public UsuarioRepository(DatabaseContext databaseContext)
: base(databaseContext)
{
}
}
Run Code Online (Sandbox Code Playgroud)
Setter Injection而不是Constructor Injection是解决这个问题的好方法吗?什么是最好的方法?
更新:
使用Setter Injection我的派生类不会有构造函数:
public class UsuarioRepository : BaseRepository<IUsuario>, IUsuarioRepository
{
}
Run Code Online (Sandbox Code Playgroud)
我的上下文只是所有应用程序中的一个.我不需要派生类来传递上下文对象,但是我想将它注入基类中以便将来使用模拟测试.
我解决了这个问题:
对不起,我对这个问题感到困惑,但是我正在解决建立工厂的问题:
public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity>
where TEntity : class
{
private readonly …Run Code Online (Sandbox Code Playgroud) 与实体框架代码 - 首先我必须公开所有属性来生成数据库,这意味着使用实体框架代码优先方法我被迫有一个贫穷的领域与丰富的模型混合?因为不需要公开的属性,必须是.
例如:
使用丰富的模型,实体框架中的这将不起作用:
public class Car
{
private int _actualPosition;
public void Accelerate()
{
this._actualPosition += 10;
}
}
Run Code Online (Sandbox Code Playgroud)
为了工作,我们需要_actualPosition公开:
public class Car
{
public int ActualPosition { get; set; }
public void Accelerate()
{
this.ActualPosition += 10;
}
}
Run Code Online (Sandbox Code Playgroud)
现在实体IMO是丑陋的,因为我有一个方法在属性中添加+ 10并同时公开它,我不希望该属性是公共的.
另一个例子:
想象一下,我想要一个多对多的关系,但只有一种方式:
public class User
{
public long Id { get; set; }
public string Name { get; set; }
public IList<Role> Roles { get; set; }
}
public class …Run Code Online (Sandbox Code Playgroud)我正在构建一个具有以下架构的应用程序:
UI - 应用程序 - 域 - 基础架构
我有一个需要使用自定义异常的应用层.我保留这些自定义异常的地方?在基础架构层?问题是我的Application Layer没有引用Infrastructure层.
什么是正确的方法?
更新:
这是我在Application Layer中引发异常的代码:
public void InsertNewImage(ImagemDTO imagemDTO)
{
if (isValidContentType(imagemDTO.ImageStreamContentType))
{
string nameOfFile = String.Format("{0}{1}", Guid.NewGuid().ToString(), ContentTypeHelper.GetExtension(imagemDTO.ImageStreamContentType));
string path = String.Format("{0}{1}", ImageSettings.PathToSave, nameOfFile);
_fileService.SaveFile(imagemDTO.ImageStream, path);
Imagem imagem = new Imagem()
{
Titulo = imagemDTO.Titulo,
Descricao = imagemDTO.Descricao,
NomeArquivo = nameOfFile
};
_imagemRepository.Add(imagem);
_dbContext.SaveChanges();
} else
{
throw new WrongFileTypeException(String.Format("{0} is not allowed.", ContentTypeHelper.GetExtension(imagemDTO.ImageStreamContentType)));
}
}
Run Code Online (Sandbox Code Playgroud)
甚至ImageSettings也是我的应用层中的ConfigurationSection,因为它使用它.我没有看到其他方式可以将我的ImageSettings(应该保留在基础设施层)转移到基础架构层,有人可以提供帮助吗?
public class ImageSettings : ConfigurationSection
{
/// <summary>
/// Caminha onde será salvo …Run Code Online (Sandbox Code Playgroud) 我有一个使用Entity Framework 4.1的关系,我的外键是自动生成的,外键的名称有下划线:
public class Setor : Entity
{
public long Id { get; set; }
public string Nome { get; set; }
public virtual Secretaria Secretaria { get; set; }
}
public class Secretaria : Entity
{
public long Id { get; set; }
public string Nome { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这在表Setor中生成了名为:Secretaria_Id的外键
我想删除这个下划线:SecretariaId
有没有办法做到这一点?我更喜欢使用DataAnnotations.
我想了解LINQ做了一些调查此事,发现IEnumerable和IQueryable有LINQ的方法.但是,当我查看这些类型的文档时,我没有看到任何LINQ方法.
LINQ方法是否IEnumerable/IQueryable在运行时插入类型中的扩展方法,如果是这样,为什么?
我有YoutubeVideoService执行 CRUD(创建、读取、更新和删除)操作的类。在我看来,创建、读取、更新和删除是类更改的四个原因。这个类是否违反了单一职责原则?
如果违反,那么,我们应该有四个类,如CreateYoutubeVideoService,ReadYoutubeVideoService,UpdateYoutubeVideoService和DeleteYoutubeVideoService。有很多课程是不是有点矫枉过正?
我在我的项目中使用DDD架构,我需要创建一个类来生成在另一个类中使用的GUID.
生成我的GUID的这个类是基础结构服务还是基础结构助手?
我怎么知道班级是帮助者还是服务员?
我已经阅读了有关Windows工作流基础的内容,人们使用它来建模业务流程,为什么不使用UML?
根据一些答案,工作流程可以是我的域名?
还有哪些其他工具与WF相同?
.net ×8
c# ×3
oop ×2
unit-testing ×2
linq ×1
mocking ×1
nhibernate ×1
single-responsibility-principle ×1
testing ×1