1
我使用將在我們的實體中動態創建集合實例的方法。我的問題是,當我創建它必須被插入到數據庫中的新記錄,我ICollection
導航屬性始終是空,在我安全的方法使用反射創建List的實例c#
我必須使用像下面這樣來創建新的List
及其絕對不是好辦法。如果我不創建List
的實例,則Mapper
會拋出錯誤,表明它不能將Collection
的某些內容映射到null
,例如它不能將Categories
的列表映射爲空。
樣品我的安全方法,它具有如上所示的例如new List<Category>()
創建List<T>
實例
//check if record exists in database so i know if have to update or insert record (based on entity Id)
var entity = repository.GetById(detail.Id)
//....some code removed for brevity
if (entity.Categories == null)
{
entity.Categories = new List<Category>();
}
if (entity.UserContacts == null)
{
entity.UserContacts = new List<UserContact>();
}
//dto => entity
Mapper.PopulateEntity(dto, entity);
//update or insert later on.
擴展方法。
public TEntity InitializeEntity(TEntity entity)
{
var properties = entity.GetType().GetProperties();
foreach (var prop in properties)
{
if (typeof(ICollection<TEntity>).Name == (prop.PropertyType.Name))
{
var get = prop.GetGetMethod();
//get assembly with entity namespace and class name
var fullName = get.GetBaseDefinition().ReturnType.GenericTypeArguments[0].AssemblyQualifiedName;
//create instance of object
var myObj = Activator.CreateInstance(Type.GetType(fullName));
//check if property is null or if get some value, dont want to rewrite values from database
var value = prop.GetValue(entity);
if (value == null)
{
var listType = typeof(List<>);
var constructedListType = listType.MakeGenericType(myObj.GetType());
Activator.CreateInstance(constructedListType);
}
}
}
return entity;
}
出於某種原因,它不是建立在所有的任何實例,我想不出哪裏的問題。
呀它更清潔的版本和完美的作品。謝謝 – Martin