我有一个C#解决方案,包含两个项目,ProductStore.Web和ProductStore.Data,都针对.NET Core 2.0。
我的HomeController和CustomerRepository如下所示(我已经在HomeController中设置了它的速度,客户创建将在客户控制器中,但尚未支持它):
namespace ProductStore.Web.Controllers
{
public class HomeController : Controller
{
private readonly DatabaseContext _context;
public HomeController(DatabaseContext context)
{
_context = context;
}
public IActionResult Index()
{
ICustomerRepository<Customer> cr = new CustomerRepository(_context);
Customer customer = new Customer
{
// customer details
};
//_context.Customers.Add(customer);
int result = cr.Create(customer).Result;
return View();
}
}
}
namespace ProductStore.Data
{
public class CustomerRepository : ICustomerRepository<Customer>
{
DatabaseContext _context;
public CustomerRepository(DatabaseContext context)
{
_context = context;
}
}
}
依赖注入会在控制器内自动解析_context。然后,我将上下文作为CustomerRepository的参数传递,该参数位于ProductStore.Data。
中我的问题有两个:
IServiceCollection services
访问上下文,其方式与我的应用程序StartUp.cs类中的DatabaseContext插入服务的方式类似...... 我觉得我不应该传递上下文,CustomerRepository应该负责获取上下文。
仅供参考,对于MVC而言相对较新,并且是Entity Framework和Dependency Injection的新手
由于
答案 0 :(得分:4)
您不需要将context
传递给controller
,以便能够使用在存储库内的服务中注册的context
。我喜欢这样做的方式如下。将context
注入repository
,然后将repository
注入控制器。在.Net Core中使用Microsoft依赖注入扩展,它将如下所示
// Service registrations in Startup class
public void ConfigureServices(IServiceCollection services)
{
// Also other service registrations
services.AddMvc();
services.AddScoped<DatabaseContext, DatabaseContext>();
services.AddScoped<ICustomerRepository<Customer>, CustomerRepository>();
}
// Controller
namespace ProductStore.Web.Controllers
{
public class HomeController : Controller
{
private readonly ICustomerRepository _customerRepository;
public HomeController(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
public IActionResult Index()
{
Customer customer = new Customer
{
// customer details
};
//_context.Customers.Add(customer);
int result = _customerRepository.Create(customer).Result;
return View();
}
}
}
//Repository
namespace ProductStore.Data
{
public class CustomerRepository : ICustomerRepository<Customer>
{
DatabaseContext _context;
public CustomerRepository(DatabaseContext context)
{
_context = context;
}
}
}
在此DependencyResolver
尝试解析ICustomerRepository
以注入HomeController
后,ICustomerRepository
注册的CustomerRepository
(在我们的案例中为DatabaseContext
} )有一个构造函数需要DependencyResolver
作为参数,而DatabaseContext
试图获得CustomerRepository
的注册服务并将其注入 <!-- Class Info -->
<!-- Fliter Btn -->
<button class="clearBtn btn" id="clearBtn">Clear Filter</button>
<button class="filterBtn btn" id="filterBtn">Filter</button>
<!-- Fliter Btn -->
<!-- Fliter -->
<div class="container" id="filterBoxes">
<div class="row " id="filterField">
<div class="col-lg-2 col-md-2 col-sm-4 fliterBox">
<button id="actBtn" class="actBtn btn">Acting Classes</button>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 fliterBox">
<button id="danceBtn" class="danceBtn btn">Dance Classes</button>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 fliterBox">
<button id="voiceBtn" class="voiceBtn btn">Voice Classes</button>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 fliterBox">
<button id="tchBtn" class="tchBtn btn">Tech Classes</button>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 fliterBox">
<button id="monBtn" class="monBtn btn">Monday Classes</button>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 fliterBox">
<button id="thursBtn" class="thursBtn btn">Thursday Classes</button>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 fliterBox">
<button id="s1Btn" class="s1Btn btn">Session 1 Classes</button>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 fliterBox">
<button id="s2Btn" class="s2Btn btn">Session 2 Classes</button>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 fliterBox">
<button id="g2-6Btn" class="g2-6Btn btn">Grade 2 - 6 Classes</button>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 fliterBox">
<button id="g7-12Btn" class="g7-12Btn btn">Grade 7 - 12 Classes</button>
</div>
</div>
</div>
<!-- Fliter -->
<!-- Acting -->
<div class="container">
<h3 class="classTitle acting">Acting Classes</h3>
<!--Class-->
<div class="row classes"> <!--Class-->
<div class="col-md-3 col-sm-3 acting g2-6 thurs s1 class" id="clown">
<h4 class="className">Clowning Skills</h4>
<p class="classInfo">Grades 2 – 6<br />Thursdays Session 1</p>
<!-- course Discription -->
<div class="panel-group" id="accordion2" role="tablist" aria-
multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-
toggle="collapse" data-parent="#accordion1"
href="#collapseOne2">Course Discription </a></h4>
</div>
<div id="collapseOne2" class="panel-collapse collapse in
courseCollapse">
<div class="panel-body">
<p class="classDis">Are you always looking for ways to entertain
and amaze? Do you love to make people laugh? Add to your
hilarious skills each week as Ms Amy Arpan teaches you the tricks
of the trade, including juggling!</p>
</div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div>
<!--Class-->
<div class="col-md-3 col-sm-3 acting g7-12 mon s2 class" id="improve1a">
<h4 class="className">Improve 1A</h4>
<p class="classInfo">Grades 7 – 12<br />Mondays Session 2</p>
<!-- course Discription -->
<div class="panel-group" id="accordion3" role="tablist" aria-
multiselectable="true" >
<div class="panel panelStyle">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-
toggle="collapse" data-parent="#accordion1" href="#collapseOne3">Course
Description & Prerequisite </a></h4>
</div>
<div id="collapseOne3" class="panel-collapse collapse in
courseCollapse">
<div class="panel-body">
<p class="classDis">
Learn challenging improv games and take your comic timing to
the next level.</p>
<p><em>(Pre-req: All students must have at least one previous
show experience)</em></p>
</div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div>
<!--Class-->
<div class="col-md-3 col-sm-3 acting g2-6 mon s2 class" id="improve1b">
<h4 class="className">Improve 1B</h4>
<p class="classInfo">Grades 2 – 6<br />Mondays Session 2</p>
<!-- course Discription -->
<div class="panel-group" id="accordion4" role="tablist" aria-multiselectable="true" >
<div class="panel panelStyle">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne4">Course Description & Prerequisite </a></h4>
</div>
<div id="collapseOne4" class="panel-collapse collapse in courseCollapse">
<div class="panel-body">
<p class="classDis">
Time to tickle the funny bone! This class is perfect for any young budding comic. Learn new short form improv games each week and start your path to comic genius.</p>
</div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div>
<!--Class-->
</div><!-- acting classes section-->
</div><!-- acting container-->
<!-- Singing Classes -->
<h3 class="classTitle sing">Vocie Studio</h3>
<div class="container">
<div class="row col-centered">
<div class="col-md-3 col-sm-3 g7-12 mon s2 sing class" id="voice studio 1A">
<h4 class="className">Voice Studio 1A</h4>
<p class="classInfo">Grades 7 – 12<br />Mondays Session 2</p>
<!-- course Discription -->
<div class="panel-group" id="accordion5" role="tablist" aria-multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne5">Course Discription </a></h4>
</div>
<div id="collapseOne5" class="panel-collapse collapse in courseCollapse">
<div class="panel-body">
<p class="classDis">Are you looking to learn proper singing technique or simply brush up on your vocal skills? The Voice Studio is the right class for you! Join our studio to practice good vocal habits, fundamental singing technique and understand how the vocal anatomy works to give you the best tone quality. You will be introduced to the art song and folk song genre.</p>
</div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div>
<!--Class-->
<div class="col-md-3 col-sm-3 g2-6 mon s1 sing class" id="voiceStudio1b">
<h4 class="className">Voice Studio 1B</h4>
<p class="classInfo">Grades 2 – 6<br />Mondays Session 1</p>
<!-- course Discription -->
<div class="panel-group" id="accordion6" role="tablist" aria-multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne6">Course Discription </a></h4>
</div>
<div id="collapseOne6" class="panel-collapse collapse in courseCollapse">
<div class="panel-body"><p class="classDis">Are you looking to learn proper singing technique or simply brush up on your vocal skills? The Voice Studio is the right class for you! Join our studio to practice good vocal habits, fundamental singing technique and understand how the vocal anatomy works to give you the best tone quality. You will be introduced to the art song and folk song genre.</p></div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div><!-- Class-->
<!--Class-->
<div class="col-md-3 col-sm-3 g7-12 thurs s2 sing class" id="voiceStudio2b">
<h4 class="className">Voice Studio 2B</h4>
<p class="classInfo">Grades 2 – 6<br />Thursdays Session 2</p>
<!-- course Discription -->
<div class="panel-group" id="accordion7" role="tablist" aria-multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne7">Course Discription & Prerequisite </a></h4>
</div>
<div id="collapseOne7" class="panel-collapse collapse in courseCollapse">
<div class="panel-body"><p class="classDis">Continue your vocal journey and re-discover your voice in this next level of The Voice Studio. You will continue to develop your singing skills with an emphasis in tone quality and resonance. Students will expand their repertoire by adding music from the Broadway and jazz genres.</p> <p><em>(Students must take The Voice Studio 1A/1B, or audition, to be in this class.)</em></p></div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div><!-- Class-->
<!--Class-->
<div class="col-md-3 col-sm-3 g7-12 thurs s2 sing class" id="duets1a">
<h4 class="className">Duets 1A</h4>
<p class="classInfo">Grades 7 – 12<br />Thursdays Session 2</p>
<!-- course Discription -->
<div class="panel-group" id="accordion8" role="tablist" aria-multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne8">Course Discription </a></h4>
</div>
<div id="collapseOne8" class="panel-collapse collapse in courseCollapse">
<div class="panel-body"><p class="classDis">Do you love the harmonious sound of the Broadway duet? Would you like to feel confident singing with another person? This Duets class will be the perfect fit! Learn how to balance and blend with another singer and confidently sing harmonies. This is a skill that is critical to the musical theatre experience!</p></div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div><!-- Class-->
</div> <!-- sing row --->
</div><!-- Sing contaner -->
<!-- Dance Classes -->
<h3 class="classTitle dance">Dance Classes</h3>
<div class="container">
<div class="row col-centered">
<div class="col-md-3 col-sm-3 g7-12 mon s1 dance class" id="hiphop1a">
<h4 class="className">Hip-Hop 1A</h4>
<p class="classInfo">Grades 7 – 12<br />Mondays Session 1</p>
<!-- course Discription -->
<div class="panel-group" id="accordion9" role="tablist" aria-multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne9">Course Discription </a></h4>
</div>
<div id="collapseOne9" class="panel-collapse collapse in courseCollapse">
<div class="panel-body">
<p class="classDis">Hip hop is an urban street dance style that infuses breaking, popping, locking, b-boy, funk and jazz. This high energy class is for beginners in 7th- 12th grades.</p>
</div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div>
<!--Class-->
<div class="col-md-3 col-sm-3 g2-6 thurs s2 dance class" id="hipHop1b">
<h4 class="className">Hip-Hop 1B</h4>
<p class="classInfo">Grades 2 – 6<br />Thursdays Session 2</p>
<!-- course Discription -->
<div class="panel-group" id="accordion10" role="tablist" aria-multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne10">Course Discription </a></h4>
</div>
<div id="collapseOne10" class="panel-collapse collapse in courseCollapse">
<div class="panel-body"><p class="classDis">Hip hop is an urban street dance style that infuses breaking, popping, locking, b-boy, funk and jazz. This high energy class is for beginners in 2nd- 6th grades.</p></div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div><!-- Class-->
<!--Class-->
<div class="col-md-3 col-sm-3 g7-12 thurs s2 dance class" id="tapB1a">
<h4 class="className">Tap Basics 1A</h4>
<p class="classInfo">Grades 7 – 12<br />Thursdays Session 2</p>
<!-- course Discription -->
<div class="panel-group" id="accordion11" role="tablist" aria-multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne11">Course Discription </a></h4>
</div>
<div id="collapseOne11" class="panel-collapse collapse in courseCollapse">
<div class="panel-body"><p class="classDis">This class, open to students in grades 7th and up, is designed for the development of basic tap technique and terminology highlighting the importance of rhythm and sound. The class will focus on classic Broadway style tap dance. No prior tap experience is required. We will work to create a percussive instrument out of our feet. Purchase of tap shoes is required prior to start of class. Preferably a black tied tap shoe that can be picked up at any dance shoe store.</p></div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div><!-- Class-->
<!--Class-->
<div class="col-md-3 col-sm-3 g2-6 thurs s1 dance class" id="TapB1b">
<h4 class="className">Tap Basics 1B</h4>
<p class="classInfo">Grades 2 – 6<br />Thursdays Session 1</p>
<!-- course Discription -->
<div class="panel-group" id="accordion12" role="tablist" aria-multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne12">Course Discription </a></h4>
</div>
<div id="collapseOne12" class="panel-collapse collapse in courseCollapse">
<div class="panel-body"><p class="classDis">This class, open to students in 2nd to 6th grades, is designed for the development of basic tap technique and terminology highlighting the importance of rhythm and sound. We will go slow in our exploration of beginner tap technique. The class will focus on classic Broadway style tap dance. We will work to create a percussive instrument out of our feet. Purchase of tap shoes is required prior to start of class. Preferably a black tied tap shoe that can be picked up at any dance shoe store.</p></div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div><!-- Class-->
<!--Class-->
<div class="col-md-3 col-sm-3 g7-12 mon s2 dance class" id="Tap2a">
<h4 class="className">Tap 2A</h4>
<p class="classInfo">Grades 7 – 12<br />Mondays Session 2</p>
<!-- course Discription -->
<div class="panel-group" id="accordion13" role="tablist" aria-multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-toggle="collapse" data-parent="#accordion1" href="#collapseOne13">Course Discription & Prerequisite </a></h4>
</div>
<div id="collapseOne13" class="panel-collapse collapse in courseCollapse">
<div class="panel-body"><p class="classDis">Tap 2A is an upper level tap class for those who completed Tap 1A. Tap is designed to develop rhythm, musicality, and coordination.</p>
<p><em>(Pre-req: Tap 1A, or Tap 1B and 6th grade placement, or a placement audition)</em></p></div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div><!-- Class-->
<!--Class-->
<div class="col-md-3 col-sm-3 g7-12 mon s1 dance class" id="Tap2b">
<h4 class="className">Tap 2B</h4>
<p class="classInfo">Grades 7 – 12<br />Mondays Session 1</p>
<!-- course Discription -->
<div class="panel-group" id="accordion14" role="tablist" aria-
multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab">
<h4 class="panel-title" style="text-align: center;"><a data-
toggle="collapse" data-parent="#accordion1" href="#collapseOne14">Course
Discription </a></h4>
</div>
<div id="collapseOne14" class="panel-collapse collapse in courseCollapse">
<div class="panel-body"><p class="classDis">Tap 2B is an upper level tap class for those who completed Tap 1B. Tap is designed to develop rhythm, musicality, and coordination.</p><p><em> (Pre-req: Tap 1B or a placement audition)!</em></p></div>
</div>
</div>
</div><!-- Corse description -->
<!-- add to cart Button-->
<div class="row">
<div class="col-lg-12 cartBtn">
<button class="addToCartBtnReg hidden">Add To Cart</button>
</div>
</div><!-- add to cart Button-->
</div><!-- Class-->
</div> <!-- dance row --->
</div><!-- dance contaner -->
答案 1 :(得分:3)
如果您在ConfigureServices
方法中定义存储库,则无需将DbContext
注入控制器,只需注入存储库:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped(typeof(ICustomerRepository<>), typeof(CustomerRepository<>));
}
然后你可以简单地将存储库注入控制器:
public class HomeController : Controller
{
private readonly ICustomerRepository _customerRepository;
public HomeController(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
...
}
依赖注入器负责将DbContext
注入您的存储库。
答案 2 :(得分:1)
1。这是最佳实践(将上下文从控制器传递到CustomerRepository)
我认为你正在寻找类似“工作单元”模式的东西。
Microsoft编写了一个关于创建一个here的教程。
我也会在您的控制器中注入存储库而不是您的存储库 上下文。
2。如果不是最佳实践,我是否可以通过IServiceCollection服务访问上下文,其方式与将DatabaseContext插入到我的应用程序StartUp.cs类中的服务的方式类似...
如果我理解正确,那么你可以。还添加了 CustomerRepository到您的StartUp.cs中的服务,以便您可以使用 它在你的控制器中。
Microsoft的Mabye this教程也将为您提供帮助。