我有一个MVC 3 Web应用程序,我使用Entity Framework进行数据访问.此外,我简单地使用了存储库模式,例如,所有与产品相关的东西都在"ProductRepository"中处理,所有与User相关的东西都在"UserRepository"中处理.
因此,我使用UNITY容器来创建DataContext的单例实例,我将其注入每个存储库.快速搜索Google,每个人都建议您不要使用DataContext的单例实例,因为它可能会在将来给您带来一些内存泄漏.
所以,受这篇文章的启发,为每个Web请求创建一个DataContext的单例实例就是答案(如果我错了,请纠正我!)
但是,UNITY不支持"Per-web-request"终身经理.但是,可以实现自己的自定义生命周期管理器,它可以为您处理此问题.实际上,这篇文章对此进行了讨论:
Unity中的Singleton Per Call上下文(Web请求)
问题是,我现在已经实现了上面帖子中描述的自定义生命周期管理器,但我不确定这是否是这样做的方法.我也想知道在提供的解决方案中处理datacontext实例的位置?我错过了什么吗?
实际上有更好的方法来解决我的"问题"吗?
谢谢!
以下是我的Global.asax,Controller和Repository的片段.这清楚地说明了我的实施情况.
Global.asax中
var container = new UnityContainer();
container
.RegisterType<ProductsRepository>(new ContainerControlledLifetimeManager())
.RegisterType<CategoryRepository>(new ContainerControlledLifetimeManager())
.RegisterType<MyEntities>(new PerResolveLifetimeManager(), dbConnectionString)
Run Code Online (Sandbox Code Playgroud)
调节器
private ProductsRepository _productsRepository;
private CategoryRepository _categoryRepository;
public ProductsController(ProductsRepository productsRepository, CategoryRepository categoryRepository)
{
_productsRepository = productsRepository;
_categoryRepository = categoryRepository;
}
public ActionResult Index()
{
ProductCategory category = _categoryRepository.GetProductCategory(categoryId);
.
.
.
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_productsRepository.Dispose();
_categoryRepository.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
产品库
public class ProductsRepository : IDisposable
{
private …
Run Code Online (Sandbox Code Playgroud) entity-framework ioc-container unity-container asp.net-mvc-3
由于我还是MVC 3和jquery的新手,我想知道如何解决以下问题的最佳实践解决方案:
我有一个视图,我使用jquery ajax来获取并显示带有产品A的一些产品细节的局部视图.加载的局部视图由一堆html和jquery代码组成,它与局部视图中定义的id相关联.
因此,我想重复使用相同的局部视图来显示同一视图上其他产品的详细信息(例如,在弹出对话框中显示产品B的详细信息).每当显示弹出窗口时,新获取的部分视图将与产品A的部分视图冲突,因为在html中使用相同的id.
有没有办法在局部视图中封装html和javascript,并重复使用几页而不用担心与ID和东西有任何冲突?
我希望我的问题有道理.谢谢,
/尼玛
更新
这是一些伪代码,概述了我的问题:
视图
<script type="text/javascript">
$(document).ready(function () {
$('.productItems').click(function () {
var input = { productId: $(this).attr('data-productID') };
var url = url = '<%: Url.Content("~/ProductDetails/ShowProductDetails") %>';
// Show the modal box with product details
$('#dialogBox').dialog({
title: $(this).attr('data-productTitle')
});
// Fetch content in the background
$.get(url, input, function (result, response) {
$('#dialogBox').html(result);
});
});
});
</script>
<div id="detailsArea">
<% Html.RenderPartial("ProductDetails", Model.Product); %>
</div>
<div id="productLinks">
<span class="productItems" data-productID="123">Product B</a>
</div>
<div id="dialogBox" style="display: …
Run Code Online (Sandbox Code Playgroud) 在我的MVC3项目中,我有一个带有[Authorize]属性的控制器.我有一个没有ajax的表单提交,如果他/她没有登录,它会将用户(如预期的那样)重定向到登录屏幕.
但是,现在我有一个用jquery ajax提交的表单,我怎么能做同样的事情呢?如果他/她未获得授权,请将用户重定向到登录屏幕?成功登录后,应将用户重定向到初始操作.
调节器
[Authorize]
[ValidateInput(false)]
public JsonResult SubmitChatMessage(string message)
{
if (!string.IsNullOrEmpty(message))
{
// Do stuff
}
// Return all chat messages
return GetChatMessages();
}
Run Code Online (Sandbox Code Playgroud)
客户端JQUERY
$(document).ready(function () {
$("form[action$='SubmitChatMessage']").submit(function (event) {
$.ajax({
url: $(this).attr("action"),
type: "post",
dataType: "json",
data: $(this).serialize(),
success: function (response) {
// do stuff
}
});
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
我可以从firebug控制台窗口看到,服务器返回:
GET http://domain/Account/LogOn?ReturnUrl=%2fProductDetails%2fSubmitChatMessage
Run Code Online (Sandbox Code Playgroud)
期待您的帮助!
更新可能的解决方案
我有一个网站,我从数据库中加载图像列表(缩略图).问题是图像显示得相当慢,因为使用Url.Action获取每个缩略图相当耗时(当通过整个MVC管道时).
因此,我想与Ajax和JQUERY异步加载图像,同时为每个图像显示标准加载图像(ajaxloag.info),直到加载图像.这里提出了类似的问题,但我需要一个更完整的例子,因为我对MVC和JQUERY很新.
提前致谢,
查看(partialView)
// Foreach product, display the corresponding thumbnail
<% foreach (var p in Model)
{ %>
.
.
<img width="100" src="<%= Url.Action( "Thumbnail", "Products", new { productId = p.ID } ) %>" alt="" />
Run Code Online (Sandbox Code Playgroud)
调节器
public ActionResult Thumbnail(string productId)
{
try
{
Guid pid = new Guid(productId);
byte[] thumbnailData = _productsRepository.GetProductThumbnail(pid);
if (thumbnailData != null)
{
return File(thumbnailData, "image/jpg");
}
else
{
return File(@"../Content/missingproduct.png", "image/jpg");
}
}
catch (Exception e)
{
throw e; …
Run Code Online (Sandbox Code Playgroud)