ObjectContext public in debug mode, internal in release mode
Asked Answered
C

2

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.

Comptom answered 27/2, 2012 at 8:39 Comment(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.Marcenemarcescent
C
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
Ciracirca answered 27/2, 2012 at 8:48 Comment(6)
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.Comptom
@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.Ciracirca
I'm not too familiar with that (yet). Can you refer to some source?Comptom
@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.)Ciracirca
Even better, and it works! Maybe you can add this to your answer and I'll be happy to mark it as accepted.Comptom
@GertArnold Glad to read that, I've included it in the answer.Ciracirca
M
0

Preprocessor directive?

# if(DEBUG)
        public ObjectContext _context;
# else
        internal ObjectContext _context;
#endif
Marcenemarcescent answered 27/2, 2012 at 8:47 Comment(2)
No, I mean the class itself, not each instance I'm using.Comptom
The same may be applied to class, as @hvd stated in his answer.Marcenemarcescent

© 2022 - 2024 — McMap. All rights reserved.