开发者

Silverlight LINQtoSQL: one big dataclass, or several small ones?

I'm new to Silverlight, but being dumped right into the fray - good way to learn I suppose :o)

Anyway, the webapp I'm working on has a relatively complex database structure that represents various object types that are linked to each other, and I was wondering 2 things:

1- What is the recommended approach when it comes to dataclasses? Have just one big dataclass, or try and separate it into several smaller dataclasses, keeping in mind they will need to reference each other? 2- If the recommended approach is to have several dataclasses, how do you define the inter-dataclasses references?

I'm asking because I did a small test. In my DB (simplified here, real model is more complex but that's not important), I have a table "Orders" and a table "Parameters". "Orders" has a foreign key on "Parameters". What I did is create 2 dataclasses. The first one, ParamClass, were I dropped the "Parameters" table only, so I can have a nice "parameter" class. I t开发者_如何学Chen created a simple service to add basic SELECT and INSERT functionality.

The second one, OrdersClass, where I dropped both tables, so that the relation between the tables would automatically create a "EntityRef<parameter>" variable inside the "order" class. I then removed the "parameters" class that was automatically created in the OrdersClass dataclass, since the class has already been declared in the ParamClass dataclass. Again I created a small service to test it.

So far so good, it builds happily. The problem is that when I try to handle things on the application code, I added service references for both dataclasses, but it is not happy doing something like:

OrdersServiceReference.order myOrder = new OrdersServiceReference.order();
myOrder.parameter = new ParamServiceReference.parameter();  //<-PROBLEM IS HERE

It comlpains that it cannot implicitly convert from type 'MytestDC.ParamServiceReference.parameter' to 'MytestDC.OrdersServiceReference.parameter'

Do I somehow need to declare some sort of reference to ParamClass from OrdersClass, or how do I "convert" one to the other? Is this even a recommended and efficient way of doing this? Since it's a team-project, I initially wanted to separate the dataclasses so that they (and their services) can be easily checked out by one member without checking out the whole entire dataclass.

Any help appreciated!

PS: using Silverlight 4, in case that's important


Based on the widely accepted Single Responsability Principle (SRP), a class should always be responsible for one task, and one task only.

That pretty much invalidates your "one big dataclass" approach.

I would always recommend smaller, more manageable bits that can be combined, instead of one humonguous class that does everything (except brew coffee for you).

Resources for the SRP:

  • Wikipedia on SRP
  • OODesign: Single Responsibility Principle
  • ObjectMentor: list of articles on good app design - which has a few links to PDF documents, like this one on SRP written by Robert C. Martin - the "guru" on proper OO design


OK, some more research let me to this: it is not simple to separate classes from a relational model using LINQtoSQL. I ended up switching to an Entity Framework approach, which itself doesn't deal with it gracefully (see here and there, for example), but at least it solved another major problem I had with LINQtoSQL. There are other ORMs out there that are apparently much more capable at this (NHibernate comes up often in recommendations), unfortunately, I don't have time to investigate them now, being under such a tight deadline.

As for the referencing, it was quite simple, change the line to:

myOrder.parameter = new OrderServiceReference.parameter();

even though I removed the declaration from that dataclass.

Hope this helps someone!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜