鉴于以下课程,您将如何为其编写单元测试?我已经读过,任何执行文件IO的测试都不是单元测试,所以这是一个需要编写的集成测试吗?我正在使用xUnit和MOQ进行测试,我对它很新,所以也许我可以MOQ文件?不确定.
public class Serializer
{
public static T LoadFromXmlFile<T>(string path)
where T : class
{
var serializer = new XmlSerializer(typeof(T));
using (var reader = new StreamReader(path))
{
return serializer.Deserialize(reader) as T;
}
}
public static void SaveToXmlFile<T>(T instance, string path)
{
var serializer = new XmlSerializer(typeof(T));
using (var writer = new StreamWriter(path))
{
serializer.Serialize(writer, instance);
writer.Flush();
}
}
}
Run Code Online (Sandbox Code Playgroud) 鉴于以下课程:
public class Transaction
{
public string Category { get; set; }
public string Form { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如何获得按Category和Form?分组的事务分组?
基本上我希望它输出如下:
Category 1
Form 1
Transaction1
Transaction2
Transaction3
...
Form 2
Transaction1
Transaction2
Transaction3
...
Category 2
Form 1
Transaction1
Transaction2
Transaction3
...
Form 2
Transaction1
Transaction2
Transaction3
...
Run Code Online (Sandbox Code Playgroud) 好吧,所以我正在玩Ninject,一个"服务层"和一个"存储库层".
我构建了一个简单的控制台应用程序来玩,这就是我想出的:
Imports Ninject
Module Module1
Sub Main()
Dim Kernel As IKernel = New StandardKernel(New CustomerModule)
Dim Service = Kernel.Get(Of CustomerService)()
Console.WriteLine(Service.GetCustomerByID(1).Name)
Console.Read()
End Sub
End Module
#Region "Services"
Public Class CustomerService
Private _Repository As ICustomerRepository
<Inject()> _
Public Sub New(ByVal Repository As ICustomerRepository)
_Repository = Repository
End Sub
Public Function GetCustomerByID(ByVal ID As Integer) As Customer
Return _Repository.GetByID(ID)
End Function
End Class
#End Region
#Region "Repositories"
Public Interface IRepository(Of T)
Function Query(ByVal Predicate As Expressions.Expression(Of Func(Of T, Boolean))) As …Run Code Online (Sandbox Code Playgroud) 我对此很新,所以请耐心等待.
我有一个使用Service/Repository/EF4模式的MVC应用程序,我正在尝试使用Ninject.我有它在控制器上工作,它们是构造函数注入服务,但服务是构造函数注入存储库,我不知道在哪里处理它.
我试图让它如此每层只知道下面的图层,这是正确的吗?如果是这样,MVC应用程序只知道服务层,服务层只知道存储库层等.所以在我创建绑定的Ninject模块中,我不能说:
Bind(Of IRepository(Of Category)).To(Of EFRepository(Of Category))
Run Code Online (Sandbox Code Playgroud)
我在哪里处理注射?
c# asp.net-mvc dependency-injection ninject repository-pattern
好的,所以我之前就这个问过几个问题,但我真的只是想了解这一点.
我正在使用Service/Repository/EF 4 w/Pocos方法,我有Ninject设置并向控制器注入服务,但我试图找出注入上下文的位置?
我希望能够在控制器上使用多个服务,这些服务又可以使用相同的上下文访问多个存储库,因此所有更改都将立即保留.
我研究了UnitOfWork模式,但我不明白MVC(控制器)如何实现它,因为他们只知道服务层和域实体.
编辑
正如Mohamed在下面建议的那样,将上下文注入存储库,然后使用它的每个请求实例.如何在MVC应用程序中配置绑定?我会假设这样的事情:
Bind(Of IContext).To(MyDataContext)
Run Code Online (Sandbox Code Playgroud)
问题是,MVC应用程序对上下文一无所知,对吧?
编辑2
Public Class ProductController
Private _Service As IProductService
Public Sub New(Service As IProductService)
_Service = Service
End Sub
End Class
Public Class NinjectWebModule
Public Sub New()
Bind(Of IProductService).To(ProductService)
End Sub
End Class
Public Interface IProductService
End Interface
Public Class ProductService
Implements IProductService
Private _Repository As IRepository(Of Product)
Public Sub New(Repository As IRepository(Of Product))
_Repository = Repository
End Sub
End Class
Public Class NinjectServiceModule
Public Sub New()
Bind(Of IRepository(Of Product)).To(EFRepository(Of …Run Code Online (Sandbox Code Playgroud) 我知道这可能是一个愚蠢的问题,但是泛型的命名标准是什么?
Of t 或 Of TEntity 或 Of... 这真的无关紧要吗?
我看到 IQueryable(Of T) 然后是 DBSet(Of TEntity)。
我正在使用EF/Repository/Unit of Work,但我很难理解一些细节.在UnitOfWork内部,我创建了一个新的EF DbContext(EmmaContext),但是在存储库中查看,我将其转换为我知道错误,如何正确获取repo中的上下文?也许我完全走错了路?
这是我的UnitOfWork:
//Interface
public interface IUnitOfWork : IDisposable
{
void Commit();
}
//Implementation
public class UnitOfWork : IUnitOfWork
{
#region Fields/Properties
private bool isDisposed = false;
public EmmaContext Context { get; set; }
#endregion
#region Constructor(s)
public UnitOfWork()
{
this.Context = new EmmaContext();
}
#endregion
#region Methods
public void Commit()
{
this.Context.SaveChanges();
}
public void Dispose()
{
if (!isDisposed)
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
isDisposed = true;
if (disposing)
{
if (this.Context != null) …Run Code Online (Sandbox Code Playgroud) 我编写了一个PowerShell脚本来安装和启动一些服务.它仅在脚本以管理员身份运行时才有效.有没有办法强制脚本拥有这些权限?
所以在普通的老Angular中,你会像这样注入一个工厂:
(function(angular){
"use strict";
var app = angular.module('app', []);
app.factory('MyFactory', ['$http', function($http) {
....
});
}());
Run Code Online (Sandbox Code Playgroud)
现在使用Typescript,我试图使用$inject类似的:
module Services {
export class MyFactory {
static $inject = ['$http'];
constructor(private $http: ng.IHttpService) {
}
}
}
var app = angular.module('app', []);
app.factory('MyFactory', Services.MyFactory) //<-- This does not work, never is instantiated.
app.factory('MyFactory', ['$http', Services.MyFactory]); //<-- No deal, it has constructor dependencies
// Seems you have to do this
app.factory('MyFactory', ['$http', ($http) => new Services.MyFactory($http)]);
Run Code Online (Sandbox Code Playgroud)
使用Typescript在Angular中注入工厂的正确方法是什么?
我有一组来自美国各地的数据,我正在尝试将其转换为每个“主题”的当地时间。我对每个事件都有 UTC 时间戳,并将其转换为 POSIXct 格式,但每次我尝试在任何 POSIXct/POSIXlt 函数(包括和)中包含tz = DS$Factor或 的向量时,都会收到一条错误消息:tz = as.character(DS$Factor)format()strftime()
as.POSIXlt.POSIXct(x, tz = tz) 中的错误:无效的“tz”值
如果我只是输入tz = 'US/Eastern'它就可以正常工作,但当然并非我的所有值都来自该时区。
如何将每个“主题”的时间戳记为当地时间?
有DS$Factor5 个值:US/Arizona US/Central US/Eastern US/Mountain US/Pacific
谢谢,速记
c# ×3
asp.net-mvc ×2
ninject ×2
vb.net ×2
.net ×1
angularjs ×1
format ×1
group-by ×1
linq ×1
moq ×1
posixct ×1
posixlt ×1
powershell ×1
strftime ×1
timezone ×1
typescript ×1
unit-of-work ×1
unit-testing ×1
xunit ×1