HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Multiple Ajax Requests per MVC 4 View

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
ajaxmvcperviewmultiplerequests

Problem

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 s

Solution

I don't think I completely know what is going on in your code.

but there is one thing that I think you are doing wrong.

you are creating a connection over the web and trying to force it to stay open.

your web application should

  • Connect



  • Send or receive the information that it needs



  • Disconnect



you should never leave a connection open for extended periods of time.

every time you need something from the server, open a new connection, when you are finished close the connection.

I may be way out of line, but that struck me as off about the way you explained that part of your application

Context

StackExchange Code Review Q#30082, answer score: 3

Revisions (0)

No revisions yet.