分层架构问题

发布于 2024-09-15 16:07:43 字数 2340 浏览 3 评论 0原文

我正在开发一个软件,其中有一些实体,例如:

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。我有一个工作单元,其中包含我的存储库的实例,并负责保存对它们所做的更改。

到目前为止我做得对吗?

我正在实现这样的业务逻辑层:

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

    }

现在我不确定我是否朝着正确的方向前进。我的 POCO(将)负责验证,并使我能够执行类似的操作

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

,因为这些对象与上下文相关联,当我保存它们时,对集合的更改也会保存在数据库中吗?

另外,我希望如果没有工作空间和没有游乐场的服务,就无法创建我的游乐场。我应该在哪里创建和删除它们?

谢谢。

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. 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. I have a UnitOfWork that contains instances of my repositories and is responsible to save the changes made to them.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

︶ ̄淡然 2024-09-22 16:07:43

到目前为止,一切都很好。

您可能希望将 Save 方法从存储库移到工作单元上。在实体框架中,您必须立即保存上下文中的所有更改;您不能按类型执行此操作。将其放在存储库中意味着只会保存对存储库的更改,这可能不正确。

您可能希望将从工作区到游乐场的级联实现为数据库级联(实体框架将识别并支持),而不是手动编码它们。

是的,保存对上下文的更改将保存所有跟踪的更改,包括相关对象。

关于添加,如果您不公开 ObjectContext,那么任何人都能够添加 Playground 的唯一方法是通过与您公开的对象的关系。

So far, so good.

You probably want to move your Save method off of the Repository and onto the unit of work. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文