BusinessListBase< T>.移除不工作
本文关键字:工作 BusinessListBase | 更新日期: 2023-09-27 18:17:48
我是CSLA的新手。Net,我有一些困难从列表中删除子对象。
我有三个类:
-
Scenario : BusinessBase<Scenario>
-
ScenarioPermissions : BusinessListBase<ScenarioPermissions, ScenarioPermission>
-
ScenarioPermission : BusinessBase<ScenarioPermission>
Scenario
类包含一个ScenarioPermissions
类型的字段。
当我尝试做Scenario.Permissions.Add(obj)
和Scenario.Save()
时,它工作正常。
但是如果我想做Scenario.Permissions.Remove(obj)
和Scenario.Save()
,它继续没有从数据库中删除我的ScenarioPermission
对象。
public class Scenario : BusinessBase<Scenario>, IMakeCopy
{
private static readonly PropertyInfo<ScenarioPermissions> m_PermissionsProperty = RegisterProperty<ScenarioPermissions>(c => c.m_Permissions);
public ScenarioPermissions m_Permissions
{
get
{
if (!FieldManager.FieldExists(m_PermissionsProperty))
{
SetProperty(m_PermissionsProperty, ScenarioPermissions.NewPermissions());
}
return GetProperty(m_PermissionsProperty);
}
}
public ReadOnlyCollection<Model.Users.User> Permissions
{
get
{
var collection = new List<Model.Users.User>();
foreach (var item in m_Permissions)
{
collection.Add(Model.Users.User.GetUser(item.UserID));
}
//Adds administrators users...
var admins = Users.Users.GetUsers(true).Where(u => u.Role.InvariantName == "SystemAdministrator" || u.Role.InvariantName == "SuperAdministrator");
foreach (var item in admins)
{
collection.Add(item);
}
return new ReadOnlyCollection<Model.Users.User>(collection.OrderBy(u => u.FullName).ToArray());
}
}
public void AddPermission(Model.Users.User user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
ScenarioPermission permission = ScenarioPermission.NewPermission(this, user);
if (!this.m_Permissions.Contains(permission))
{
this.m_Permissions.Add(permission);
}
}
public void RemovePermission(Model.Users.User user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
ScenarioPermission permission = this.m_Permissions.FirstOrDefault(p => p.UserID == user.Id);
if (permission != null)
{
this.m_Permissions.Remove(permission);
}
}
protected override void DataPortal_Update()
{
using (var ctx = DbContextManager<DatabaseContext>.GetManager())
{
var context = ctx.DbContext;
var scenarioId = ReadProperty(m_IdProperty);
var scenario = context.Scenarios.FirstOrDefault(s => s.Id == scenarioId);
if (scenario != null)
{
scenario.Name = ReadProperty(m_NameProperty);
//Some codes....
context.SaveChanges();
}
FieldManager.UpdateChildren(this);
}
}
protected override void DataPortal_DeleteSelf()
{
DataPortal_Delete(ReadProperty(m_IdProperty));
}
private void DataPortal_Delete(Guid id)
{
using (var contextManager = DbContextManager<DatabaseContext>.GetManager())
{
var context = contextManager.DbContext;
var scenario = context.Scenarios.FirstOrDefault(s => s.Id == id);
if (scenario != null)
{
context.Scenarios.Remove(scenario);
context.SaveChanges();
}
}
Dispatcher.CurrentDispatcher.Invoke(new Action(() => ScenarioList.Delete(id)));
}
}
public class ScenarioPermissions : BusinessListBase<ScenarioPermissions, ScenarioPermission>
{
public static ScenarioPermissions NewPermissions()
{
return DataPortal.Create<ScenarioPermissions>();
}
public static ScenarioPermissions GetUsersByScenario(Scenario scenario)
{
return DataPortal.FetchChild<ScenarioPermissions>(scenario);
}
private void Child_Fetch(Scenario obj)
{
using (var ctx = DbContextManager<DatabaseContext>.GetManager())
{
var context = ctx.DbContext;
var scenario = context.Scenarios.Where(s => s.Id == obj.Id).FirstOrDefault();
if (scenario != null)
{
foreach (var item in scenario.Users)
{
this.Add(ScenarioPermission.NewPermission(scenario.Id, item.Id));
}
}
}
}
}
public class ScenarioPermission : BusinessBase<ScenarioPermission>
{
private static readonly PropertyInfo<Guid> m_ScenarioID = RegisterProperty<Guid>(p => p.ScenarioID);
public Guid ScenarioID
{
get { return GetProperty(m_ScenarioID); }
private set { SetProperty(m_ScenarioID, value); }
}
private static readonly PropertyInfo<int> m_UserID = RegisterProperty<int>(p => p.UserID);
public int UserID
{
get { return GetProperty(m_UserID); }
private set { SetProperty(m_UserID, value); }
}
public static ScenarioPermission NewPermission(Scenario scenario, Model.Users.User user)
{
return NewPermission(scenario.Id, user.Id);
}
public static ScenarioPermission NewPermission(Guid scenarioID, int userID)
{
var newObj = DataPortal.CreateChild<ScenarioPermission>();
newObj.ScenarioID = scenarioID;
newObj.UserID = userID;
return newObj;
}
private ScenarioPermission() { /* Used for Factory Methods */}
private void Child_Insert(Scenario scenario)
{
DataPortal_Insert();
}
private void Child_DeleteSelf(Scenario scenario)
{
DataPortal_DeleteSelf();
}
private void Child_DeleteSelf()
{
DataPortal_DeleteSelf();
}
protected override void DataPortal_Insert()
{
using (var ctx = DbContextManager<DatabaseContext>.GetManager())
{
var context = ctx.DbContext;
var scenario = context.Scenarios.FirstOrDefault(s => s.Id == ScenarioID);
var user = context.Users.FirstOrDefault(u => u.Id == UserID);
if (scenario != null && user != null)
{
scenario.Users.Add(user);
context.SaveChanges();
}
}
}
protected override void DataPortal_DeleteSelf()
{
using (var ctx = DbContextManager<DatabaseContext>.GetManager())
{
var context = ctx.DbContext;
var scenario = context.Scenarios.FirstOrDefault(s => s.Id == ScenarioID);
var user = context.Users.FirstOrDefault(u => u.Id == UserID);
if (scenario != null && user != null)
{
if (scenario.Users.Contains(user))
{
scenario.Users.Remove(user);
context.SaveChanges();
}
}
}
}
public override bool Equals(object obj)
{
// If parameter is null return false.
if (obj == null)
{
return false;
}
// If parameter cannot be cast to ScenarioPermission return false.
ScenarioPermission p = obj as ScenarioPermission;
if ((System.Object)p == null)
{
return false;
}
// Return true if the fields match:
return (this.ScenarioID == p.ScenarioID) && (this.UserID == p.UserID);
}
}
我刚刚找到解决方案:当我获取场景权限的子节点时,我创建了子节点而不是获取现有的…因此,当我想要删除权限时,CSLA将现有对象视为NewObject,因此它不需要Delete();-)
更正:
public class ScenarioPermissions : BusinessListBase<ScenarioPermissions, ScenarioPermission>
{
public static ScenarioPermissions NewPermissions()
{
return DataPortal.Create<ScenarioPermissions>();
}
public static ScenarioPermissions GetUsersByScenario(Scenario scenario)
{
return DataPortal.FetchChild<ScenarioPermissions>(scenario);
}
private void Child_Fetch(Scenario obj)
{
RaiseListChangedEvents = false;
using (var ctx = DbContextManager<DatabaseContext>.GetManager())
{
var context = ctx.DbContext;
var scenario = context.Scenarios.Where(s => s.Id == obj.Id).FirstOrDefault();
if (scenario != null)
{
foreach (var item in scenario.Users)
{
this.Add(ScenarioPermission.GetPermission(scenario.Id, item.Id));
}
}
}
RaiseListChangedEvents = true;
}
}
public class ScenarioPermission : BusinessBase<ScenarioPermission>
{
public static ScenarioPermission GetPermission(Guid scenarioID, int userID)
{
return DataPortal.FetchChild<ScenarioPermission>(scenarioID, userID);
}
private void Child_Fetch(Guid scenarioID, int userID)
{
LoadProperty(m_ScenarioID, scenarioID);
LoadProperty(m_UserID, userID);
}
}