小编son*_*ard的帖子

"aaaa".StartsWith("aaa")返回false

如果这不是一个错误,那么有人可以解释这种行为背后的原因吗?事实上,似乎每个奇数个字母都会返回false:

string test = "aaaaaaaaaaaaaaaaaaaa";
Console.WriteLine(test.StartsWith("aa"));
Console.WriteLine(test.StartsWith("aaa"));
Console.WriteLine(test.StartsWith("aaaa"));
Console.WriteLine(test.StartsWith("aaaaa"));
Console.WriteLine(test.StartsWith("aaaaaa"));
Console.WriteLine(test.StartsWith("aaaaaaa"));
Run Code Online (Sandbox Code Playgroud)

在丹麦系统上执行时输出后的输出:

True
False
True
False
True
False
Run Code Online (Sandbox Code Playgroud)

c# string startswith

17
推荐指数
1
解决办法
1608
查看次数

服务定位器和依赖注入

我认为普遍认为以下是不好的

public class Foo
{
    private IService _service;
    public Foo()
    {
        _service = IocContainer.Resolve<IService>();
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是首选(依赖注入)

public class Foo
{
    private IService _service;
    public Foo(IService service)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

但是现在由消费者提供服务.消费者当然也可以在构造函数中要求IService,但是当层次结构变得更深时,它似乎很烦人.在某些时候,有人需要从IoC容器请求IService - 但是什么时候......?

我工作场所的一位前同事为UoW/Repository模式编写了一个UnitOfWork类(使用Microsoft ServiceLocator):

public static UnitOfWork
{
    public static IUnitOfWork Current
    {
        get { return ServiceLocator.Current.GetInstance<IUnitOfWork>(); }
    }

    public static void Commit()
    {
        Current.Commit();
    }

    public static void Dispose()
    {
        Current.Dispose();
    }

    public static IRepository<T> GetRepository<T>() where T : class
    {
        return ServiceLocator.Current.GetInstance<IRepository>();
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用Ninject连接IoC,因此对IRepository的请求将找到当前的UoW或在需要时创建一个新的(如果处理当前电流).使用成为

public class …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection inversion-of-control

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