Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why should I use Interface type of object in Constructor instead of Actual Class Object

Why should I use Interface type of object in Constructor instead of Actual Class Object

Scheduled Pinned Locked Moved C#
architecturecsharpasp-netregexworkspace
20 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Amy Dev

    I've outsourced my enterprise level project to a freelancer and I got a quite good setup too. But now that contract has finished and the person has also moved to a new technology, in other words not willing to extend the contract. Now I'm looking into this code on myself. I do have a 2 3 years of background in C# and MVC. Below is a rough idea of my application architecture. Hopefully I've tried my best to abstract the architectural details of an enterprise level application. Please let me know if you need further brief on any of the questions. All my Entities are defined as C# POCO classes as:

    public class Product : BaseEntity
    {
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    }

    Now I've a IDbContext like as :

    public interface IDbContext : IDisposable
    {
    IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity;
    }

    Base Entity is a Partial POCO class that each POCO entity is inheriting. Here is a class that implements this IDBContext as:

    public class MyObjectContext : DbContext, IDbContext
    {
    public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
    {
    return base.Set<TEntity>();
    }
    }

    Now I've defined a IDbContextFactory that is responsible for providing the DBContexts as :

    public interface IDbContextFactory
    {
    Lazy<IDbContext> CreateDbContext();
    }

    The class implementing this IDBContextFactory interface is having below structure :

    public class MyDbContextFactory : IDbContextFactory
    {
    public MyDbContextFactory(string dbConnectionString)
    {
    _dbConnectionString = Settings.DbConnectionString;
    _dbContext = CreateDbContext();
    }

    public IDbContext CreateDbContext()
    {
        IDbContext dbContext = new IDbContext(() => CreateNewContext());
        return dbContext;
    }
    
    private MyObjectContext CreateNewContext()
    {
        return new MyObjectContext (\_dbConnectionString);
    }
    

    }

    Here IRepo Pattern comes into role as:

    public partial interface IRepository<T> where T : BaseEntity
    {
    T GetById(object id);
    }

    Now the Repository class implementing this Interface is as below :

    public partial class EfRepository<T> : IRepository<T> where T : BaseEntity
    {
    private readonly Lazy<IDbContext> _dbContext;
    private readonly IDbContextFactory

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #6

    This might be a good starting point... An Absolute Beginner's Tutorial on Dependency Inversion Principle, Inversion of Control and Dependency Injection[^]

    How do you know so much about swallows? Well, you have to know these things when you're a king, you know.

    A 1 Reply Last reply
    0
    • A Amy Dev

      I've outsourced my enterprise level project to a freelancer and I got a quite good setup too. But now that contract has finished and the person has also moved to a new technology, in other words not willing to extend the contract. Now I'm looking into this code on myself. I do have a 2 3 years of background in C# and MVC. Below is a rough idea of my application architecture. Hopefully I've tried my best to abstract the architectural details of an enterprise level application. Please let me know if you need further brief on any of the questions. All my Entities are defined as C# POCO classes as:

      public class Product : BaseEntity
      {
      public int ProductId { get; set; }
      public string ProductName { get; set; }
      }

      Now I've a IDbContext like as :

      public interface IDbContext : IDisposable
      {
      IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity;
      }

      Base Entity is a Partial POCO class that each POCO entity is inheriting. Here is a class that implements this IDBContext as:

      public class MyObjectContext : DbContext, IDbContext
      {
      public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
      {
      return base.Set<TEntity>();
      }
      }

      Now I've defined a IDbContextFactory that is responsible for providing the DBContexts as :

      public interface IDbContextFactory
      {
      Lazy<IDbContext> CreateDbContext();
      }

      The class implementing this IDBContextFactory interface is having below structure :

      public class MyDbContextFactory : IDbContextFactory
      {
      public MyDbContextFactory(string dbConnectionString)
      {
      _dbConnectionString = Settings.DbConnectionString;
      _dbContext = CreateDbContext();
      }

      public IDbContext CreateDbContext()
      {
          IDbContext dbContext = new IDbContext(() => CreateNewContext());
          return dbContext;
      }
      
      private MyObjectContext CreateNewContext()
      {
          return new MyObjectContext (\_dbConnectionString);
      }
      

      }

      Here IRepo Pattern comes into role as:

      public partial interface IRepository<T> where T : BaseEntity
      {
      T GetById(object id);
      }

      Now the Repository class implementing this Interface is as below :

      public partial class EfRepository<T> : IRepository<T> where T : BaseEntity
      {
      private readonly Lazy<IDbContext> _dbContext;
      private readonly IDbContextFactory

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #7

      Rule 1: Learn to read. At the top of the page, it says: "if you need specific help please ask your question here." and links to the QA forum: http://www.codeproject.com/Questions/ask.aspx[^] Instead, you posted what looks like your homework in a non-question forum, because you couldn't be bothered to look properly. Now, what makes you feel that I might want to bother myself and spend time answering your question, when you can't be bothered to ask in the right place?

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      L A 2 Replies Last reply
      0
      • A Amy Dev

        I've outsourced my enterprise level project to a freelancer and I got a quite good setup too. But now that contract has finished and the person has also moved to a new technology, in other words not willing to extend the contract. Now I'm looking into this code on myself. I do have a 2 3 years of background in C# and MVC. Below is a rough idea of my application architecture. Hopefully I've tried my best to abstract the architectural details of an enterprise level application. Please let me know if you need further brief on any of the questions. All my Entities are defined as C# POCO classes as:

        public class Product : BaseEntity
        {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        }

        Now I've a IDbContext like as :

        public interface IDbContext : IDisposable
        {
        IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity;
        }

        Base Entity is a Partial POCO class that each POCO entity is inheriting. Here is a class that implements this IDBContext as:

        public class MyObjectContext : DbContext, IDbContext
        {
        public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
        {
        return base.Set<TEntity>();
        }
        }

        Now I've defined a IDbContextFactory that is responsible for providing the DBContexts as :

        public interface IDbContextFactory
        {
        Lazy<IDbContext> CreateDbContext();
        }

        The class implementing this IDBContextFactory interface is having below structure :

        public class MyDbContextFactory : IDbContextFactory
        {
        public MyDbContextFactory(string dbConnectionString)
        {
        _dbConnectionString = Settings.DbConnectionString;
        _dbContext = CreateDbContext();
        }

        public IDbContext CreateDbContext()
        {
            IDbContext dbContext = new IDbContext(() => CreateNewContext());
            return dbContext;
        }
        
        private MyObjectContext CreateNewContext()
        {
            return new MyObjectContext (\_dbConnectionString);
        }
        

        }

        Here IRepo Pattern comes into role as:

        public partial interface IRepository<T> where T : BaseEntity
        {
        T GetById(object id);
        }

        Now the Repository class implementing this Interface is as below :

        public partial class EfRepository<T> : IRepository<T> where T : BaseEntity
        {
        private readonly Lazy<IDbContext> _dbContext;
        private readonly IDbContextFactory

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #8

        0 - Well structured question! Well done for that! 1 - this forum is the Lounge where programming questions are not allowed. Suggest you go to quick questions and ask this question again. but as a really quick answer, you pass interfaces rather than objects so that you can pass any object that implements that interface - especially useful if you are unit testing, as you can pass fake objects that implement the interface.

        PooperPig - Coming Soon

        A 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Rule 1: Learn to read. At the top of the page, it says: "if you need specific help please ask your question here." and links to the QA forum: http://www.codeproject.com/Questions/ask.aspx[^] Instead, you posted what looks like your homework in a non-question forum, because you couldn't be bothered to look properly. Now, what makes you feel that I might want to bother myself and spend time answering your question, when you can't be bothered to ask in the right place?

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #9

          Rule 2: You don't talk about CodeProject :laugh:

          How do you know so much about swallows? Well, you have to know these things when you're a king, you know.

          A 1 Reply Last reply
          0
          • K kakan

            Run Run! Watch out for the flamethrower!

            Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

            A Offline
            A Offline
            Amy Dev
            wrote on last edited by
            #10

            Thanks mate. Please find it out yourself. I posted this question here by mistake. I appreciate your response.

            1 Reply Last reply
            0
            • A Agent__007

              Amy Dev wrote:

              public Country GetCountryById(int id) { Country country = _countryRepository.Value.GetByIdNonProxiedAsync(id);   if (country != null) return country; else return null; }

              Not to answer your question, but this made me chuckle. :) BTW you may want to ask the question in the QA or in the C# forum.

              Your time will come, if you let it be right.

              A Offline
              A Offline
              Amy Dev
              wrote on last edited by
              #11

              Thanks mate. Nice catch. I posted this question here by mistake. I appreciate your response and I've moved the question to ASP.NET Forum. Just in case if you are interested then my answer is, I tried to abstract as much complexity I can hide from my code but I forgot this one here. Actually this method is coded as per parallel programming.

              1 Reply Last reply
              0
              • M megaadam

                Unfortunately your code will fail due to infrastructure instability[^].

                Life is too shor

                A Offline
                A Offline
                Amy Dev
                wrote on last edited by
                #12

                Thanks mate. Nice catch. I wish you continue your these kind of annoying replies.

                1 Reply Last reply
                0
                • L Lost User

                  This might be a good starting point... An Absolute Beginner's Tutorial on Dependency Inversion Principle, Inversion of Control and Dependency Injection[^]

                  How do you know so much about swallows? Well, you have to know these things when you're a king, you know.

                  A Offline
                  A Offline
                  Amy Dev
                  wrote on last edited by
                  #13

                  Thanks mate. I posted this question here by mistake. I appreciate your response and I've moved the question to ASP.NET Forum. Parallely I'm going through your referenced Link. :) Really thanks

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Rule 1: Learn to read. At the top of the page, it says: "if you need specific help please ask your question here." and links to the QA forum: http://www.codeproject.com/Questions/ask.aspx[^] Instead, you posted what looks like your homework in a non-question forum, because you couldn't be bothered to look properly. Now, what makes you feel that I might want to bother myself and spend time answering your question, when you can't be bothered to ask in the right place?

                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                    A Offline
                    A Offline
                    Amy Dev
                    wrote on last edited by
                    #14

                    Thanks mate. Nice catch. I posted this question here by mistake. I appreciate your response and I've moved the question to ASP.NET Forum. My apologies for wasting your precious time.

                    1 Reply Last reply
                    0
                    • L Lost User

                      0 - Well structured question! Well done for that! 1 - this forum is the Lounge where programming questions are not allowed. Suggest you go to quick questions and ask this question again. but as a really quick answer, you pass interfaces rather than objects so that you can pass any object that implements that interface - especially useful if you are unit testing, as you can pass fake objects that implement the interface.

                      PooperPig - Coming Soon

                      A Offline
                      A Offline
                      Amy Dev
                      wrote on last edited by
                      #15

                      Thanks mate.I posted this question here by mistake. I appreciate your response and I've moved the question to ASP.NET Forum. Regarding your answer, Thanks a lot for that. I got the point. I agree that was a stupid question but looking at someone else code in such a large application confused me. Thanks

                      L 1 Reply Last reply
                      0
                      • L Lost User

                        Rule 2: You don't talk about CodeProject :laugh:

                        How do you know so much about swallows? Well, you have to know these things when you're a king, you know.

                        A Offline
                        A Offline
                        Amy Dev
                        wrote on last edited by
                        #16

                        And Nice revert back @Brent Jenkins. ;) Please let people think what else they want. It's upto them how they represent themselves to others as it describes their atticates which matters equal to technical strength.

                        L 1 Reply Last reply
                        0
                        • A Amy Dev

                          And Nice revert back @Brent Jenkins. ;) Please let people think what else they want. It's upto them how they represent themselves to others as it describes their atticates which matters equal to technical strength.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #17

                          We're a bit like Fight Club here and have similar rules: http://www.urbandictionary.com/define.php?term=Rules+of+Fight+Club[^] Of course, being programmers we're a lot more buff and better looking than those skinny, ugly guys in the film :laugh:

                          How do you know so much about swallows? Well, you have to know these things when you're a king, you know.

                          1 Reply Last reply
                          0
                          • A Amy Dev

                            Thanks mate.I posted this question here by mistake. I appreciate your response and I've moved the question to ASP.NET Forum. Regarding your answer, Thanks a lot for that. I got the point. I agree that was a stupid question but looking at someone else code in such a large application confused me. Thanks

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #18

                            There are no stupid questions - only stupid answers!

                            PooperPig - Coming Soon

                            1 Reply Last reply
                            0
                            • A Amy Dev

                              I've outsourced my enterprise level project to a freelancer and I got a quite good setup too. But now that contract has finished and the person has also moved to a new technology, in other words not willing to extend the contract. Now I'm looking into this code on myself. I do have a 2 3 years of background in C# and MVC. Below is a rough idea of my application architecture. Hopefully I've tried my best to abstract the architectural details of an enterprise level application. Please let me know if you need further brief on any of the questions. All my Entities are defined as C# POCO classes as:

                              public class Product : BaseEntity
                              {
                              public int ProductId { get; set; }
                              public string ProductName { get; set; }
                              }

                              Now I've a IDbContext like as :

                              public interface IDbContext : IDisposable
                              {
                              IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity;
                              }

                              Base Entity is a Partial POCO class that each POCO entity is inheriting. Here is a class that implements this IDBContext as:

                              public class MyObjectContext : DbContext, IDbContext
                              {
                              public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
                              {
                              return base.Set<TEntity>();
                              }
                              }

                              Now I've defined a IDbContextFactory that is responsible for providing the DBContexts as :

                              public interface IDbContextFactory
                              {
                              Lazy<IDbContext> CreateDbContext();
                              }

                              The class implementing this IDBContextFactory interface is having below structure :

                              public class MyDbContextFactory : IDbContextFactory
                              {
                              public MyDbContextFactory(string dbConnectionString)
                              {
                              _dbConnectionString = Settings.DbConnectionString;
                              _dbContext = CreateDbContext();
                              }

                              public IDbContext CreateDbContext()
                              {
                                  IDbContext dbContext = new IDbContext(() => CreateNewContext());
                                  return dbContext;
                              }
                              
                              private MyObjectContext CreateNewContext()
                              {
                                  return new MyObjectContext (\_dbConnectionString);
                              }
                              

                              }

                              Here IRepo Pattern comes into role as:

                              public partial interface IRepository<T> where T : BaseEntity
                              {
                              T GetById(object id);
                              }

                              Now the Repository class implementing this Interface is as below :

                              public partial class EfRepository<T> : IRepository<T> where T : BaseEntity
                              {
                              private readonly Lazy<IDbContext> _dbContext;
                              private readonly IDbContextFactory

                              P Offline
                              P Offline
                              Pete OHanlon
                              wrote on last edited by
                              #19

                              There are many reasons for passing in an interface into a constructor. One reason is to be able to supply different implementations into the constructor, which is particularly useful when you want to unit test your code. Rather than having to pass in a concrete implementation, which might rely on things that are difficult to test, you can use a mocking tool to create a mock of the interface and pass this in. This makes it easy to do things like set up verifications.

                              1 Reply Last reply
                              0
                              • A Amy Dev

                                I've outsourced my enterprise level project to a freelancer and I got a quite good setup too. But now that contract has finished and the person has also moved to a new technology, in other words not willing to extend the contract. Now I'm looking into this code on myself. I do have a 2 3 years of background in C# and MVC. Below is a rough idea of my application architecture. Hopefully I've tried my best to abstract the architectural details of an enterprise level application. Please let me know if you need further brief on any of the questions. All my Entities are defined as C# POCO classes as:

                                public class Product : BaseEntity
                                {
                                public int ProductId { get; set; }
                                public string ProductName { get; set; }
                                }

                                Now I've a IDbContext like as :

                                public interface IDbContext : IDisposable
                                {
                                IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity;
                                }

                                Base Entity is a Partial POCO class that each POCO entity is inheriting. Here is a class that implements this IDBContext as:

                                public class MyObjectContext : DbContext, IDbContext
                                {
                                public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
                                {
                                return base.Set<TEntity>();
                                }
                                }

                                Now I've defined a IDbContextFactory that is responsible for providing the DBContexts as :

                                public interface IDbContextFactory
                                {
                                Lazy<IDbContext> CreateDbContext();
                                }

                                The class implementing this IDBContextFactory interface is having below structure :

                                public class MyDbContextFactory : IDbContextFactory
                                {
                                public MyDbContextFactory(string dbConnectionString)
                                {
                                _dbConnectionString = Settings.DbConnectionString;
                                _dbContext = CreateDbContext();
                                }

                                public IDbContext CreateDbContext()
                                {
                                    IDbContext dbContext = new IDbContext(() => CreateNewContext());
                                    return dbContext;
                                }
                                
                                private MyObjectContext CreateNewContext()
                                {
                                    return new MyObjectContext (\_dbConnectionString);
                                }
                                

                                }

                                Here IRepo Pattern comes into role as:

                                public partial interface IRepository<T> where T : BaseEntity
                                {
                                T GetById(object id);
                                }

                                Now the Repository class implementing this Interface is as below :

                                public partial class EfRepository<T> : IRepository<T> where T : BaseEntity
                                {
                                private readonly Lazy<IDbContext> _dbContext;
                                private readonly IDbContextFactory

                                A Offline
                                A Offline
                                Anurag Gandhi
                                wrote on last edited by
                                #20

                                Cross post. I have already answered it in Asp.Net section.

                                Life is a computer program and everyone is the programmer of his own life.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups