ajax multiple,每个MVC 4视图的多个Ajax请求(Multiple Ajax Requests per MVC 4 View)

I'm using the repository pattern with a context and ninject as the IOC. I have a service which handles getting and setting page properties in the database.

public class MyContext : DbContext

{

public MyContext() : base ("DefaultConnection")

{

}

public DbSet PageProperties { get; set; }

public DbSet Contacts { get; set; }

}

public class DefaultRepository : IRepository

{

MyContext _context;

public DefaultRepository(MyContext context)

{

_context = context;

}

public IQueryable PageProperties { get { return _context.PageProperties; } }

public IQueryable Contacts { get { return _context.Contacts; } }

}

public class ModuleLoader : NinjectModule

{

public ModuleLoader()

{

}

public override void Load()

{

var context = new MyContext();

context.Database.Initialize(false);

Bind().ToConstant(context).InSingletonScope();

Bind().To();

Bind().To().InSingletonScope();

}

}

public class DefaultPagePropertyProvider : IPagePropertyProvider

{

IRepository _repository;

object _syncLock = new object();

public DefaultPagePropertyProvider (IRepository repository)

{

_repository = repository;

}

public string GetValue(string pageName, string propertyName

{

lock (_syncLock)

{

var prop = page.PageProperties.FirstOrDefault(x => x.Property.Equals(propertyName) && x.PageName.Equals(pageName)).Value;

return prop;

}

}

public void SetValue(string pageName, string propertyName, string value)

{

var pageProp = _repository.PageProperties.FirstOrDefault(x => x.Property.Equals(propertyName) && x.PageName.Equals(pageName));

pageProp.Value = value;

_repository.SaveSingleEntity(pageProp);

}

}

In my view I am doing 3 ajax calls, one to get a list from contacts to fill out a table, one ajax call to determine how many pages i have depending on the page size I'm using, and one ajax call to set the page size that I want to use. so a select box changes the page size (How many contacts per page: [ 30]) , a table that displays the contacts (generated from jquery which decifers json), and finally a div containing a list of page numbers to click. The workflow is, call GetContacts(), this function then queries the PageProperties to find out the page size to use, then call GetPages(), this function also queries PageProperties to find out what page size to use, SetPageSize() which sets the page size. So GetContacts() and GetPages() is used when a page is selected from the div, SetPageSize() then GetContacts() and GetPages() is called when the select box change event is fired. GetContacts() and GetPages() is only called when the first SetPageSize() $.ajax request is done() and there is a success from that function.

Now, before I added lock(syncLock) in the DefaultPageProperty service and before I added InSingletonScope to both that service and the context, I was getting two errors.

The connection was not closed. The connection's current state is connecting.

An EdmType cannot be mapped to CLR classes multiple times

I assumed because the connection was in a connecting state, that the context was being reused and reused and reused, so I thought putting that to SingletonScope() would mean that only one connection was made, then I thought the same about DefaultPageProperty and then because I was making async calls to that service, I should put a lock over the database querying.

It works, and the problems don't exist. But I don't know if what I have done is correct within the pattern I'm using, I'm wondering if I've missed something fundamental? My question is, is this a proper/viable solution which won't create any caveats later down the road? Have I actually solved the issue or just created more?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值