4

.NET 4.0の最新のautofacバージョンと最新のC#ドライバー

Autofac DIコンテナをMongoDBアプリに統合する過程にあり、私の側のBUD(Bad User on Device)である可能性があるという例外を除いて、物事は順調に進んでいます。私のグーグルフーは私に失敗したので、私はあなたの知恵を利用するだろうと思いました。

問題は、インターフェースベースのポリモーフィズムをサポートし、データレイヤー外のすべての対話をconcreateエンティティタイプに気付かないようにしたいということです。簡単な例を次に示します。

///Datastore wrapper
public interfaces IDataStore
{
    T Get<T>(string key);
    IQueryable<T> GetCollection(string key)        
}

public interface ITransaction
{
    public string key {get;}
    public double value {get;}
}

public interface IStockTransaction: ITransaction
{
    public string Symbol{get;}
}

public class StockTransaction: IStockTransaction
{
    public string key {get;}
    public double value {get;}
    public double price {get;}
}

//The goal is to get this test to pass. 
[TestMethod()]
public void PolyMorphicTest()
{
    //Wrapper around on Autofac.
    var container = new ContainerConfiguration();
    var target= container.Resolve<IDataStore>();
    //Assume a stock transaction with a key named foo has been saved. 
    var type = target.Get<IStockTransaction>("foo");
    Assert.IsInstanceOfType(type,typeof(StockSymbol));
} 

さまざまなタイプのAutofac登録アプローチを試しましたが、Mongoタイプ(MongoDatabase.GetCollection()などの呼び出し)のアクティベーションチェーンから完全に外れているため、すべてが爆発しているようです。Autofacを使用してMongoタイプのアクティベーションチェーンを制御する方法はありますか?私たちが望むように機能するためには、Autofacを使用して、上位層のコードによって解決された型に渡される型パラメーターTを置き換える方法を見つける必要があります。次に、渡された型として返すために分散に依存できるはずです。

T Get<T>(string key) //T Arrives as IStockTransaction 
{
    var coll = Database.GetCollection<T>(T.FullName).AsQueryAble<T>();
    /*T is now the concrete type StockTransaction so that Mongo can query properly.*/

    /* perform a linq query here against the IQueryable. */ 

    return  (T) ((from tmp in coll 
        where tmp.key == " ").FirstOrDefault());  
}

IQueryable<T> GetCollection<T>(string key) //T Arrives as IStockTransaction 
{        
    /*Attempting to Find the Mongo collection using the interface type will lead to Mongo
    serialization errors so we need to change the interface type to the concrete type that we 
    have registered in Autofac or have stashed off manually somewhere before this call. Ideally 
    this would be done with a simple resolve call against Autofac so that if our concrete types 
    change, all of this would still work. */

    var coll = Database.GetCollection<T>(T.FullName).AsQueryAble<T>(); 

    /*Here we would rely on C# 4.0 variance to allow us to cast back to the interface type.*/

    return  (T) coll.FirstOrDefault();  
}

長い投稿で申し訳ありません-アイデアをいただければ幸いです。質問が不明な場合は、喜んで明確にしてください。現在、Autofacが型の解決に不可欠であるように、MongoDriverをハッキングすることに傾倒しています。私たちの他のオプションは、ドライバーがインターフェースとDIをよりよくサポートするまで待っています(私はそれが来ると聞いています)。ご協力いただきありがとうございます。

4

0 に答える 0