3
\$\begingroup\$

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<PageProperty> PageProperties { get; set; }
    public DbSet<Contact> Contacts { get; set; }
}

public class DefaultRepository : IRepository
{
    MyContext _context;
    public DefaultRepository(MyContext context)
    {
        _context = context;
    }

    public IQueryable<PageProperty> PageProperties { get { return _context.PageProperties; } }
    public IQueryable<Contact> Contacts { get { return _context.Contacts; } }
}

public class ModuleLoader : NinjectModule
{
    public ModuleLoader()
    {

    }

    public override void Load()
    {
        var context = new MyContext();
        context.Database.Initialize(false);
        Bind<MyContext>().ToConstant(context).InSingletonScope();
        Bind<IRepository>().To<DefaultRepository>();
        Bind<IPagePropertyProvider>().To<DefaultPagePropertyProvider>().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?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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

  1. Connect
  2. Send or receive the information that it needs
  3. 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

\$\endgroup\$
5
  • \$\begingroup\$ Yeah, I took that into account, and started doing InTransientScope() that way it calls up a new connection each time, and it solved all my problems :) \$\endgroup\$ Commented Nov 4, 2013 at 12:28
  • \$\begingroup\$ so you are closing the original connection and then opening a new connection everytime? \$\endgroup\$
    – Malachi
    Commented Nov 4, 2013 at 14:11
  • \$\begingroup\$ Yeah, because the underlying connection gets created on instantiation of the context class, everytime I call that class, transient re instantiates a new copy \$\endgroup\$ Commented Nov 5, 2013 at 12:56
  • \$\begingroup\$ when that context class is done it should dispose the connection before another one is even thought about. it might be personal opinion, but I don't like that part of the design, when the object does something, it should open a connection, when it is done it should close that connection. when that object needs to do something (else/again) it should open a completely new connection and close it when it is finished. \$\endgroup\$
    – Malachi
    Commented Nov 5, 2013 at 14:25
  • \$\begingroup\$ I believe that when the class has been finished with, it does get disposed of properly, thus the connection gets terminated correctly \$\endgroup\$ Commented Nov 5, 2013 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.