What is the best practice for using Unity Mvc and Entity Framework?












2
















  • What is the best practice to instantiate my DbContext?


I see some people creating an empty interface for the DbContext like this:



public interface IEasyDayContext : IDisposable { }


and then register it in Unity like this (UnityConfig.cs):



container.RegisterType<IEasyDayContext, EasyDayContext>();


I see others using the DbContext as an "interface":



container.RegisterType<DbContext, EasyDayContext>();


I see other people just instantiating the DbContext in the constructor of the service.




  • I am finding it hard to get updated documentation on Unity Mvc, should I even use it today?


  • How do I setup Unity Mvc?



Current documentation (from https://code.msdn.microsoft.com/Dependency-Injection-in-11d54863) suggests that I should put this line of code in my Global.asax.cs file:



UnityConfig.RegisterComponents(); 


However, this method doesn't exist anymore. Should I use this instead?



UnityConfig.RegisterTypes(UnityConfig.Container);



  • Should I use PerRequestLifetimeManager?


I am using



AspNet.Mvc 5.2.6
Unity.Mvc 5.0.15
Unity.Container 5.8.10









share|improve this question

























  • Which version of MVC are you using?

    – mbharanidharan88
    Nov 14 '18 at 14:33











  • Look for "Step 4- Register the Dependency in UnityConfig.cs file" in your linked documentation. If you follow it closely, a RegisterComponents method will exist.

    – grek40
    Nov 14 '18 at 17:07











  • Regarding your other question: setting up unity mvc and registering EF contexts within any dependency resolver framework are very different things. That's why I vote to close as to broad.

    – grek40
    Nov 14 '18 at 17:09











  • @mbharanidharan88 AspNet.Mvc 5.2.6, Unity.Mvc 5.0.15

    – Asger Vestbjerg
    Nov 15 '18 at 9:15













  • This isn't about Unity. It's about dependency injection in general. When you register any type of service through an interface it's easy to test your code using a mock service or dummy object that only needs to implement that interface. Of course, that means that IEasyDayContext should not be empty.

    – Panagiotis Kanavos
    Nov 15 '18 at 9:21
















2
















  • What is the best practice to instantiate my DbContext?


I see some people creating an empty interface for the DbContext like this:



public interface IEasyDayContext : IDisposable { }


and then register it in Unity like this (UnityConfig.cs):



container.RegisterType<IEasyDayContext, EasyDayContext>();


I see others using the DbContext as an "interface":



container.RegisterType<DbContext, EasyDayContext>();


I see other people just instantiating the DbContext in the constructor of the service.




  • I am finding it hard to get updated documentation on Unity Mvc, should I even use it today?


  • How do I setup Unity Mvc?



Current documentation (from https://code.msdn.microsoft.com/Dependency-Injection-in-11d54863) suggests that I should put this line of code in my Global.asax.cs file:



UnityConfig.RegisterComponents(); 


However, this method doesn't exist anymore. Should I use this instead?



UnityConfig.RegisterTypes(UnityConfig.Container);



  • Should I use PerRequestLifetimeManager?


I am using



AspNet.Mvc 5.2.6
Unity.Mvc 5.0.15
Unity.Container 5.8.10









share|improve this question

























  • Which version of MVC are you using?

    – mbharanidharan88
    Nov 14 '18 at 14:33











  • Look for "Step 4- Register the Dependency in UnityConfig.cs file" in your linked documentation. If you follow it closely, a RegisterComponents method will exist.

    – grek40
    Nov 14 '18 at 17:07











  • Regarding your other question: setting up unity mvc and registering EF contexts within any dependency resolver framework are very different things. That's why I vote to close as to broad.

    – grek40
    Nov 14 '18 at 17:09











  • @mbharanidharan88 AspNet.Mvc 5.2.6, Unity.Mvc 5.0.15

    – Asger Vestbjerg
    Nov 15 '18 at 9:15













  • This isn't about Unity. It's about dependency injection in general. When you register any type of service through an interface it's easy to test your code using a mock service or dummy object that only needs to implement that interface. Of course, that means that IEasyDayContext should not be empty.

    – Panagiotis Kanavos
    Nov 15 '18 at 9:21














2












2








2









  • What is the best practice to instantiate my DbContext?


I see some people creating an empty interface for the DbContext like this:



public interface IEasyDayContext : IDisposable { }


and then register it in Unity like this (UnityConfig.cs):



container.RegisterType<IEasyDayContext, EasyDayContext>();


I see others using the DbContext as an "interface":



container.RegisterType<DbContext, EasyDayContext>();


I see other people just instantiating the DbContext in the constructor of the service.




  • I am finding it hard to get updated documentation on Unity Mvc, should I even use it today?


  • How do I setup Unity Mvc?



Current documentation (from https://code.msdn.microsoft.com/Dependency-Injection-in-11d54863) suggests that I should put this line of code in my Global.asax.cs file:



UnityConfig.RegisterComponents(); 


However, this method doesn't exist anymore. Should I use this instead?



UnityConfig.RegisterTypes(UnityConfig.Container);



  • Should I use PerRequestLifetimeManager?


I am using



AspNet.Mvc 5.2.6
Unity.Mvc 5.0.15
Unity.Container 5.8.10









share|improve this question

















  • What is the best practice to instantiate my DbContext?


I see some people creating an empty interface for the DbContext like this:



public interface IEasyDayContext : IDisposable { }


and then register it in Unity like this (UnityConfig.cs):



container.RegisterType<IEasyDayContext, EasyDayContext>();


I see others using the DbContext as an "interface":



container.RegisterType<DbContext, EasyDayContext>();


I see other people just instantiating the DbContext in the constructor of the service.




  • I am finding it hard to get updated documentation on Unity Mvc, should I even use it today?


  • How do I setup Unity Mvc?



Current documentation (from https://code.msdn.microsoft.com/Dependency-Injection-in-11d54863) suggests that I should put this line of code in my Global.asax.cs file:



UnityConfig.RegisterComponents(); 


However, this method doesn't exist anymore. Should I use this instead?



UnityConfig.RegisterTypes(UnityConfig.Container);



  • Should I use PerRequestLifetimeManager?


I am using



AspNet.Mvc 5.2.6
Unity.Mvc 5.0.15
Unity.Container 5.8.10






c# asp.net-mvc entity-framework unity-container dbcontext






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 9:18







Asger Vestbjerg

















asked Nov 14 '18 at 14:31









Asger VestbjergAsger Vestbjerg

1021110




1021110













  • Which version of MVC are you using?

    – mbharanidharan88
    Nov 14 '18 at 14:33











  • Look for "Step 4- Register the Dependency in UnityConfig.cs file" in your linked documentation. If you follow it closely, a RegisterComponents method will exist.

    – grek40
    Nov 14 '18 at 17:07











  • Regarding your other question: setting up unity mvc and registering EF contexts within any dependency resolver framework are very different things. That's why I vote to close as to broad.

    – grek40
    Nov 14 '18 at 17:09











  • @mbharanidharan88 AspNet.Mvc 5.2.6, Unity.Mvc 5.0.15

    – Asger Vestbjerg
    Nov 15 '18 at 9:15













  • This isn't about Unity. It's about dependency injection in general. When you register any type of service through an interface it's easy to test your code using a mock service or dummy object that only needs to implement that interface. Of course, that means that IEasyDayContext should not be empty.

    – Panagiotis Kanavos
    Nov 15 '18 at 9:21



















  • Which version of MVC are you using?

    – mbharanidharan88
    Nov 14 '18 at 14:33











  • Look for "Step 4- Register the Dependency in UnityConfig.cs file" in your linked documentation. If you follow it closely, a RegisterComponents method will exist.

    – grek40
    Nov 14 '18 at 17:07











  • Regarding your other question: setting up unity mvc and registering EF contexts within any dependency resolver framework are very different things. That's why I vote to close as to broad.

    – grek40
    Nov 14 '18 at 17:09











  • @mbharanidharan88 AspNet.Mvc 5.2.6, Unity.Mvc 5.0.15

    – Asger Vestbjerg
    Nov 15 '18 at 9:15













  • This isn't about Unity. It's about dependency injection in general. When you register any type of service through an interface it's easy to test your code using a mock service or dummy object that only needs to implement that interface. Of course, that means that IEasyDayContext should not be empty.

    – Panagiotis Kanavos
    Nov 15 '18 at 9:21

















Which version of MVC are you using?

– mbharanidharan88
Nov 14 '18 at 14:33





Which version of MVC are you using?

– mbharanidharan88
Nov 14 '18 at 14:33













Look for "Step 4- Register the Dependency in UnityConfig.cs file" in your linked documentation. If you follow it closely, a RegisterComponents method will exist.

– grek40
Nov 14 '18 at 17:07





Look for "Step 4- Register the Dependency in UnityConfig.cs file" in your linked documentation. If you follow it closely, a RegisterComponents method will exist.

– grek40
Nov 14 '18 at 17:07













Regarding your other question: setting up unity mvc and registering EF contexts within any dependency resolver framework are very different things. That's why I vote to close as to broad.

– grek40
Nov 14 '18 at 17:09





Regarding your other question: setting up unity mvc and registering EF contexts within any dependency resolver framework are very different things. That's why I vote to close as to broad.

– grek40
Nov 14 '18 at 17:09













@mbharanidharan88 AspNet.Mvc 5.2.6, Unity.Mvc 5.0.15

– Asger Vestbjerg
Nov 15 '18 at 9:15







@mbharanidharan88 AspNet.Mvc 5.2.6, Unity.Mvc 5.0.15

– Asger Vestbjerg
Nov 15 '18 at 9:15















This isn't about Unity. It's about dependency injection in general. When you register any type of service through an interface it's easy to test your code using a mock service or dummy object that only needs to implement that interface. Of course, that means that IEasyDayContext should not be empty.

– Panagiotis Kanavos
Nov 15 '18 at 9:21





This isn't about Unity. It's about dependency injection in general. When you register any type of service through an interface it's easy to test your code using a mock service or dummy object that only needs to implement that interface. Of course, that means that IEasyDayContext should not be empty.

– Panagiotis Kanavos
Nov 15 '18 at 9:21












0






active

oldest

votes











Your Answer






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: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302583%2fwhat-is-the-best-practice-for-using-unity-mvc-and-entity-framework%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302583%2fwhat-is-the-best-practice-for-using-unity-mvc-and-entity-framework%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine