Skip to main content

Microsoft Silverlight

Answered Question Why is the generated xxxDomainContext sealed?RSS Feed

(0)

ericsson007
ericsson007

Member

Member

43 points

36 Posts

Why is the generated xxxDomainContext sealed?

The xxxDomainContext class generated into the .Web.g.cs source file is sealed. What is the reason behind this? Can this be changed?

MarkMonster
MarkMonster

Contributor

Contributor

5220 points

1,046 Posts

Re: Why is the generated xxxDomainContext sealed?

I don't know the reason behind the class being sealed. But what I understood: there will be code-gen-hookpoints. This might help you.

From the roadmap:

 

Mark Monster - MCPD Enterprise
http://mark.mymonster.nl
Silverlight and Expression Insiders UG

Dont forget to click "Mark as Answer" on the post that helped you.

ColinBlair
ColinBlair

Contributor

Contributor

6601 points

1,298 Posts

Re: Why is the generated xxxDomainContext sealed?

I don't know the actual reason Microsoft sealed it, but generally speaking inheriting from generated code probably isn't best practice anyway. Using the codegen hooks that Mark pointed you can remove the sealed  but I am not sure if it would be a good idea. Using the partial class to add methods to the DomainContext is much safer

At one point (January if I remember correctly) I was looking at the concept of inheriting the DomainContext and turning it into a ViewModel which required getting around the sealed. After Reflectoring into the plumbing of DomainContext and really getting to know how it works behind the scenes, I came to the conclusion that DomainContext is simply too complex under the covers to play around with that much, it is better to have a seperate ViewModel object which has a reference to the DomainContext instance. Generally speaking, I am looking forward to using the CodeGen hooks to do some interesting things with the entities themselves but I think that DomainContext modifications should be left to the partial class.

-Colin Blair

http://www.RiaServicesBlog.net : The Elephant Guide to RIA Services
SLColinBlair on Twitter

waldred
waldred

Member

Member

702 points

113 Posts

Answered Question

Re: Why is the generated xxxDomainContext sealed?

ericsson007:
The xxxDomainContext class generated into the .Web.g.cs source file is sealed. What is the reason behind this?
 

In short, the entity and context types are sealed to reduce complexity.  Unsealing these types would introduce a range of additional extensibility scenarios that need to be handled and tested.  (E.g., someone derives from a DomainContext and starts partying with internal state in ways we didn't anticipate or someone derives from an entity and breaks our serialization scheme.)  Sealing these types now allows us to carefully consider such scenarios and leaves open the option to unseal at some point in the future without breaking existing applications.

ericsson007:
Can this be changed?
 

If you absolutely must, you can use a CodeProcessor to unseal the DomainContext type.  CodeProcessors are an advanced topic but you get complete control over the generated code output.  I would recommend looking into this option if you don't mind the wilderness.  Smile 

ericsson007
ericsson007

Member

Member

43 points

36 Posts

Re: Re: Why is the generated xxxDomainContext sealed?

Thanks for all the feedback!
The main reason for this question was that I was working on subclassing the DomainDataSource to for example implement generic error handling, and still be able to easily use it in the XAML. That worked out to be no problem, and I have the same need for the xxxDomainContext.

The CodeProcessor seems to some work, which I don't have time for right now, but soon :) Thanks again for all the comments!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities