سؤال

لدي طريقة WCF هذه

Profile GetProfileInfo(string profileType, string profileName)

وقاعدة العمل:

إذا كان profiletype هو "A" قراءة من قاعدة البيانات.

إذا كان profiletype "B" قراءة من ملف XML.

والسؤال هو: كيفية تنفيذها باستخدام حاوية حقن التبعية؟

هل كانت مفيدة؟

المحلول

دعنا نفترض أولاً أن لديك شيء مثل هذا: شيء مثل هذا:

public interface IProfileRepository
{
     Profile GetProfile(string profileName);
}

وكذلك اثنين من التطبيقات: DatabaseProfileRepository و XmlProfileRepository. المشكلة هي أنك ترغب في اختيار القصة الصحيحة بناءً على قيمة profiletype.

يمكنك القيام بذلك عن طريق تقديم هذا مصنع مجردة:

public interface IProfileRepositoryFactory
{
    IProfileRepository Create(string profileType);
}

على افتراض أن iProfiLerePositoryFactory قد تم حقنها في تنفيذ الخدمة ، يمكنك الآن تنفيذ طريقة getProfileInfo مثل هذا:

public Profile GetProfileInfo(string profileType, string profileName)
{
    return this.factory.Create(profileType).GetProfile(profileName);
}

قد يبدو التنفيذ الملموس لـ iProfiLerePositoryFactory هكذا:

public class ProfileRepositoryFactory : IProfileRepositoryFactory
{
    private readonly IProfileRepository aRepository;
    private readonly IProfileRepository bRepository;

    public ProfileRepositoryFactory(IProfileRepository aRepository,
        IProfileRepository bRepository)
    {
        if(aRepository == null)
        {
            throw new ArgumentNullException("aRepository");
        }
        if(bRepository == null)
        {
            throw new ArgumentNullException("bRepository");
        }

        this.aRepository = aRepository;
        this.bRepository = bRepository;
    }

    public IProfileRepository Create(string profileType)
    {
        if(profileType == "A")
        {
            return this.aRepository;
        }
        if(profileType == "B")
        {
            return this.bRepository;
        }

        // and so on...
    }
}

أنت الآن بحاجة فقط إلى الحصول على حاوية DI الخاصة بك لتسليم كل شيء لك ...

نصائح أخرى

إجابة رائعة من قبل مارك ، ولكن الحل المعطى ليس كذلك مصنع مجردة لكن تنفيذ المعيار مصنع نمط. يرجى التحقق من كيفية تناسب فصول العلامات في مخطط UML نمط المصنع القياسي. انقر هنا لترى الفصول المذكورة أعلاه المطبق على نمط المصنع UML

نظرًا لأن مصنع المصنع يدرك الطبقات الملموسة ، يمكننا عمل رمز ProfileRepositoryFactory أبسط بكثير مثل أدناه. تكمن مشكلة حقن المستودعات المختلفة للمصنع في أن لديك المزيد من التغييرات في التعليمات البرمجية في كل مرة تضيف فيها نوعًا جديدًا من الخرسانة. مع الرمز أدناه ، عليك فقط تحديث المفتاح لتضمين فئة خرسانية جديدة


    public class ProfileRepositoryFactory : IProfileRepositoryFactory
    {
        public IProfileRepository Create(string profileType)
        {
            switch(profileType)
            {
                case "A":
                    return new DatabaseProfileRepository(); 

                case  "B":
                    return new XmlProfileRepository();
            }
        }
    }

مصنع مجردة هو نمط أكثر تقدما يستخدم لإنشاء عائلات من الكائنات ذات الصلة أو التابعة دون تحديد فصولها الخرسانية. مخطط فئة UML متاح هنا يشرح ذلك جيدا.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top