小编But*_*aur的帖子

Stub一个类的方法,让其他真正的方法使用这个stubbed方法

我有一个TimeMachine课程,它提供了我当前的日期/时间值.这个类看起来像这样:

public class TimeMachine
{
    public virtual DateTime GetCurrentDateTime(){ return DateTime.Now; };
    public virtual DateTime GetCurrentDate(){ return GetCurrentDateTime().Date; };
    public virtual TimeSpan GetCurrentTime(){ return GetCurrentDateTime().TimeOfDay; };
}
Run Code Online (Sandbox Code Playgroud)

我想TimeMachine在我的测试中使用存根,以便我只是存根GetCurrentDateTime方法,让其他两个方法使用存根GetCurrentDateTime方法,这样我就不必存根这三种方法.我试着像这样编写测试:

var time = MockRepository.GenerateStub<TimeMachine>();
time.Stub(x => x.GetCurrentDateTime())
    .Return(new DateTime(2009, 11, 25, 12, 0, 0));
Assert.AreEqual(new DateTime(2009, 11, 25), time.GetCurrentDate());
Run Code Online (Sandbox Code Playgroud)

但测试失败了.GetCurrentDate返回default(DateTime)而不是在GetCurrentDateTime内部使用存根.

有没有什么方法可以用来实现这样的行为,还是只是RhinoMocks的一些基本概念特征,我现在还没有抓住?我知道我可以摆脱这两个GetDate/ Time方法并内联.Date/ .TimeOfDay用法,但我想知道这是否可行.

c# rhino-mocks mocking stub

13
推荐指数
3
解决办法
1万
查看次数

如何在NH Criteria API中使用year()和month()函数?

我需要在Criteria API中使用year()和month()函数来表达业务过滤器约束.表达式如

cri.Add(Expression.Ge("year(Duration.DateFrom)", Year.Value));
cri.Add(Expression.Le("year(Duration.DateTo)", Year.Value));
Run Code Online (Sandbox Code Playgroud)

显然不起作用 - 有没有解决方法如何实现这一目标?

我知道它在HQL中是完全可能的,但我需要使用条件API构建查询,因为有一些额外的进程处理查询添加排序,分页等.


示例HQL解决方案,我想重写为Criteria API:

var ym = year * 100 + month;
var hql = ...(:ym between 100 * year(f.Duration.DateFrom) + month(f.Duration.DateFrom) and 100 * year(f.Duration.DateTo) + month(f.Duration.DateTo)";
Run Code Online (Sandbox Code Playgroud)

nhibernate criteria

8
推荐指数
1
解决办法
7700
查看次数

c# - 将泛型类转换为其基类非泛型类

我有以下课程:

public abstract class CustomerBase
{
    public long CustomerNumber { get; set; }
    public string Name { get; set; }
}

public abstract class CustomerWithChildern<T> : CustomerBase
    where T: CustomerBase
{
    public IList<T> Childern { get; private set; }

    public CustomerWithChildern()
    {
        Childern = new List<T>();
    }
}

public class SalesOffice : CustomerWithChildern<NationalNegotiation>
{
}
Run Code Online (Sandbox Code Playgroud)

SalesOffice只是代表不同级别的客户层次结构的少数类之一.现在我需要从某个角度(CustomerBase)遍历这个层次结构.我不知道如何在不使用反射的情况下实现.我想实现类似的东西:

    public void WalkHierarchy(CustomerBase start)
    {
        Print(start.CustomerNumber);
        if (start is CustomerWithChildern<>)
        {
            foreach(ch in start.Childern)
            {
                WalkHierarchy(ch);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我有没有机会得到这样的工作?


基于我实现的has-childern接口的解决方案:

public interface ICustomerWithChildern …
Run Code Online (Sandbox Code Playgroud)

c# generics

7
推荐指数
2
解决办法
1万
查看次数

asp.net mvc模块化应用程序howto/recommended

有没有人建议以可插拔的模块化方式构建ASP.Net MVC应用程序?我的意思是一些博客文章/教程等.我刚刚阅读了Hammet的MEF和ASP.NET MVC示例博文,但我发现MEF还不够成熟,无法在生产中使用它.我无法在此主题上搜索任何其他相关帖子...

我知道我可以实现模块化实现自定义ControllerProvider和一些插件检查机制,但我想在开始重新实现轮子之前研究一些更多的方法/最佳实践:)


我在本主题中所指的模块性应仅涵盖Web应用程序的观点 - 没有其他类似模块化应用程序模型/服务等.我想找到最佳方法将一个Web应用程序分成多个程序集,其中包含其他控制器,视图,资源,JS + CSS文件,图像等所以应该有一些主要的Web应用程序项目,它将寻找可用的可插入Web应用程序组件,加载它们,发布它们的控制器,使它们的视图和其他资源可用,并可能准备一些菜单每个Web应用程序模块的条目.

我不想在这里讨论任何模型+服务可插拔架构 - 只是ASP.Net M VC部分.

asp.net-mvc

7
推荐指数
0
解决办法
5617
查看次数

c#console应用程序 - 防止默认异常对话框

我有单个AppDomain的简单应用程序,它在服务器上定期启动.有时在应用程序中发生未处理的异常,弹出默认的中止/重试/忽略对话框.我需要以某种方式阻止edialog显示并在StrErr上输出异常并关闭应用程序.所以我用try-catch语句将main方法中的所有代码都包含在内,但它根本没有帮助 - 有时候仍会显示异常对话框.

Main()代码如下所示:

try
{
    RunApplication();
}
catch (Exception exc)
{   
    Console.Error.WriteLine(exc.ToString());
    Console.Error.WriteLine(exc.StackTrace);
    if (exc.InnerException != null)
    {
       Console.Error.WriteLine(exc.InnerException.ToString());
       Console.Error.WriteLine(exc.InnerException.StackTrace);
    }
    Environment.Exit(666);
}
Run Code Online (Sandbox Code Playgroud)

这个try-catch子句可以捕获所有未处理的异常,异常对话框永远不会弹出AFAIK.我错过了什么吗?或者服务器上是否有任何设置(注册表等)控制与异常对话框/应用程序错误代码相关的一些特殊行为?

c# dialog exception-handling console-application

7
推荐指数
1
解决办法
6646
查看次数

WCF - 如何通过HTTP(S)以二进制编码创建编程自定义绑定

我想将我当前的HTTP/HTTPS WCF绑定设置转换为使用二进制消息编码,我需要在代码中进行 - 而不是在XML配置中.AFAIK有必要创建CustomBinding对象并设置正确的BindingElements,但我无法弄清楚我的场景中应该使用哪些元素.

我的WCF配置的要点是:

  • 根据配置使用HTTP或HTTPS传输(在app.config中)
  • 使用用户名消息安全
  • todo:添加二进制编码而不是默认文本

我当前设置绑定的代码(工作,但没有二进制编码):

var isHttps = Settings.Default.wcfServiceBaseAddress.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase);
var binding = new WSHttpBinding(isHttps ? SecurityMode.TransportWithMessageCredential : SecurityMode.Message);
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
Run Code Online (Sandbox Code Playgroud)

我正在尝试此代码,但它不起作用 - 我不知道如何为用户名消息安全设置消息安全元素:

var custBinding = new CustomBinding();
custBinding.Elements.Add(new BinaryMessageEncodingBindingElement());
//Transport Security (Not Required)
if (isHttps)
{
    custBinding.Elements.Add(SecurityBindingElement.CreateUserNameForSslBindingElement());
}
//Transport (Required)
custBinding.Elements.Add(isHttps ?
    new HttpsTransportBindingElement() :
    new HttpTransportBindingElement());
Run Code Online (Sandbox Code Playgroud)

有谁知道如何设置它?我试图搜索类似的问题/解决方案,但没有成功...

security wcf binding

6
推荐指数
1
解决办法
2万
查看次数

Wrapped AggregateException仅报告ToString方法中的第一个异常

当我直接使用ToString()method 时,我得到完整的嵌套异常集AggregateException:

public void GreenTest()
{
    var ex = new AggregateException(new Exception("ex1"), new Exception("ex2"));

    ex.ToString()
        .Should()
        .Contain("ex1")
        .And
        .Contain("ex2");
}
Run Code Online (Sandbox Code Playgroud)

问题是当我被AggregateException另一个异常包装时,我只得到第一个异常:

public void RedTest()
{
    var ex = new Exception("wrapper", new AggregateException(new Exception("ex1"), new Exception("ex2")));

    ex.ToString()
        .Should()
        .Contain("wrapper")
        .And
        .Contain("ex1")
        .And
        .Contain("ex2");
}
Run Code Online (Sandbox Code Playgroud)

ex2在结果字符串中不存在.这是一个错误或类的一些众所周知的功能AggregateException

c# aggregateexception

6
推荐指数
1
解决办法
687
查看次数

ASP.Net MVC - 如何在JSON操作中处理异常(返回JSON错误信息),还发布过滤器的异常?

我正在使用过滤器来记录由以下操作引发的异常:

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Exception != null)
        {
           //logger.Error(xxx);
        }
        base.OnActionExecuted(filterContext);
    }
Run Code Online (Sandbox Code Playgroud)

现在我想处理我的所有JSON操作以返回带有异常信息的JSON结果.这允许Ajax调用确定服务器上是否有任何错误,而不是接收错误页面源,这对Ajax来说是无用的.我在AppControllerBase中为JSON操作实现了这个方法:

    public ActionResult JsonExceptionHandler(Func<object> action)
    {
        try
        {
            var res = action();
            return res == null ? JsonSuccess() : JsonSuccess(res);
        }
        catch(Exception exc)
        {
            return JsonFailure(new {errorMessage = exc.Message});
        }
    }
Run Code Online (Sandbox Code Playgroud)

这很好用,但很明显catch()语句阻止所有过滤器处理异常,因为实际上没有抛出异常.有没有办法如何为过滤器留下可用的异常(filterContext.Exception)?

c# asp.net-mvc json exception-handling filter

5
推荐指数
1
解决办法
5983
查看次数

Oracle ODP.NET托管驱动程序在64位运行速度比在32位运行速度慢50-100%

我刚刚将一些NUnit测试项目从x86切换到AnyCPU,并开始使用nunit-console.exe而不是nunit-console-x86.exe在64位运行时运行它们.这使我的测试持续时间延长了大约50-100%.我可以在任何测试程序集上轻松地重复模拟它,甚至可以对测试运行进行分析,但是我无法弄清楚导致性能损失的原因,因为只有一切看起来在64位中表现得更慢.我还尝试在几台不同的机器上运行测试,结果相同.

测试程序集使用Spring.NET IOC和Oracle ODP托管提供程序(这是将测试从x86切换到AnyCPU的原因)以执行集成测试.测试运行的唯一区别实际上是32对64位环境或nunit可执行文件(nunit-console-x86.exe vs nunit-console.exe).

是否存在如此大的测试持续时间差异的一般原因?或者有没有人知道我应该检查什么来找出问题的原因?


跟进:

问题是由于本月8日发布的Oracle ODP.NET托管驱动程序(版本121010或4.121.1.0)引起的.它显然有很多性能问题,其中一个是64位性能损失.在32位环境下运行以下代码时,持续时间约为0.6秒,在64位运行时运行时间约为1.5秒:

        var sw = Stopwatch.StartNew();
        using (var conn = new OracleConnection(ConnectionString))
        {
            conn.Open();

            for (var i = 0; i < 100; i++)
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select sysdate from dual";
                    var result = Convert.ToDateTime(cmd.ExecuteScalar());
                }
            }
        }

        Console.Out.WriteLine(sw.Elapsed);
Run Code Online (Sandbox Code Playgroud)

现在是否有任何技巧如何从Oracle ODP.NET托管驱动程序中获得可接受的性能,或者即使Oracle声称它是"最终"版本,它是否还没有生产就绪?除了回到原生ODP.NET提供商之外,我还有其他选择吗?

c# oracle performance odp.net 32bit-64bit

5
推荐指数
1
解决办法
1788
查看次数