本文介绍了检索子对象,以及何时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好,很抱歉,如果这篇文章真的很长,我只想做 我确定我完全解释了我想要做什么。我是OOP的新手和.net 2.0,所以如果这很明显,我很抱歉。 我写了一个数据访问层,它继续我的数据库中每个对象的单独的 服务区。 CompanyDataService(包含CRUD方法) ContactDataService(包含CRUD方法) Q1,当我检索每个对象(公司,联系人等)的数据时我会返回一个数据集,我应该返回一个数据集吗? Q2,在联系表中我有一个名为CompanyID的外键,因为 联系人链接到公司。如果我要使用我的ContactDataService中的检索功能从 数据库中检索联系人, 我将如何获得联系人链接到的CompanyName?请 i拨打公司检索方法,在我的联系方式内检索 方法?? Q3,如果我创建公司和联系人的业务对象, 我应该将联系对象写为包含公司对象,或者 只是一个companyID字段??? ie 联系 ID 名称 CompanyID 或 联系 ID 名称 公司(我可以然后输入Contact.Company.ID来访问公司ID 注意,公司是单独的对象,不必与任何联系人链接 数据库。 我会很乐意帮助,这个问题一直困扰着我 好​​几天。如果你想了解更多,请告诉我。 解决方案 如果需要引用多个相关的DataTable,则返回一个DataSet 如果只需要处理一组数据,则返回一个DataTable (在其父DataSet中可能有也可能没有关联) 如果您只代表一个实体(在其父数据集中可能有也可能没有关联),则返回DataRow 您可能想要创建一个强类型的DataSet,其中至少包含一个Company表和一个Contact表,其中定义了 适当的关联。 在这种情况下,它将返回业务对象或每种方法的业务对象数组,而不是返回底层的 数据。我更喜欢使用该服务来检索数据并拥有业务对象,例如联系,必要时拨打 ContactDataService。通过从业务层分离数据访问层,您会发现应用程序更好地扩展,并且:更容易维护,修改,调试(并理解IMO)。 Q2,在联系表中我有一个名为CompanyID的外键,因为 联系人链接到公司。如果我要使用我的ContactDataService中的检索功能从 数据库中检索联系人, 我将如何获得联系人链接到的CompanyName?请 我打电话给公司检索方法,在我的联系方式中检索 方法?? (我在昨天对这个新闻组中你的相关帖子的回复中解决了这个问题。如果你对我有任何疑问 回复我很乐意为你回答。只需回复另一个帖子。) 如果你总是需要公司名称和联系方式,那么你应该将一个CompanyName字段添加到您的 强类型,联系DataTable和您的Contact业务对象。在填充并返回 Contact对象的ContactDataServer方法中,例如GetContactByID,您有几个选项来查询数据库。 IMO,你最好的选择是创建一个 存储过程,该过程从一个View中选择(SQL Server允许你创建数据视图),它聚合你所有的数据 需要每个联系人并按指定的ContactID对其进行过滤。由于View,结果集将包含CompanyName 字段(通过View的SELECT语句中的JOIN完成)。 如果你有时只需要公司名称,那么你可以有两个数据视图,两个存储过程(或者一个存储过程,带有BIT参数的过程,例如SummaryDataOnly),和ContactDataServer对象上的两个方法,例如, " GetContactByID"和GetContactSummaryByID,后者仅返回数据库中Contact表的信息。我实际上甚至不建议这样做,除非有某种原因需要它,而是建议你使用 总是通过联系信息检索CompanyName您甚至需要在申请中使用一次。不要检索CompanyName 如果流程证明对性能有重大影响,或者创建某种安全风险,那么每次联系信息。 风险。 > (我相信我昨天在这个新闻组的相关帖子中已经回答了这个问题。再次,如果你有问题 关于我的回复只是在那个帖子上发表回复,我很乐意回答他们。) < snip> HTH - Dave Sexton Hi everyone, sorry if this post gets really long, i just wanna makesure i fully explain what i am trying to do. I am new to OOP and .net2.0, so if this is obvious, i am sorry.I have wrote a data access layer, which contists of separateservicebases for each object within my database.CompanyDataService (contains CRUD methods)ContactDataService (contains CRUD methods)Q1, when i retrieve data for each object (company, contacts etc) ireturn a dataset, should i return a datatable??Q2, in the contact table i have a foreign key called CompanyID, ascontacts are linked to a company. If i was to retrieve a contact fromthe database using my retrieve function within my ContactDataService,how would i obtain the CompanyName that the contact is linked to?? Doi put a call to the company retrieve method, within my contact retrievemethod??Q3, if i create the business object for both company and contacts,should i write the contact object as containing a company object, orjust a companyID field???i.e.ContactIDNameCompanyIDorContactIDNameCompany (i can then access Company ID by typing Contact.Company.ID)Note, that companies are separate objects and do not have to be linkedto any contacts in the database.I would be greatful for a lil help, this question has been bugging mefor days. If you want to know more, please let me know. 解决方案Return a DataSet if you need to reference multiple, related DataTablesReturn a DataTable if you only need to work with one set of data (which may or may not have associations in its parent DataSet)Return a DataRow if you are only representing one entity (which may or may not have associations in its parent DataSet)You''ll probably want to create a strong-typed DataSet that contains at least a Company table and a Contact table with theappropriate association defined.The ContactDataService, for instance, could either return the data to a business object or it can act as a business object factory,in which case it would return a business object or an array of business objects for each method instead of returning the underlyingdata. I prefer using the service to retrieve data and have a business object, e.g. Contact, make the calls to theContactDataService when necessary. By seperating the data access layer from the business layer you''ll find that the applicationscales better, and is: easier to maintain, modify, debug (and understand IMO).(I addressed this somewhat in a response to a related post of yours in this newsgroup yesterday. If you have any questions about myresponse I''d be glad to answer them for you. Just post a reply to the other thread.)If you always require the company name along with the contact information then you should add a CompanyName field to yourstrong-typed, Contact DataTable and to your Contact business object. In the ContactDataServer method that fills and returns aContact object, such as "GetContactByID", you have several options to query the database. IMO, your best option is to create astored procedure that selects from a View (SQL Server allows you to create Views of data) that aggregates all of the data that youneed for each contact and filters it by the specified ContactID. The result set, because of the View, will include the CompanyNamefield (accomplished through a JOIN in the View''s SELECT statement).If you only need the company name some of the time, then you could have two Views of the data, two stored procedures (or one storedprocedure with a BIT parameter, such as "SummaryDataOnly"), and two methods on your ContactDataServer object such as,"GetContactByID" and "GetContactSummaryByID", the latter only returning information from the Contact table in the database. Iactually wouldn''t even recommend doing this unless there is some reason that it''s required and would recommend instead that youalways retrieve CompanyName with contact information if you need it even once in your application. Don''t retrieve CompanyName withContact information every time if the process proves to have a substantial impact on performance, or creates some sort of securityrisk.(I believe that I answered this already in a related thread of yours in this newsgroup, yesterday. Again, if you have an questionsabout my response just post a reply on that thread and I''ll be glad to answer them.)<snip>HTH--Dave Sexton 这篇关于检索子对象,以及何时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 07:56