POCO的,行为和Peristance Igorance [英] POCO's, behavior and Peristance Igorance

查看:201
本文介绍了POCO的,行为和Peristance Igorance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我已阅读POCO类应该是持续性无知,不应该包含对库引用。

Q1。鉴于上述情况,我将如何填充QuestionBlocks收藏?我已阅读,POCO的应该包含行为,使你不贫血模型结束,所以我一心应该怎样做,没有持久样的困惑。如果是这样的话,那么你会放什么样的行为在POCO?

例如:

 公共类调查
    {
        公众诠释SurveyId {搞定;组; }
        公共字符串名称{搞定;组; }
        公众诠释BrandId {搞定;组; }
        公众的DateTime创建{搞定;组; }
        公开名单< SurveyQuestionBlock> QuestionBlocks {搞定;组; }        [ResultColumn]
        公共字符串名称{;组; }
        ///<总结>
        ///构造
        ///< /总结>
        公众调查()
        {
            创建= DateTime.Now;
            QuestionBlocks =新的List< SurveyQuestionBlock>();
        }
    }


解决方案

  

鉴于上述情况,我将如何填充QuestionBlocks集合?


从数据库中读取时,持久性基础设施应该填充QuestionBlocks集合 - 重建。重建不应该调用的行为,它应该只设置在POCO相应的字段。这是库的责任。信息库通常是从一个应用程序服务,设置了舞台,调用实体的行为参照。


  

如果是这样的话,那么你会把什么样的行为在POCO?


即保证实体的完整性 -

在POCO实体行为应在更改实体本身,以及维持不变有关。在你的榜样,在POCO最简单的一种行为应该是在调查中加入了新的问题块的收集方法。理想情况下,你会做出许多对调查实体的属性只读:

 公共类调查
    {
        公众诠释SurveyId {搞定;私人集; }
        公共字符串名称{搞定;私人集; }
        公众诠释BrandId {搞定;私人集; }
        公众的DateTime创建{搞定;私人集; }
        公众的IList< SurveyQuestionBlock> QuestionBlocks {搞定;私人集; }
        公共字符串名称{;私人集; }        公共无效AddQuestionBlock(字符串questionBlockInfo)
        {
          this.QuestionBlocks.Add(新SurveyQuestionBlock(...));
        }        公众调查()
        {
            创建= DateTime.Now;
            QuestionBlocks =新的List< SurveyQuestionBlock>();
        }
    }

持久层应该能够通过反射来设置只读属性的值。你可以走得更远了一步,就只是暴露问题块集合作为一个只读集合,以确保它只能从实体本身内进行修改。

From what I have read POCO classes should be persistence ignorant and should not contain references to repositories.

Q1. Given the above, how would I populate the QuestionBlocks collection? I have read that POCO's should contain behavior so you don't end of with an anemic model, so I'm kind of confused as how one is supposed to do that without persistence. If that's the case then what kind of behavior would you put in a POCO?

Ex:

 public class Survey
    {
        public int SurveyId { get; set; }
        public string Title { get; set; }
        public int BrandId { get; set; }
        public DateTime Created { get; set; }
        public List<SurveyQuestionBlock> QuestionBlocks { get; set; }

        [ResultColumn]
        public string Name { get; set; }


        /// <summary>
        /// Constructor
        /// </summary>
        public Survey()
        {
            Created = DateTime.Now;
            QuestionBlocks = new List<SurveyQuestionBlock>();
        }
    }

解决方案

Given the above, how would I populate the QuestionBlocks collection?

When reading from a database, the persistence infrastructure should populate the QuestionBlocks collection - reconstitution. Reconstruction should not invoke behavior, it should only set appropriate fields on the POCO. This is the responsibility of the repository. A repository is typically referenced from an application service, which sets up the stage for invoking entity behavior.

If that's the case then what kind of behavior would you put in a POCO?

The behavior in the POCO entity should be concerned with making changes to the entity itself as well as maintaining invariants - ie ensuring the integrity of the entity. In your example, the simplest kind of behavior on the POCO should be method for adding a new question block to the collection on the survey. Ideally, you would make many of the properties on the survey entity read-only:

    public class Survey
    {
        public int SurveyId { get; private set; }
        public string Title { get; private set; }
        public int BrandId { get; private set; }
        public DateTime Created { get; private set; }
        public IList<SurveyQuestionBlock> QuestionBlocks { get; private set; }
        public string Name { get; private set; }

        public void AddQuestionBlock(string questionBlockInfo)
        { 
          this.QuestionBlocks.Add(new SurveyQuestionBlock(...));
        }

        public Survey()
        {
            Created = DateTime.Now;
            QuestionBlocks = new List<SurveyQuestionBlock>();
        }
    }

The persistence layer should be able to set the values of the read-only properties via reflection. You can go a step further and only expose the question blocks collection as a read-only collection to ensure that it can only be modified from within the entity itself.

这篇关于POCO的,行为和Peristance Igorance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆