小编NWa*_*ard的帖子

使用Unity的ASP.NET MVC中的基本控制器构造函数注入

我的MVC 5项目中有一个基本控制器,它实现了一些共享功能.此功能需要一些依赖项.我正在使用Unity 3将这些实现注入到我的控制器中,这种模式运行良好,直到我将控制器切换为从该基本控制器继承.现在我遇到了以下问题:

public class BaseController : Controller
{
    private readonly IService _service;

    public BaseController(IService service)
    {
        _service = service;
    }
}

public class ChildController : BaseController
{
    private readonly IDifferentService _differentService;

    public ChildController(IDifferentService differentService)
    {
        _differentService = differentService;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是可以理解的抛出错误'BaseController' does not contain a constructor that takes 0 arguments.Unity没有解析BaseController的构造,因此它无法将依赖项注入其中.我看到了解决这个问题的两种明显方法:

1.)显式调用BaseController ctor并让每个ChildController ctor注入BaseController的依赖项

public class ChildController : BaseController
{
    private readonly IDifferentService _differentService;

    public ChildController(IDifferentService differentService,
                           IService baseService)
        : base(baseService)
    {
        _differentService = differentService;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不喜欢这种方法有几个原因:一,因为ChildControllers没有使用额外的依赖项(因此它会导致子控制器中的构造函数膨胀),更重要的是,如果我更改了构造函数签名基本控制器,我必须更改每个子控制器的构造函数签名. …

c# asp.net-mvc dependency-injection inversion-of-control unity-container

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

合并提交不会出现在git rebase --interactive中

git log 揭示以下内容:

commit 1abcd[...]
Author: [...]
Date: [...]

    [Useful commit]

commit 2abcd[...]
Author: [...]
Date: [...]

    Merge branch [...] of [etc. etc.]

commit 3abcd[...]
Author: [...]
Date: [...]

    [Useful commit]
Run Code Online (Sandbox Code Playgroud)

那个合并提交对我来说没用 - 它不代表一个有意义的分支状态,而是从远程拉动生成的,所以我有远程历史的真实提交 - 不需要提交来标记我拉的事实.我想压缩这个合并提交.我常用的壁球技术是:

git rebase --interactive HEAD~2 (或者我需要去的远方)

然后我会把它压成邻近的提交.我做了一些这样的事情,例如我做了一个提交,意识到我错过了一个很小的重要细节(单个文件,或者没有更改其中一个文件中的一行),并做了另一个提交,基本上只是一个快速的oops.这样,当我将我的更改推回到遥控器时,一切都很干净,并且讲述了一个有凝聚力的叙述.

但是,在这种情况下,当我运行git rebase ...命令时,提交2abcd不会出现!它似乎跳过了正确的2abcd显示1abcd3abcd.合并提交有什么特别之处可以阻止它出现git rebase --interactive吗?我可以用什么其他技术来压缩合并提交?

UPDATE每@蛋糕的要求:

输出git log --graph --oneline --decorate看起来像这样:

* 1abcd (useful commit)
* 2abcd (merge)
|  \ <-- from remote …
Run Code Online (Sandbox Code Playgroud)

git git-interactive-rebase

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

通过反射在属性上设置私有 setter

我知道这有几个规范的答案(我过去成功使用过!)请参阅/sf/answers/124488731//sf/answers/109603651/。作为参考,这些方法包括使用SetValue上的方法PropertyInfo的属性的,或以其他方式调用setter方法用的反射,或在极端情况下,直接设置支持字段。但是,这些方法现在似乎都不适合我,我很好奇是否发生了破坏此功能的更改。

考虑以下:

using System;
using System.Reflection;

public class Program
{
    public static void Main()
    {
        var exampleInstance = new Example();
        var exampleType = typeof(Example);
        var fooProperty = exampleType.GetProperty("Foo"); // this works fine - the "GetSetMethod" method returns null, however

        // fails with the following:
        // [System.MethodAccessException: Attempt by method 'Program.Main()' to access method 'Example.set_Foo(Int32)' failed.]
        //fooProperty.SetValue(exampleInstance, 24);

        // same error here:
        //Run-time exception (line 14): Attempt by method 'Program.Main()' to access method …
Run Code Online (Sandbox Code Playgroud)

c# reflection c#-7.2

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

AutoMapper不一致地自动解析字符串 - > DateTime

我的期望是AutoMapper(3.3.0)不会自动解析字符串 - > DateTime转换,即使字符串是一种易于理解的格式.在图书馆作者吉米·博加德(Jimmy Bogard)对此StackOverflow答案的评论中,注意到缺少一个默认字符串 - > DateTime转换器(尽管是四年前):https://stackoverflow.com/a/4915449/1675729

但是,我有一个.NET小提琴,它似乎暗示AutoMapper 可以默认处理这个映射:https://dotnetfiddle.net/dDtUGx

在该示例中,Zing属性从stringin 映射FooDateTimein in Bar,而不指定自定义映射或解析器.

但是,当此代码在我的解决方案单元测试中运行时(使用相同的AutoMapper版本),它会产生我期望的异常,即:

AutoMapper.AutoMapperMappingExceptionMissing type map configuration or unsupported mapping.
Mapping types:  
    String -> DateTime
    System.String -> System.DateTime
Destination path:
    Bar.Zing
Source value:
    Friday, December 26, 2014
Run Code Online (Sandbox Code Playgroud)

导致这种不一致行为的原因是什么?

为了完整起见,.NET Fiddle中的代码在此处转载:

using System;
using AutoMapper;

public class Program
{
    public static void Main()
    {
        var foo = new Foo();
        foo.Zing = DateTime.Now.ToLongDateString();
        Mapper.CreateMap<Foo, Bar>();
        var …
Run Code Online (Sandbox Code Playgroud)

c# string datetime automapper

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