Accessing internal DbContext with reflection

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
-1
down vote

favorite












I have a DbContext like that.:



Domain.MyDbContext



public class CoiDbContext : IdentityDbContext<Usuario, Funcao, Guid>

public DbSet<OcorrenciaTalao> OcorrenciasTalao get; set;



Some reusable queries like that:



Extenions.OcorrenciasTalao



public static class OcorrenciasTalao

private static Func<CoiDbContext, int, Task<int>> _getProximoSequencial = EF.CompileAsyncQuery((CoiDbContext context, int ano) =>
context.OcorrenciasTalao.Where(x => x.BoAno == ano).Select(x => x.BoSequencial + 1).DefaultIfEmpty(1).Max());

public static async Task<int> GetProximoSequencial(this DbSet<OcorrenciaTalao> dbSet, int ano)

var context = dbSet.GetContext();
return await _getNextSequencial(context, ano);




Since I want to extend the DbSet<TEntity> indeed of the DbContext, I need to get the InternalDbContext in order to compile my reusable query.



Extenions.Common



public static class Common

private static Dictionary<Type, FieldInfo> _fields = new Dictionary<Type, FieldInfo>();
public static MyDbContext GetContext<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class

var type = typeof(TEntity);
if (!Common._fields.ContainsKey(type))
BindingFlags.Instance));

return Common._fields[type].GetValue(dbSet) as MyDbContext;




So, is a better way to get the InternalDbContext without rely on Reflection, since I'm accessing a private field of a third party package, I fear that can silently break my code at any time?



As you may have noticed, I'm using Extensions like a Repository Pattern, is there a good idea? how i can improve that?







share|improve this question

















  • 2




    Please post your real code. Psedocode is off-topic on Code Review.
    – t3chb0t
    Jul 17 at 6:26










  • @t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
    – Tobias Mesquita
    Jul 17 at 12:00






  • 2




    The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
    – Graipher
    Jul 17 at 12:51











  • Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
    – t3chb0t
    Jul 17 at 18:57











  • Entity Framework Core
    – Tobias Mesquita
    Jul 17 at 18:58
















up vote
-1
down vote

favorite












I have a DbContext like that.:



Domain.MyDbContext



public class CoiDbContext : IdentityDbContext<Usuario, Funcao, Guid>

public DbSet<OcorrenciaTalao> OcorrenciasTalao get; set;



Some reusable queries like that:



Extenions.OcorrenciasTalao



public static class OcorrenciasTalao

private static Func<CoiDbContext, int, Task<int>> _getProximoSequencial = EF.CompileAsyncQuery((CoiDbContext context, int ano) =>
context.OcorrenciasTalao.Where(x => x.BoAno == ano).Select(x => x.BoSequencial + 1).DefaultIfEmpty(1).Max());

public static async Task<int> GetProximoSequencial(this DbSet<OcorrenciaTalao> dbSet, int ano)

var context = dbSet.GetContext();
return await _getNextSequencial(context, ano);




Since I want to extend the DbSet<TEntity> indeed of the DbContext, I need to get the InternalDbContext in order to compile my reusable query.



Extenions.Common



public static class Common

private static Dictionary<Type, FieldInfo> _fields = new Dictionary<Type, FieldInfo>();
public static MyDbContext GetContext<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class

var type = typeof(TEntity);
if (!Common._fields.ContainsKey(type))
BindingFlags.Instance));

return Common._fields[type].GetValue(dbSet) as MyDbContext;




So, is a better way to get the InternalDbContext without rely on Reflection, since I'm accessing a private field of a third party package, I fear that can silently break my code at any time?



As you may have noticed, I'm using Extensions like a Repository Pattern, is there a good idea? how i can improve that?







share|improve this question

















  • 2




    Please post your real code. Psedocode is off-topic on Code Review.
    – t3chb0t
    Jul 17 at 6:26










  • @t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
    – Tobias Mesquita
    Jul 17 at 12:00






  • 2




    The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
    – Graipher
    Jul 17 at 12:51











  • Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
    – t3chb0t
    Jul 17 at 18:57











  • Entity Framework Core
    – Tobias Mesquita
    Jul 17 at 18:58












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a DbContext like that.:



Domain.MyDbContext



public class CoiDbContext : IdentityDbContext<Usuario, Funcao, Guid>

public DbSet<OcorrenciaTalao> OcorrenciasTalao get; set;



Some reusable queries like that:



Extenions.OcorrenciasTalao



public static class OcorrenciasTalao

private static Func<CoiDbContext, int, Task<int>> _getProximoSequencial = EF.CompileAsyncQuery((CoiDbContext context, int ano) =>
context.OcorrenciasTalao.Where(x => x.BoAno == ano).Select(x => x.BoSequencial + 1).DefaultIfEmpty(1).Max());

public static async Task<int> GetProximoSequencial(this DbSet<OcorrenciaTalao> dbSet, int ano)

var context = dbSet.GetContext();
return await _getNextSequencial(context, ano);




Since I want to extend the DbSet<TEntity> indeed of the DbContext, I need to get the InternalDbContext in order to compile my reusable query.



Extenions.Common



public static class Common

private static Dictionary<Type, FieldInfo> _fields = new Dictionary<Type, FieldInfo>();
public static MyDbContext GetContext<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class

var type = typeof(TEntity);
if (!Common._fields.ContainsKey(type))
BindingFlags.Instance));

return Common._fields[type].GetValue(dbSet) as MyDbContext;




So, is a better way to get the InternalDbContext without rely on Reflection, since I'm accessing a private field of a third party package, I fear that can silently break my code at any time?



As you may have noticed, I'm using Extensions like a Repository Pattern, is there a good idea? how i can improve that?







share|improve this question













I have a DbContext like that.:



Domain.MyDbContext



public class CoiDbContext : IdentityDbContext<Usuario, Funcao, Guid>

public DbSet<OcorrenciaTalao> OcorrenciasTalao get; set;



Some reusable queries like that:



Extenions.OcorrenciasTalao



public static class OcorrenciasTalao

private static Func<CoiDbContext, int, Task<int>> _getProximoSequencial = EF.CompileAsyncQuery((CoiDbContext context, int ano) =>
context.OcorrenciasTalao.Where(x => x.BoAno == ano).Select(x => x.BoSequencial + 1).DefaultIfEmpty(1).Max());

public static async Task<int> GetProximoSequencial(this DbSet<OcorrenciaTalao> dbSet, int ano)

var context = dbSet.GetContext();
return await _getNextSequencial(context, ano);




Since I want to extend the DbSet<TEntity> indeed of the DbContext, I need to get the InternalDbContext in order to compile my reusable query.



Extenions.Common



public static class Common

private static Dictionary<Type, FieldInfo> _fields = new Dictionary<Type, FieldInfo>();
public static MyDbContext GetContext<TEntity>(this DbSet<TEntity> dbSet) where TEntity : class

var type = typeof(TEntity);
if (!Common._fields.ContainsKey(type))
BindingFlags.Instance));

return Common._fields[type].GetValue(dbSet) as MyDbContext;




So, is a better way to get the InternalDbContext without rely on Reflection, since I'm accessing a private field of a third party package, I fear that can silently break my code at any time?



As you may have noticed, I'm using Extensions like a Repository Pattern, is there a good idea? how i can improve that?









share|improve this question












share|improve this question




share|improve this question








edited Jul 17 at 16:07









t3chb0t

31.8k54095




31.8k54095









asked Jul 16 at 20:19









Tobias Mesquita

1021




1021







  • 2




    Please post your real code. Psedocode is off-topic on Code Review.
    – t3chb0t
    Jul 17 at 6:26










  • @t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
    – Tobias Mesquita
    Jul 17 at 12:00






  • 2




    The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
    – Graipher
    Jul 17 at 12:51











  • Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
    – t3chb0t
    Jul 17 at 18:57











  • Entity Framework Core
    – Tobias Mesquita
    Jul 17 at 18:58












  • 2




    Please post your real code. Psedocode is off-topic on Code Review.
    – t3chb0t
    Jul 17 at 6:26










  • @t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
    – Tobias Mesquita
    Jul 17 at 12:00






  • 2




    The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
    – Graipher
    Jul 17 at 12:51











  • Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
    – t3chb0t
    Jul 17 at 18:57











  • Entity Framework Core
    – Tobias Mesquita
    Jul 17 at 18:58







2




2




Please post your real code. Psedocode is off-topic on Code Review.
– t3chb0t
Jul 17 at 6:26




Please post your real code. Psedocode is off-topic on Code Review.
– t3chb0t
Jul 17 at 6:26












@t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
– Tobias Mesquita
Jul 17 at 12:00




@t3chb0t i renamed the classes and proprieties, and now u have the real code. I just don't know how the real one diffs from the pseudo code.
– Tobias Mesquita
Jul 17 at 12:00




2




2




The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
– Graipher
Jul 17 at 12:51





The difference is that now we know that that is your real code, so when we make a recommendation it is actually useful to you instead of you having to say "Yeah that's all nice and dandy, but it won't work in my real code because actually I have...", which would make this just a waste of time for everybody involved.
– Graipher
Jul 17 at 12:51













Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
– t3chb0t
Jul 17 at 18:57





Do you really need to hack it with reflection (which is a very bad idea unless you really really really have to)? Doesn't your 3rd party package provide any other access to the db-context or extensibility mechanisms? Can we know its name?
– t3chb0t
Jul 17 at 18:57













Entity Framework Core
– Tobias Mesquita
Jul 17 at 18:58




Entity Framework Core
– Tobias Mesquita
Jul 17 at 18:58















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199624%2faccessing-internal-dbcontext-with-reflection%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199624%2faccessing-internal-dbcontext-with-reflection%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?