У меня есть класс LocalizedString
, используемый для хранения локализации одного значения.Концепция основана на посте Фабио Мауло .
Я использую новую концепцию Mapping-By-Code в NHibernate 3.2, но, похоже, игнорирует реализацию IUserType
потому что, когда он генерирует SQL, он создает столбец с другим именем и строкой по умолчанию NVARCHAR(255)
type.
Я пытаюсь отобразить этот простой класс
public class Region : Entity
{
/// <summary>
/// Initializes a new instance of the <see cref="Region"/> class.
/// </summary>
public Region()
{
}
/// <summary>
/// Gets or sets the localized name of the <see cref="Region"/>.
/// </summary>
public virtual LocalizedString Name { get; set; }
}
В результате получается SQL
create table Regions (RegionId INT not null, Item NVARCHAR(255) not null, primary key (RegionId))
Столбец Item
здесь должен называться Name
и иметь тип XML.Я предполагаю, что имя столбца происходит от имени индексатора LocalizedString
.
Это моя конфигурация NHibernate (она не завершена, я нахожусь в процессе создания соглашения)
private static Configuration CreateNHibernateConfiguration()
{
var cfg = new Configuration();
cfg.Proxy(p => p.ProxyFactoryFactory<NHibernate.Bytecode.DefaultProxyFactoryFactory>())
.DataBaseIntegration(db =>
{
db.ConnectionStringName = "***";
db.Dialect<MsSql2008Dialect>();
db.BatchSize = 500;
});
var mapper = new ConventionModelMapper();
var baseEntityType = typeof(Entity);
mapper.IsEntity((t, declared) => baseEntityType.IsAssignableFrom(t) && baseEntityType != t && !t.IsInterface);
mapper.IsRootEntity((t, declared) => baseEntityType.Equals(t.BaseType));
mapper.BeforeMapClass += (mi, t, map) =>
{
map.Table(Inflector.MakePlural(t.Name));
map.Id(x =>
{
x.Column(t.Name + "Id");
});
};
mapper.BeforeMapManyToOne += (insp, prop, map) =>
{
map.Column(prop.LocalMember.GetPropertyOrFieldType().Name + "Id");
map.Cascade(Cascade.Persist);
};
mapper.BeforeMapBag += (insp, prop, map) =>
{
map.Key(km => km.Column(prop.GetContainerEntity(insp).Name + "Id"));
map.Cascade(Cascade.All);
};
mapper.BeforeMapProperty += (insp, prop, map) =>
{
map.NotNullable(true);
};
var exportedTypes = baseEntityType.Assembly.GetExportedTypes();
mapper.AddMappings(exportedTypes.Where(t => t.Namespace.EndsWith("Mappings", StringComparison.Ordinal)));
var mapping = mapper.CompileMappingFor(exportedTypes.Where(t => t.Namespace.EndsWith("Data", StringComparison.Ordinal)));
cfg.AddDeserializedMapping(mapping, "MyModel");
SchemaMetadataUpdater.QuoteTableAndColumns(cfg);
return cfg;
}
Это определение IUserType
моего LocalizedString
класса:
/// <summary>
/// Defines a string that can have a different value in multiple cultures.
/// </summary>
public sealed partial class LocalizedString : IUserType
{
object IUserType.Assemble(object cached, object owner)
{
var value = cached as string;
if (value != null)
{
return LocalizedString.Parse(value);
}
return null;
}
object IUserType.DeepCopy(object value)
{
var toCopy = value as LocalizedString;
if (toCopy == null)
{
return null;
}
var localizedString = new LocalizedString();
foreach (var localizedValue in toCopy.localizedValues)
{
localizedString.localizedValues.Add(localizedValue.Key, localizedValue.Value);
}
return localizedString;
}
object IUserType.Disassemble(object value)
{
var localizedString = value as LocalizedString;
if (localizedString != null)
{
return localizedString.ToXml();
}
return null;
}
bool IUserType.Equals(object x, object y)
{
if (x == null && y == null)
{
return true;
}
if (x == null || y == null)
{
return false;
}
var localizedStringX = (LocalizedString)x;
var localizedStringY = (LocalizedString)y;
if (localizedStringX.localizedValues.Count() != localizedStringY.localizedValues.Count())
{
return false;
}
foreach (var value in localizedStringX.localizedValues)
{
if (!localizedStringY.localizedValues.ContainsKey(value.Key) || localizedStringY.localizedValues[value.Key] == value.Value)
{
return false;
}
}
return true;
}
int IUserType.GetHashCode(object x)
{
if (x == null)
{
throw new ArgumentNullException("x");
}
return x.GetHashCode();
}
bool IUserType.IsMutable
{
get { return true; }
}
object IUserType.NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
{
if (rs == null)
{
throw new ArgumentNullException("rs");
}
if (names == null)
{
throw new ArgumentNullException("names");
}
if (names.Length != 1)
{
throw new InvalidOperationException("names array has more than one element. can't handle this!");
}
var val = rs[names[0]] as string;
if (val != null)
{
return LocalizedString.Parse(val);
}
return null;
}
void IUserType.NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
{
if (cmd == null)
{
throw new ArgumentNullException("cmd");
}
var parameter = (DbParameter)cmd.Parameters[index];
var localizedString = value as LocalizedString;
if (localizedString == null)
{
parameter.Value = DBNull.Value;
}
else
{
parameter.Value = localizedString.ToXml();
}
}
object IUserType.Replace(object original, object target, object owner)
{
throw new NotImplementedException();
}
Type IUserType.ReturnedType
{
get { return typeof(LocalizedString); }
}
NHibernate.SqlTypes.SqlType[] IUserType.SqlTypes
{
get { return new[] { new XmlSqlType() }; }
}
}