-1

2 つのインターフェイスがあり、どちらもまったく同じプロパティを持っています。

なぜ私がこのような 2 つのインターフェースを持っているのか不思議に思っているかもしれませんが、それは長い話ですが、そうです、そうでなければなりません。

条件に基づいて、リストが返される条件とは逆の場合にリストが返されます。

以下のインターフェースとコードを見ると、1 つのオブジェクトを使用できるようにする必要があります。つまり、どのインターフェースが返されてもかまわない場合は、1 つのオブジェクトを操作し、1 つの List インターフェースをループしないようにする必要があります。他のプロパティを設定します。

私はこのようなものが必要です

compParts = genCompParts;

--- コードの使用法

    public class ComponentParts : IComponentParts
    {
        public ComponentParts() { }
        public ComponentParts(Guid userID, int compID, bool isGeneric)
        {
            List<IComponentPart> compParts = null;
            List<IComponentPart_Max> genCompParts = null;

            if (isGeneric)
            {
                genCompParts = GenericCatalogBL.GenericCatalogManagerBL.GetComponentPartsMax(compID);
            }
            else
            {
                compParts = CatalogManagerDL.GetComponentParts(userID, compID);
            }

            var verParts = compParts.Where(x => x.CompTypeName.ToLower().Contains("vertical"));
            if (verParts.Count() > 0) { this.Vertical = verParts.ToList<IComponentPart>(); }

            var horParts = compParts.Where(x => x.CompTypeName.ToLower().Contains("horizontal"));
            if (horParts.Count() > 0) { this.Horizontal = horParts.ToList<IComponentPart>(); }

//... redundant code omitted

---インターフェースのスナップショット---

インターフェースのスナップショット


最終的にクラス ライブラリ コール インターフェイスを作成し、ソリューション内のさまざまなプログラム間でこれらのインターフェイスを共有するだけです。

そもそも怠惰な私がやるべきことです。

4

1 に答える 1

0

IComponentPartどちらも所有していないか、いずれかIComponentPart_Maxを修正できないと仮定すると、完全に力ずくの方法です。

あなたがコントロールする新しいインターフェースを作る

interface IComponentPart {
    string BrandGroup {get; set;}
    int BrandID {get; set;}
    // ...
}

両方の既存のインターフェースのラッパーを作成して、それらをインターフェースに適合させます

class IComponentPartWrapper : IComponentPart  {
    private readonly CatelogDL.IComponentPart _underlyingPart;

    public IComponentPartWrapper(CatelogDL.IComponentPart underlyingPart) {
        _underlyingPart = underlyingPart
    }

    public string BrandGroup {
        get {return _underlyingPart.BrandGroup;}
        set {_underlyingPart.BrandGroup = value;}
    }

    public int BrandID {
        get {return _underlyingPart.BrandID ;}
        set {_underlyingPart.BrandID = value;}
    }

    // ...
}

class IComponentPart_MaxWrapper : IComponentPart  {
    private readonly GenericCatalogDL.IComponentPart_Max _underlyingPart;

    public IComponentPartWrapper(GenericCatalogDL.IComponentPart_Max underlyingPart) {
        _underlyingPart = underlyingPart
    }

    public string BrandGroup {
        get {return _underlyingPart.BrandGroup;}
        set {_underlyingPart.BrandGroup = value;}
    }

    public int BrandID {
        get {return _underlyingPart.BrandID ;}
        set {_underlyingPart.BrandID = value;}
    }

    // ...
}

コードでインターフェイスを使用し、いずれかのライブラリからの結果を対応するラッパーでラップします。

public class ComponentParts : IComponentParts
{
    public ComponentParts() { }
    public ComponentParts(Guid userID, int compID, bool isGeneric)
    {
        List<IComponentPart> compParts;

        if (isGeneric)
        {
            compParts = GenericCatalogBL.GenericCatalogManagerBL.GetComponentPartsMax(compID)
                .Select(x => new IComponentPart_MaxWrapper(x))
                .ToList();
        }
        else
        {
            compParts = CatalogManagerDL.GetComponentParts(userID, compID)
                .Select(x => new IComponentPartWrapper(x))
                .ToList();
        }

        // ...
于 2013-10-25T23:02:29.653 に答える