小编Arn*_*psa的帖子

ASP.NET MVC,强类型视图,局部视图参数故障

如果我得到了继承自的视图:

System.Web.Mvc.ViewPage<Foo>
Run Code Online (Sandbox Code Playgroud)

其中Foo有一个带有类型字符串的属性栏
和视图想要呈现强类型的局部视图,它继承自:

System.Web.Mvc.ViewUserControl<string>  
Run Code Online (Sandbox Code Playgroud)

像这样:

Html.RenderPartial("_Bar", Model.Bar);%>
Run Code Online (Sandbox Code Playgroud)

那为什么会抛出这个:

传递到字典中的模型项是'Foo'类型,
但是这个字典需要一个'System.String'类型的模型项.

当bar未初始化时?

更具体:为什么它传递Foo,它应该传递null?

asp.net-mvc null strongly-typed-view

22
推荐指数
2
解决办法
9757
查看次数

渴望加载和存储库模式

我想知道如何在使用Repository模式时正确处理复杂对象图的急切加载问题.我猜这不是ORM特有的问题.

第一次尝试:

public interface IProductRepository : IRepository<Product>
{
  Product GetById(int id);
  IProductRepository WithCustomers();
}
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作,但这将涉及一直重复自己(在各地的存储库实现中编写自定义'With'方法).

下一步方法:

public interface IRepository<T> where T : IAggregateRoot
{
  ...
  void With(Expression<Func<T, object>> propToExpand);
}
Run Code Online (Sandbox Code Playgroud)

With 方法会将一个项目添加到私有集合中,稍后将使用该项目来查找在检索必要的实体时应该急切加载哪些道具.

这种工作很好.但我不喜欢用法:

productRepository.With(x=>x.Customer);
productRepository.With(x=>x.Price);
productRepository.With(x=>x.Manufacturer);
var product = productRepository.GetById(id);
Run Code Online (Sandbox Code Playgroud)

基本上 - 问题是没有链接.我希望它是这样的:

var product = productRepository
  .With(x=>x.Customer)
  .With(x=>x.Price)
  .With(x=>x.Manufacturer)
  .GetById(id);
Run Code Online (Sandbox Code Playgroud)

我无法做到这一点.即使我可以 - 我不确定这个解决方案是否优雅.

这导致我缺少一些基本的想法(在任何地方缺乏例子).有没有不同的方法来处理这个?什么是最佳做法?

repository-pattern eager-loading

21
推荐指数
1
解决办法
5407
查看次数

ASP.NET MVC单元测试 - 会话

搜索了StackOverflow和谷歌我认为我正在做的事情是正确的,但结果似乎并不顺利

    [TestMethod]
    public void LoginAction_Should_Return_View_and_User_Authenticated()
    {
        // Arrange
        var mock = new Mock<ControllerContext>();
        var mockSession = new Mock<HttpSessionStateBase>();
        mock.Setup(p => p.HttpContext.Session).Returns(mockSession.Object);

        var testData = FakeUserData.CreateTestUsers();
        var repository = new FakeUserRepository(testData);
        var controller = new AccountController(repository);
        controller.ControllerContext = mock.Object;

        // Act
        var result = controller.Login("testuser1", "testuser1");

        // Assert
        Assert.AreEqual("testuser1", controller.HttpContext.Session["Username"]);
        Assert.IsTrue((bool)controller.HttpContext.Session["IsAuthenticated"]);
        Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
    }
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,controller.HttpContext.Session ["Username"]的值为null,但是我使用Session帮助器将值设置为用户名.我做错了什么,还是其他什么?任何帮助将不胜感激.

asp.net-mvc unit-testing moq session-variables

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

DDD:聚合根问题

假设我有2个实体--Foo和Bar.Foo是一个聚合根,包含Bar.据我所知,它应该是这样的:

public class Foo{
    private readonly Bar Bar;
}
Run Code Online (Sandbox Code Playgroud)

我想为用户提供从定义列表中选择Bars for Foos的功能(并进行更改).

如果存储库应该仅用于聚合根,则意味着Bar实体将没有存储库.

这会导致问题 - 如果没有引用Foo,就无法独立创建/更新Bar.

这是否意味着Bar应该拥有一个存储库,尽管没有Foo它没有意义?

domain-driven-design aggregateroot

16
推荐指数
2
解决办法
4561
查看次数

存储库模式,POCO和业务实体

我知道存储库模式上已有很多线程但不知何故我觉得我的问题有点不同.也许是因为昨天我第一次听说POCO这个词.

我的问题是 - 通常,我在我的业务实体中添加和保存方法.假设我正在写一个Q/A网站,我有以下实体:问题,答案和评论.如果我想使用存储库模式,我基本上只需要保留业务实体中的属性(例如,问题),并将我的操作移动到存储库类(例如,QuestionRepository),对吧?如果这是真的,那么POCO是指具有属性的商业实体吗?

我正在使用Entity Framework 4.0,后者在edmx代码中创建了我的实体.如果我想使用存储库模式,那么就不需要编写我自己的业务实体(问题,答案等),因为它们已经由EF生成了,对吧?我只需要存储库来做CRUD吗?我将为这个例子提供三个存储库,每个实体一个存储库?

.net c# asp.net-mvc entity-framework entity-framework-4

16
推荐指数
3
解决办法
8837
查看次数

如何测试ASP.NET MVC中的动作过滤器?

需要一些指示.找到这个这个,但我仍然有点困惑.

我只是想模拟ActionExecutedContext,传递它,让过滤器稍微工作并检查结果.

有帮助吗?

你可以在这里找到过滤器的来源
(它有点改变,但目前不是重点).

所以 - 我想要单元测试,RememberUrl过滤器足够智能,可以在会话中保存当前的URL.

testing asp.net-mvc action-filter

15
推荐指数
1
解决办法
8542
查看次数

IE7/IE8和冻结GIF动画

我很确定这是一个老问题.

这就是我渲染动画gif的方式:

 <img id='loading' alt='loading' style="display: none; position:  
    relative; left:10px; top:2px;" src="<%= Url.Image("loading.gif") %>" />
Run Code Online (Sandbox Code Playgroud)

这就是我现在拼命想要展示它的方式:

showLoading: function(gifId, butId) {
        var n = gifId != undefined ? gifId : 'loading';
        var l = $('#' + n);

        //if browser is stupid
        if ('v' == '\v') {
            var s = l.attr('src');
            var x = document.getElementById(n);
            x.style.visibility = "visible";
            x.style.display = "inline";
            setTimeout("document.getElementById('" + n + "').src = '"+s+"';",  
                        100);
        } else {
            l.show();
        }
        if (butId != undefined)
            $('#' + butId).css('cursor', 'default').attr("disabled", …
Run Code Online (Sandbox Code Playgroud)

internet-explorer animated-gif

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

如何在c#中调用受保护的构造函数?

如何调用受保护的构造函数?

public class Foo{
  public Foo(a lot of arguments){}
  protected Foo(){}
}
var foo=???
Run Code Online (Sandbox Code Playgroud)

这显然未通过测试:

public class FooMock:Foo{}
var foo=new FooMock();
Assert(typeof(Foo), foo.GetType());
Run Code Online (Sandbox Code Playgroud)

c# inheritance constructor

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

C#4中是否会有通用属性?

所以 - 如果没有特殊原因没有通用属性,
我想知道 - 也许它们会被实现?

那些对ASP.NET MVC动作过滤器来说非常棒.

c# generics attributes

14
推荐指数
2
解决办法
3170
查看次数

Powershell get-eventlog消息列太短

使用PowerShell检索有关事件的信息时消息列被修剪并且太短:

索引时间类型源事件ID消息
----- ---- ---- ------ ------- -------
2 Sep 18 12:50 Info yaddayadda 0分类: Controllers.BasketController ...
1 Sep 18 12:50 Info yaddayadda 0类:Controllers.BasketController ...

有可能看到完整的消息吗?

powershell event-log

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