我在開發的軟件,在這裏我有幾個實體,如:分層架構問題
public class Workspace
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual List<Playground> Playground { get; set; }
public virtual List<Workspace> Children { get; set; }
public virtual List<Member> Members { get; set; }
public virtual Workspace Parent { get; set; }
}
public class Playground
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual List<Service> Services { get; set; }
public virtual Workspace Workspace { get; set; }
}
public class Service
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual Playground Playground { get; set; }
}
這些都是我EF4 POCO對象。我正在使用存儲庫模式和以下接口:
public interface IRepository<T>
{
void Add(T entity);
void Delete(T entity);
IEnumerable<T> Get(Expression<Func<T, bool>> expression);
IEnumerable<T> Get();
void Attach(T entity);
int Save();
}
存儲庫具有內部ObjectContext。我有一個UnitOfWork,它包含我的存儲庫的實例,並負責保存對它們所做的更改。
我到底做得對嗎?
我實現業務邏輯層是這樣的:
public class DomainWorkspaceService : DomainServiceBase
{
public DomainWorkspaceService(Workspace workspace)
: base(UnitOfWorkFactory.GetInstance())
{
}
public void Create(Workspace workspace)
{
UoW.GetRepository<Workspace>().Add(workspace);
}
public void Delete(Workspace workspace)
{
var pservice = new DomainPlaygroundService();
foreach (var playground in workspace.Playground)
pservice.Delete(playground);
foreach (var child in workspace.Children)
Delete(child);
}
}
現在,我不知道我在正確的方向我要去。我的波蘇斯是(會)負責審定,將使我做這樣的事情
SomeWorkspace.Children.Add(new Workspace {...});
由於這些對象與上下文相關的,當我救他們將更改到收藏也被保存在數據庫中?
此外,我希望我的遊樂場不能在沒有遊樂場的情況下沒有工作區和服務的情況下創建。我應該在哪裏創建和刪除它們?
謝謝。
如果你不想強迫操場用工作區構建 - 添加需要它們的構造函數。服務也是一樣。 – 2010-08-24 18:57:04
是的。謝謝。 – codegarten 2010-08-24 19:08:14