简体   繁体   English

加强不同对象之间的数据完整性

[英]Enforce data integrity between different object

I am learning OOP through developing a C# music management software. 我正在通过开发C#音乐管理软件来学习OOP。 So far, I have laid out the interaction of different classes and object as shown on this class diagram http://img811.imageshack.us/img811/7624/classdiagramh.png 到目前为止,我已经安排了不同类和对象的交互,如该类图所示: http://img811.imageshack.us/img811/7624/classdiagramh.png

I'm however very confused on making my design solid as I am finding a myriad of possibility of how things can be done but I don't want to end up with a bad design. 但是,我对使设计变得扎实感到非常困惑,因为我发现如何完成事情的可能性无数,但我不想以糟糕的设计告终。 The main problem I'm having is to make sure that I enforce data integrity between different object (To my understanding, I am supposing that in OOP, like in database design, there is a way one can provide an accuracy, consistency, and reliability of data store in different object as it is done in database design (For example the use of foreign key constraint). For example, in my application, I have Artist, Song, and Album objects that should interact together. I do not want for example to have a song associated with the wrong album by mistake. I have been told that I should do something like: 我遇到的主要问题是确保在不同对象之间强制执行数据完整性(据我所知,我认为在OOP中,就像在数据库设计中一样,有一种方法可以提供准确性,一致性和可靠性。数据库设计中完成的操作将数据存储在不同的对象中(例如,使用外键约束),例如,在我的应用程序中,我有Artist,Song和Album对象应该一起交互。例如,某首歌与错误的专辑相关联,我被告知我应该执行以下操作:

class Album
{
  public String Name{ get; set;}

  List<Track> tracks;

  public void AddTrack(Track t)
  {
    tracks.Add(t);
    t.Album = this;
  }
} 
class Track
{
  public Album Album{ get; set;}
  public String AlbumName
  { 
    get{ return this.Album.Name}
  }

  public String ArtistName{ get; set}
}

However this is not working for me. 但是,这对我不起作用。 Can someone suggest how I can actually make this work. 有人可以建议我如何实际进行这项工作。 Specifically, how I can add a new song with a title, an album and artist and make sure that If for some strange reason the name of the album is changed, all of the tracks will still return the correct album name, since they're getting the data from the album itself? 具体来说,我如何添加带有标题,专辑和歌手的新歌曲,并确保如果由于某种奇怪的原因专辑名称被更改,所有曲目仍将返回正确的专辑名称,因为它们是从专辑本身获取数据?

Also, can someone suggest a better design. 另外,有人可以建议一个更好的设计。 Should I be using inheritance or any polymorphism to make my design robust? 我应该使用继承还是任何多态性来使设计更坚固?

As in DB design you have to make your choices based on your business requirements. 与数据库设计一样,您必须根据业务需求做出选择。 Means: You have to ask yourself questions of possible constellations - are all the songs on a CD from the same artist? 意思是:您必须问自己一些可能的星座问题-CD中的所有歌曲是否都来自同一位艺术家? No -> artist field on track, yes -> artist field on album - do you have to be able to get the album when you know a track, or just all the tracks from a specific album - ... 否->曲目上的艺术家字段,是->专辑上的艺术家字段-您是否必须能够在知道曲目或仅来自特定专辑的所有曲目时获取专辑-...

What you are doing is correct in the way that you reference from the Track object the name of the referenced Album. 从Track对象引用所引用专辑的名称的方式是正确的。 What is to mention: they do return the albums name, but just the name the album has in the moment of the GET. 值得一提的是:它们确实会返回相册名称,但仅返回GET时刻相册名称。 So if you assigned the AlbumName property to a visual control or whatever it will not update automatical when you change the Albums name. 因此,如果您将AlbumName属性分配给可视控件,或者更改相册名称时它不会自动更新。

if thats not what you want to hear ask more specific. 如果那不是您想听到的,请更具体地询问。

Actually you're not doing associations. 实际上,您不是在进行关联。

In object-oriented programming, you associate objects instead of "keys". 在面向对象的编程中,您关联对象而不是“键”。 There's no concept of "foreign key". 没有“外键”的概念。

If an object B has a parent A, and A has an unique identifier , you won't associate B's parent by setting this unique identifier but the object itself: 如果对象B有一个父对象A,并且A有一个唯一标识符 ,则您将不会通过设置该唯一标识符来关联B的父对象,而是设置对象本身:

B has a parent A : B有一个父A

public class A { public Guid ID { get; set; } }
public class B { public A Parent { get; set; } }

A someA = new A();
B someB = new B();
someB.Parent = someA;

Working this way, as "instance of A" may be associated with many other objects, changing the same object from any reference, will result in modifying that object . 以这种方式工作,因为“ A的实例”可能与许多其他对象相关联, 因此从任何引用中更改同一对象都会导致修改该对象

How to achieve integrity constraints? 如何实现完整性约束? There're some design patterns, but one of most common one is Specification . 有一些设计模式,但是最常见的一种是Specification

That's creating a common interface "ISpecification" which has a method that may be called "Check", which accepts an argument - the object to check for an specification -. 这样就创建了一个通用接口“ ISpecification”,该接口具有一种可以称为“ Check”的方法,该方法接受一个参数( 用于检查规范的对象) In implementations, this method will check that some object conforms some specification. 在实现中,此方法将检查某些对象是否符合某些规范。

public interface ISpecification { bool Check(object some); }

public class Person { public string Name { get; set; } }

public class PersonSpecification : ISpecification
{
     public bool Check(object some) 
     {
          // Checks that some person won't be null and his/her name isn't null or empty
          return some != null && !string.IsNullOrEmpty(((Person)some).Name);
     }
} 

This is a very simple and generic sample of how to implement business rules and/or constraints. 这是关于如何实现业务规则和/或约束的非常简单且通用的示例。

There're many frameworks which may help you in this area: 有许多框架可以在此领域为您提供帮助:

In some projects I've integrated NHibernate Validator, and others, a custom solution based on Specification pattern, but in a more complex and flexible way than the sample. 在某些项目中,我集成了NHibernate Validator,而在其他项目中,则集成了基于“ 规范”模式的自定义解决方案,但比示例更复杂,更灵活。

About Specification pattern: 关于规格模式:

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

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