1

I am a winform guy moving to asp.net mvc and all my methods are opening and closing connection (shortLived)

    public void Save()
    {
        using (var conn=new SqlConnection(ConnectionString))
        {
            conn.Open()
            et.....
        }
    }

but I have noticed that you tend to keep the connection open and having a class to handle the connectivity implementing IDisposible and then keep this connection open.something like

      public class DbManager : IDisposable
        {
            private IDbConnection Conn { get; set; }
           public IDbConnection Connection
            {
                get
                {
                    if (Conn.State == ConnectionState.Closed)
                        Conn.Open();

                    return Conn;
                }
            }
            public DbManager(string connString)
            {
                Conn = new SqlConnection(connString);
            }

            public void Dispose()
            {
                if (Conn == null) return;
                if (Conn.State == ConnectionState.Open)
                {
                    Conn.Close();
                    Conn.Dispose();
                }
                Conn = null;

My understanding was that it's not the responsability of the consumer to close connectivity and also I am not sure whether you would run out of pools if you have many requests

can somebody clarify what is the best practice?

Please note that I am not using EF but pure ado.net,just in case you mention dbcontext etc...

It seems all wrong to me ,Am I missing the obvious

thanks for clarifying

FYI

I am implementing my own aspnet identity provider (NO EF) and noticed that people do the following in the web project which seems wrong to me as you coupling your datalayer to your UI:

Shall I just shut my brain and follow?

  app.CreatePerOwinContext(ApplicationDbContext.Create);

    public class ApplicationDbContext : DbManager
    {
        public ApplicationDbContext(string connectionName)
            : base(connectionName)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        }
    }
developer9969
  • 4,628
  • 6
  • 40
  • 88
  • 1
    Both of the patterns you have described as "accepted" are wrong. Don't do them. The first is completely unnecessary and considered very bad practice. The second is most likely a tutorial compacting loosely coupled layers into a tightly coupled example. – David L Oct 11 '15 at 06:32
  • 1
    @DavidL thanks for your comment.I have not found a single example of what I want to do,searched codeproject-codeplex-github givenup. they all tightly couple the UI to the datalayer.Any psudo code or may be expand a bit more about how you do it?how do you create the owincontext in datalayer? Which one is bad practice and what is good practice then – developer9969 Oct 11 '15 at 06:36
  • @DavidL - Okay, now the OP knows what *not* to do, but you haven't provided any useful direction to go in. What approach *should* we take? – NightOwl888 Oct 11 '15 at 07:19
  • 1
    @NightOwl888 wondering that too!!! to have some direction would be nice.I dont want to get stuck but seems so vital at this stage.anybody else with some sample code or psuedo code?We can all say it's wrong but not helpful if then cant provide an example.I thought shortlived connectivity was the way to go and let ado.net handle it .. that what Microsoft suggest but then when they implement the "createowincontext it goes out of window... – developer9969 Oct 11 '15 at 07:29
  • This post lacks a question, but in general any example you find online that allocates some sort of connection in a controller is flatly wrong. Don't do it, ever, regardless of the size of the project. A controller should call some collaborator/service method that creates a connection that lives long enough to perform its work and then evaporate. It's unfortunate that "bloggers" use stupid practices to illustrate a point without a big red flag highlighting the stupidity, but that's what happens when cheap internet becomes available to the cultures that think blogging stupidly means anything. – Jeff Dunlop Oct 11 '15 at 08:58
  • @JeffDunlop thanks for your time.Well it does not lack a question ,my question was simple "do you do shortlive connection or do you hold onto it eg caching or perrequest?" and I think you answered "shortlived" however beleive me books,blogs,github-codeprojects they all do the same and then people put code in real projects. I here I am trying to get the best practices about mvc and all my years (winforms-wwcf)learning decoupling-solid etc... go out of the windows as it's seems good practice in mvc to reference dal as you doing the rootcomposition there. – developer9969 Oct 11 '15 at 09:10
  • @developer9969 I answered the original question, *"is the current example best practice?"*. The answer is no. The problem is that your followup question *"what is the best practice then?"* is too broad for Stack Overflow and too difficult to answer appropriately. If you can provide a concrete question, you'll get a concrete answer. Please see http://stackoverflow.com/help/how-to-ask – David L Oct 11 '15 at 15:02
  • 1
    @DavidL with due respect your help is useless .can you imagine me reviewing your code and sayings "it;s all wrong" and you saying what is right then and i answer "oh well .....my question is not to broad but you made broad and vague in your answer – developer9969 Oct 11 '15 at 15:30
  • Go the answer from this link http://stackoverflow.com/questions/9811899/how-should-one-maintain-a-database-connection-in-an-asp-net-mvc-application – developer9969 Oct 14 '15 at 11:02

0 Answers0