NameValueCollection
ファクトリクラスにを渡すだけです。
public SomeDomainObject ConstructDomainObject(NameValueCollection data)
{
...
}
次に、コントローラーで次のようにします。
var domainModel = factory.ConstructDomainObject(Request.Params);
ConstructDomainObject
これで、メソッドを好きなだけ単体テストできます。
コントローラを個別にテストする場合は、このファクトリオブジェクトにインターフェイスを実装させます。
public interface IMyFactory
{
SomeDomainObject ConstructDomainObject(NameValueCollection data);
}
コントローラが動作するもの:
public class SomeController: Controller
{
private readonly IMyFactory factory;
public SomeController(IMyFactory factory)
{
this.factory = factory;
}
public ActionResult SomeAction()
{
var domainModel = this.factory.ConstructDomainObject(Request.Params);
...
}
}
そうは言っても、カスタムモデルバインダーを書くことを検討しましたか?ASP.NET MVCでは、モデルバインダーの責任はまさに次のとおりです。要求からモデルをインスタンス化します。このように、いくつかのホイールを再発明する必要はありませんが、この目的のためにフレームワークにネイティブに組み込まれているものを使用します。