유창한 nhibernte 변경 / 컨벤션
-
03-07-2019 - |
문제
나는이 블로그 게시물에서 본 패턴을 좋아한다 (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가 부동산이 이미 설정되었는지 알려주는 내용이 어디에 있는지 알 수 없습니다. 이게 가능해?
티아, 베리
해결책 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")
.
제휴하지 않습니다 StackOverflow