0

顧客向けの請求書を生成する必要がありますが、住んでいる場所によって、請求書に含まれるデータはほぼ同じですが、形式が大きく異なります。これを処理する方法として、次のようないくつかの方法を考えました。

public void GenerateBill(User user)
{
if(user.Location == "blah blah")
{
  //Code specific to this location
}
else if (user.Location == "dah dah")
{
  //Code specific to this location
}
}

上記の方法は、特に別の場所の新しいユーザーがポップアップし続けると、非常に長くなる可能性があるようです. 私は次のようなことを考えました:

public void GenerateBillForUserInBlah();

public void GenerateBilForUserInDah();

しかし、上記も手に負えなくなり、メンテナンスの悪夢になるようです。

私の他のアイデアは、次のようなインターフェースを使用してから、次のIGenerateBillようなことをすることでした:

class UserInBlah : IGenerateBill
{
//Implement IGenerateBill members

}

ただし、上記では、ユーザーごとにクラスを作成する必要があります。私はまだ上記のことをしなければならないかもしれませんが、Managed Extension Frameworkここで役立つかどうかはわかりません。代替ソリューションはありますか?主な問題は、実行時までユーザーの場所がわからないため、実行時にユーザーの場所に応じて正しいメソッドを呼び出す必要があることです。

4

2 に答える 2

2

もちろん、このアプローチ:

public interface IGenerateBill {}

他のものよりもエレガントで拡張性があります。はい、MEF がお手伝いします。
MEF には「パーツ メタデータ」という概念があります。よく知らない場合は、ここで読むことができます。

概念的には、MEF メタデータを使用するとIGenerateBill、国コードやロケールなどを含む実装を記述できます。後で実行時に、次のような方法で適切な実装を取得できます。

[BillGenerator("en-us")]
public class EnUsGenerateBill : IGenerateBill {}
[BillGenerator("ru-ru")]
public class RuRuGenerateBill : IGenerateBill {}
[BillGenerator("de-de")]
public class DeDeGenerateBill : IGenerateBill {}

container.GetExports<IGenerateBill, BillGeneratorMetadata>().Single(export => export.Metadata.Locale == "en-us");
于 2012-10-05T06:18:40.663 に答える
0

場所の名前を返すすべてのクラス内にプロパティを持つことができます

例:

        interface IGenerateBill
        {
            void GenerateBill();
            string SupportedCountry { get; }
        }

        class UserInUs : IGenerateBill
        {
        //Implement IGenerateBill members

            public void GenerateBill()
            {
                //generate bill
            }

            public string SupportedCountry
            {
                get { return "US"; }
            }
        }
        class UserInRussia : IGenerateBill
        {
        //Implement IGenerateBill members

            public void GenerateBill()
            {
                //generate bill
            }

            public string SupportedCountry
            {
                get { return "Russia"; }
            }
        }

クライアント側のコードは次のようになります:

        string country = //Get from user in runtime
        var billGenerator = BillgeneratorFactory.ResolveAll().ToList().First
        (x => x.SupportedCountry == country);
        billGenerator.GenerateBill;
于 2012-10-05T06:39:25.933 に答える