using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.Entities.Files;
using Model.UnitsOfWork;
namespaceModel.Entities.Base
{
publicabstractclassBase_FS_Repository<T> : BaseRepository<T> whereT : FS_Item
{
protected DbSet<FS_Item> Set_Fs => context.Set<FS_Item>();
protectedreadonly Enum_BaseDirectoryEntity RepoType;
publicoverridevoidRepoInit() { }
publicBase_FS_Repository(UOW UOW, Enum_BaseDirectoryEntity type) : base(UOW)
{
this.RepoType = type;
}
//public override T GetFromDBNoChange(T elem)//{// using (var context = new Context())// {// var en = context.Set<FS_Item>().// FirstOrDefault(e => e.ID == elem.ID);//.// //Include(e => e.Parent).// //FirstOrDefault();// //context.Entry(en).Reference(e => e.Parent).Load();// var p = en.PhysicalPath;// return (T)en;// }//}publicoverride T Create(T elem)
{
if (elem.Type != RepoType)
throw Repo_Exception<T>.Factory(this, elem, Repo_Exceptions.FSRepo_TypeException);
returnbase.Create(elem);
}
publicoverridevoidUpdate(T elem)
{
if (elem.Type != RepoType)
throw Repo_Exception<T>.Factory(this, elem, Repo_Exceptions.FSRepo_TypeException);
base.Update(elem);
}
publicoverridevoidDelete(T elem)
{
if (elem.Type != RepoType)
throw Repo_Exception<T>.Factory(this, elem, Repo_Exceptions.FSRepo_TypeException);
base.Delete(elem);
}
///<summary>/// Рекурсия/// Функция удаляет данные о файлах из базы, при этом не влияя на ФС/// Обычно вызывается при сканировании ФС, когда папка/файл из базы не найдена в ФС///</summary>///<param name="elem"></param>publicvirtualvoidDeleteInList(FS_Item elem)
{
_DeleteInList(elem);
context.SaveChanges();
}
privatevoid _DeleteInList(FS_Item elem)
{
var items = elem.Items;
//При foreach вылетает exception изменение коллекцииfor (int i = 0; i < items.Count; i++)
{
DeleteInList(items[i]);
}
Set_Fs.Remove(elem);
}
}
}