0

インスタンスを StructureMap に要求し、すぐにパラメーターを渡して、(渡したパラメーターを使用して) インスタンスを構成しようとしていますが、これを行う方法がわかりません!

ここにコードがあります。私がやりたいことを理解するのは簡単です。

class MyRegistry : Registry {

public MyRegistry() {

    // Use a new Connection for each instance
    For<ISQLRepositoryCommands>().Use(x => new SQLRepositoryCommands(DatabaseConnections.NewSqlConnection()));

    // WHAT I Want to do is.. remove the line above of code and let only this.
    For<ISQLRepositoryCommands>()
    .Use(x => {
        if( x.arguments != null ) {
            // Specific connection passed by parameter
            return new SQLRepositoryCommands( (SqlConnection) x.arguments[0]);
        }       
        // default connection
        return new SQLRepositoryCommands(DatabaseConnections.NewSqlConnection());
    }); 
}   

}

メイン

public static class StartUseStructureMap {
public static void Main() {
    // SQLRepositoryCommands with default connection associated
    var def = ObjectFactory.GetInstance<ISQLRepositoryCommands>();

    // SQLRepositoryCommands with custom connection associated
    var personalizedConnection = new SqlConnection("cstring");
    var personalized = ObjectFactory.GetInstance<ISQLRepositoryCommands>(new Arguments[] { new Argument(personalizedConnection) };
}

}

4

1 に答える 1

0

この疑似コードは私の考えに過ぎず、それを達成するためのより良い方法があるかもしれません。しかし、これが私がすることです。

使用できるものの 1 つはファクトリ パターンですが、そのためにはコードを少し調整する必要があります。SqlConnectionしかし、そのステップに進む前に、登録コードからの直接使用を取り除く必要があります。StructureMap はすべてのオブジェクトを明示的に登録する必要はありませんが、StructureMap に登録でき、SqlConnection が作成されるたびに呼び出されるラッパーを作成できます。そうすれば、接続文字列 (空かどうか) に応じて作成された SqlConnection のロジックを配置できます。これがどのように見えるかです:

public interface ISQLConnectionWrapper
{
      SqlConnection GetConnection(string connectionName);
}

public class SQLConnectionWrapper : ISQLConnectionWrapper
{
    public SqlConnection GetConnection(string connectionName)
    {
        if (string.IsNullOrWhiteSpace(connectionName))
            return DatabaseConnections.NewSqlConnection();
        return new SqlConnection(connectionName);
    }
}

次に、作成する接続を認識するラッパーを返すコンストラクターでビルダーを取得するために、SQLRepositoryCommands クラスを少し調整できます。

public class SQLRepositoryCommands : ISQLRepositoryCommands
{
    private readonly Func<string, ISQLConnectionWrapper> _aBuilder;
    public SQLRepositoryCommands(Func<string, ISQLConnectionWrapper> aBuilder)
    {
        // guard clause first of course.
        _aBuilder = aBuilder;
    }

    public void DoWork(string connectionName = null)
    {
        var connection = _aBuilder(connectionName);
    }
}

実際にビルダーを使用して正しい接続を取得し、作業を行う DoWork メソッドを追加しました。

次に、それを登録する方法は次のとおりです。

// register
ObjectFactory.Initialize( c =>
    {
              c.For<ISQLConnectionWrapper>().Use<SQLConnectionWrapper>();
              c.For<Func<string, SqlConnection>>().Use(d => (s => ObjectFactory.GetInstance<ISQLConnectionWrapper>().GetConnection(s)));
        c.For<ISQLRepositoryCommands>().Use<SQLRepositoryCommands>();
    });

そして、これを使用する方法は次のとおりです。

// use
var command = ObjectFactory.GetInstance<ISQLRepositoryCommands>();
command.DoWork("MyConnection"); // specific connection
command.DoWork(); // default connection

私が言ったように。もっと良い方法があるかもしれませんが、このコードは機能するはずです。

于 2012-11-30T09:22:46.363 に答える