1

Is there an easy way to make an ObjectContext public in debug mode and internal in release mode?

With Linqpad it is very convenient to connect to an ObjectContext for quick testing and querying, so for development purposes I want it to be public. But I don't want to think of the consequences when the same convenience is deployed to some smart customer.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • 1
    Smart customer can use Reflector, for example. Not that I approve having ObjectContext public in production - just that having it internal may not resolve your concern. – Sergey Kudriavtsev Feb 27 '12 at 08:42

2 Answers2

2

As mentioned in the comment, this may not be of any practical use, but:

#if DEBUG
public
#endif
class YourContext : ObjectContext
{
    ...
}

When dealing with a generated ObjectContext from a .edmx file, you'll need to customize how C# files are generated. The default is not customizable, but the designer has an option "Add Code Generation Item". If you use this, you'll get several options. I'm using "ADO.NET Self-Tracking Entity Generator", but the same way works for all of them. Choosing this adds two template files (Model.tt and Model.Context.tt) to your project, which you are free to modify as you see fit. For the modification you're asking about, you'll find <#=Accessibility.ForType(container)#> partial class in the Model.Context.tt file. You can update this so that it reads

#if DEBUG
<#=Accessibility.ForType(container)#>
#endif
partial class
  • Sorry, I should have mentioned that I refer to model-first (added the tag). So I'm dealing with a generated edmx file. I can not add a partial class because it cannot have a different access modifier. +1 anyway, because it would work in code-first. – Gert Arnold Feb 27 '12 at 08:56
  • @GertArnold You can use a T4 template to customize the generated class, and modify that template to include anything you want in the result, including preprocessor directives. –  Feb 27 '12 at 09:01
  • I'm not too familiar with that (yet). Can you refer to some source? – Gert Arnold Feb 27 '12 at 09:03
  • @GertArnold No source, but a quick explanation: in the EDMX designer, right-click, choose "Add Code Generation Item". You then get several choices, I'll use "ADO.NET Self-Tracking Entity Generator". In the newly created "Model.Context.tt", you'll find `<#=Accessibility.ForType(container)#> partial class`. You can put the `<#=Accessibility.ForType(container)#>` part between `#if DEBUG` and `#endif`. (I'm not sure if you need to install anything extra to get these templates.) –  Feb 27 '12 at 09:08
  • Even better, and it works! Maybe you can add this to your answer and I'll be happy to mark it as accepted. – Gert Arnold Feb 27 '12 at 09:24
  • @GertArnold Glad to read that, I've included it in the answer. –  Feb 27 '12 at 09:31
0

Preprocessor directive?

# if(DEBUG)
        public ObjectContext _context;
# else
        internal ObjectContext _context;
#endif
Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68