简体   繁体   English

分层架构问题

[英]Layered Architecture Question

I am developing an piece of software where I have a few entities such as: 我正在开发一款软件,其中包含一些实体,例如:

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; }
    }

These are my EF4 POCO objects. 这些是我的EF4 POCO对象。 I am using the repository pattern and the following interface: 我正在使用存储库模式和以下界面:

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();
}

The repositories have an internal ObjectContext. 存储库具有内部ObjectContext。 I have a UnitOfWork that contains instances of my repositories and is responsible to save the changes made to them. 我有一个UnitOfWork,其中包含我的存储库实例,并负责保存对其所做的更改。

Am i doing it right so far? 到目前为止,我做得对吗?

I am implementing a Business Logic Layer like this: 我正在实现这样的业务逻辑层:

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);
        }

    }

Now i'm not sure i am going in the right direction. 现在我不确定我会朝正确的方向前进。 My POCOs are (will be) responsible for validation and will enable me to do something like 我的POCO(将)负责验证,并使我能够做类似的事情

SomeWorkspace.Children.Add(new Workspace {...});

Since these objects are associated with a context, when i save them will the changes to the collections also be saved in the database? 由于这些对象与上下文相关联,因此当我保存它们时,对集合所做的更改是否也将保存在数据库中?

Also, I want that my Playgrounds can't be created without a Workspace and Services without a Playground. 另外,我希望没有工作区就不能创建我的操场,没有操场就不能创建服务。 Where should i create and delete them? 我应该在哪里创建和删除它们?

Thanks. 谢谢。

So far, so good. 到现在为止还挺好。

You probably want to move your Save method off of the Repository and onto the unit of work. 您可能希望将Save方法从存储库移至工作单元。 In the Entity Framework, you have to save all changes in the context at once; 在实体框架中,您必须立即将所有更改保存在上下文中; you cannot do it per-type. 您无法按类型进行操作。 Putting it on the Repository implies that only changes to the repository will be saved, which is probably not correct. 将其放在存储库上意味着仅将保存对存储库的更改,这可能是不正确的。

You might want to implement the cascade from workspace to playground as database cascades (which the Entity Framework will recognize and support) instead of manually coding them. 您可能希望将从工作空间到游乐场的级联实现为数据库级联(实体框架将识别并支持),而不是手动对其进行编码。

Yes, saving changes to the context will save all tracked changes, including related objects. 是的,将更改保存到上下文将保存所有跟踪的更改,包括相关对象。

Regarding adding, if you don't expose the ObjectContext, then the only way that anyone will be able to add a Playground is via relationships with the objects you do expose. 关于添加,如果您不公开ObjectContext,那么任何人都可以添加Playground的唯一方法是通过与您公开的对象的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM