Why should I use Interface type of object in Constructor instead of Actual Class Object
-
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 IDbContextFactoryThis 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.
-
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 IDbContextFactoryRule 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'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 IDbContextFactory0 - 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
-
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...
-
Run Run! Watch out for the flamethrower!
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
-
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.
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.
-
Unfortunately your code will fail due to infrastructure instability[^].
Life is too shor
-
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.
-
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...
-
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
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
-
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.
-
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.
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.
-
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
-
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 IDbContextFactoryThere 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.
-
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 IDbContextFactoryCross post. I have already answered it in Asp.Net section.
Life is a computer program and everyone is the programmer of his own life.