1

我最近在我的另一个问题上发布了以下代码。请原谅部分属性,这是我Wanted要做的,但不能......

public partial class Agency : PropertyValidator, IAgency
{
    private string _name;

    public partial string Name 
    {
        get { return _name; }
        set
        {
            // PropertyValidator stuff
            if (string.IsNullOrEmpty(value))
                AddErrorToProperty("Agency name must contain a reasonable alphanumeric value.");
            if (string.IsNullOrWhiteSpace(value))   
                AddErrorToProperty("Agency name cannot contain all spaces.");

            SetPropertyIfValid(ref _name, value);
        }
    }
}

public partial class Agency : IPersitentEntity, IAgency
{       
    [Key]    // NOTE these Annotations are because of Entity Framework...nice separation! 
    public int ID { get; set; } // From IPersitentEntity

    [Required]
    [MinLength(3), MaxLength(50)]
    public partial string Name { get; set; } // IAgency NOTE this is not valid, but the 
                                             // separation is amazing!

    // From IPersitentEntity provide CRUD support
    public void Create() { throw new NotImplementedException(); }
    public void Retrieve(int id) { throw new NotImplementedException(); }
    public void Update(int id) { throw new NotImplementedException(); }
    public void Delete(int id) { throw new NotImplementedException(); }
}    

...在其中,我有一条评论没有回复 stackoverflow 说...

You might want to read up on persistence ignorance and why making each of your entities inherit from IPersitentEntity is a "bad thing".


以前没听说过Persistence Ignorance,不得不查。原来我知道这个概念是什么,而不是术语。但是,我有点困惑为什么我在做的事情很糟糕。

4

1 回答 1

1

主要问题是你的班级现在有多个变化轴。如果您的业务模型发生变化,它可能会发生变化,并且也会因数据库层的变化而发生变化。这打破了“单一职责原则”

查找 SOLID 原则,这里是 Robert Martin 和 Scott Hanselmen 的播客,其中描述了它们

http://www.hanselman.com/blog/HanselminutesPodcast145SOLIDPrinciplesWithUncleBobRobertCMartin.aspx

于 2013-08-22T08:20:17.823 回答