سؤال

وأنا أحب نمط رأيت في هذا بلوق وظيفة ( HTTP: //marekblotny.blogspot.com/2009/04/conventions-after-rewrite.html )، حيث الكاتب هو التحقق لمعرفة ما إذا كان قد تم إجراء تغيير اسم الجدول قبل تطبيق الاتفاقية.

public bool Accept(IClassMap target)
{
    //apply this convention if table wasn't specified with WithTable(..) method
    return string.IsNullOrEmpty(target.TableName);
}

واجهة اتفاقية أنا باستخدام لطول السلسلة IProperty:

public class DefaultStringLengthConvention: IPropertyConvention
{
    public bool Accept(IProperty property) {
        //apply if the string length hasn't been already been specified
        return ??; <------ ??
    }

    public void Apply(IProperty property) {
        property.WithLengthOf(50);
    }
}

وأنا لا أرى فيها IProperty يعرض أي شيء أن يقول لي إذا كان قد تم تعيين الخاصية. هل هذا ممكن؟

وTIA، Berryl

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

المحلول 3

لتوضيح في التعليمات البرمجية ما تقول ستيوارت وجيمي، وهنا ما يصلح:

public class UserMap : IAutoMappingOverride<User>
{
    public void Override(AutoMap<User> mapping) {
        ...
        const int emailStringLength = 30;
        mapping.Map(x => x.Email)
            .WithLengthOf(emailStringLength)                        // actually set it
            .SetAttribute("length", emailStringLength.ToString());  // flag it is set
        ...

    }
}

public class DefaultStringLengthConvention: IPropertyConvention
{
    public bool Accept(IProperty property) {
        return true;
    }

    public void Apply(IProperty property) {
        // only for strings
        if (!property.PropertyType.Equals(typeof(string))) return;

        // only if not already set
        if (property.HasAttribute("length")) return;

        property.WithLengthOf(50);
    }
}

نصائح أخرى

و.WithLengthOf() يضيف "التغيير" (Action<XmlElement>) إلى قائمة تعديلات على تطبيقها عند إنشاء خرائط XML. للأسف، هذا المجال هو private وليس هناك خاصية للوصول إلى قائمة التغيير، لذلك أخشى هناك (حاليا) أي وسيلة للتحقق مما إذا خريطة الممتلكات تمت زيارتها WithLengthOf المطبق عليه.

وحتى أفضل بديل يأتي جنبا إلى جنب، يمكنك استخدام HasAttribute("length").

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