小编Mad*_*Max的帖子

使用View.Render方法将MVC视图转换为字符串的单元测试方法

我编写了将MVC视图转换为字符串和测试方法的方法,以检查它是否返回字符串.

当然它适用于Web,但是当我在NUnit中运行测试时,当方法试图调用View.Render时,它会在System.Web中抛出NullReferenceException.

这是StackTrace:

   w System.Web.VirtualPath.GetCacheKey()
   w System.Web.Compilation.BuildManager.GetCacheKeyFromVirtualPath(VirtualPath virtualPath, Boolean& keyFromVPP)
   w System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath virtualPath, Boolean ensureIsUpToDate)
   w System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
   w System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
   w System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
   w System.Web.Compilation.BuildManager.GetObjectFactory(String virtualPath, Boolean throwIfNotFound)
   w System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists(String virtualPath)
   w System.Web.Mvc.BuildManagerViewEngine.FileExists(ControllerContext controllerContext, String virtualPath)
   w System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromSpecificName(ControllerContext controllerContext, String name, String cacheKey, String[]& searchedLocations)
   w System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc nunit unit-testing controllercontext

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

c#在域驱动设计中放置业务规则

我正在学习域驱动设计,我对我应该制定业务规则的地方有点困惑.假设我有一个名为Member的聚合根,它保存对实体密码的引用.密码看起来像这样:

public class Password
{
    public string Hash { get; private set; }
    public string Salt { get; private set; }
    public DateTime ExpirationDate { get; private set; }

    public void GenerateNewPassword(string plainPassword)
    {
        //Some logic here
    }
}
Run Code Online (Sandbox Code Playgroud)

我还有一个名为Settings的设置对象.设置从设置存储库加载.该对象引用了另一个名为PasswordRules的对象.密码规则当然会检查方法CheckPasswordRequirements中的密码要求:

public bool CheckPasswordRequirements(string password)
{
    //Some logic here
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,它是保存此密码规则的最佳位置,还是应该将此方法移至Password类,因为密码对象负责检查给定的普通密码是否符合要求(然后我还应将设置存储库放在密码实体中)或者是否应该在创建成员对象的服务中直接进行此检查?也许还有其他一些优雅的解决方案?

c# domain-driven-design business-rules

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

域服务注入域实体

我正在学习域驱动设计,并且对实体和向其注入域服务有些困惑。我发现此博客的结论是将服务注入实体是一个坏主意。我部分同意这一点,但是在这种情况下该怎么做:我有一个User实体,它是一个聚合根,其中包含Password值对象。看起来像这样:

密码值对象:

public class Password  
{  
    public string Hash { get; private set; }

    public string Salt { get; private set; }

    private readonly IHashGeneratorService _hashGeneratorService;

    public Password(IHashGeneratorService hashGeneratorService)
    {
        _hashGeneratorService = hashGeneratorService;
    }

    public void GenerateHash(string inputString)
    {
        //Some logic

        Salt = hashGeneratorService.GenerateSalt();
        Hash = hashGeneratorService.GenerateHash(inputString);
    }
}
Run Code Online (Sandbox Code Playgroud)

用户实体:

public class User
{
    public Password Password { get; private set; }

    public User(IHashGeneratorService hashGeneratorService)
    {
        this.Password = new Password(hashGeneratorService);
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,如果我通过工厂创建User实体,则需要向工厂构造函数或Create()方法提供IHashGeneratorService实现。在那之后,如果我的工厂被使用,例如。SomeUserService我必须为其提供实现(例如,通过ctor注入)。依此类推...老实说,它从我身上散发出来,因为我的许多类都依赖于哈希生成器服务实现,但只有密码类使用它。而且我认为这也违反了密码类的SRP原理。

我发现了一些解决方案:

  1. 使用服务定位器。但是它也有气味,因为它是一种反模式,如果我们使用它很难测试和管理实体。

  2. 直接在密码方法中实现哈希算法。 …

c# domain-driven-design dependency-injection

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