Click here to Skip to main content
15,898,134 members
Home / Discussions / C#
   

C#

 
GeneralRe: WinForms: run-time loading of UserControl .dll that implements an Interface ? Pin
BillWoodruff30-Jan-15 4:33
professionalBillWoodruff30-Jan-15 4:33 
Questiongetting os info of local network Pin
JSingh_Freelancer29-Jan-15 20:47
JSingh_Freelancer29-Jan-15 20:47 
QuestionRe: getting os info of local network Pin
Richard MacCutchan29-Jan-15 22:59
mveRichard MacCutchan29-Jan-15 22:59 
AnswerRe: getting os info of local network Pin
OriginalGriff30-Jan-15 0:08
mveOriginalGriff30-Jan-15 0:08 
GeneralRe: getting os info of local network Pin
JSingh_Freelancer30-Jan-15 0:16
JSingh_Freelancer30-Jan-15 0:16 
GeneralRe: getting os info of local network Pin
OriginalGriff30-Jan-15 0:23
mveOriginalGriff30-Jan-15 0:23 
AnswerRe: getting os info of local network Pin
Pete O'Hanlon30-Jan-15 1:15
mvePete O'Hanlon30-Jan-15 1:15 
QuestionWhy should I use Interface type of object in Constructor instead of Actual Class Object Pin
Amy Dev29-Jan-15 0:26
Amy Dev29-Jan-15 0:26 
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:

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



Now I've a IDbContext like as :

XML
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:

C#
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 :

XML
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:

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


Now the Repository class implementing this Interface is as below :

C#
public partial class EfRepository<T> : IRepository<T> where T : BaseEntity
{
    private readonly Lazy<IDbContext> _dbContext;
    private readonly IDbContextFactory _dbContextFactory;
    private readonly Lazy<ObjectStateManager> _objectStateManager;

    public EfRepository(IDbContextFactory dbContextFactory)
    {
        _dbContextFactory = dbContextFactory;
        _dbContext= _dbContextFactory.CreateDbContext();
        _objectStateManager= new Lazy<ObjectStateManager>(() => ((IObjectContextAdapter)_dbContext.Value).ObjectContext.ObjectStateManager);
    }

    public T GetById(object id)
    {
        return this.Entities.Find(id);
    }
}


Till now we are done with the Infrastructure level setup for DB Access Management. Now the thing is to utilize this setup into Controllers(as I'm having directly accessing Repositories from Controllers) to as below :

SQL
public CountryController(Lazy<IRepository<Country>> countryRepository)
    {
        _countryRepository = countryRepository;
    }

    public Country GetCountryById(int id)
    {
        Country country = _countryRepository.Value.GetByIdNonProxiedAsync(id);

        if (country != null)
            return country;
        else
            return null;
    }


Hopefully all above is clear. Now here are the some questions that I need to be answered :

1) Why we are having this layered flow like as:

IDBContext -> IDBContextFactory -> IRepository <T>


and then finally using this IRepository into Controllers for accessing Data objects. In other words why we are relying on Interfaces instead of actual Class Objects while implementing Constructor Injection for Country Controller ?

2) Is this the correct approach for a Enterprise level Application as it should be much scalable for future purpose. If there is any other then I would be glad to know about that ?

3) In Controller's constructor I've used Lazy>, so what's the purpose of this Lazy and is it beneficial actually If yes then in what way ?

modified 29-Jan-15 8:20am.

AnswerRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
kakan29-Jan-15 0:31
professionalkakan29-Jan-15 0:31 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Amy Dev29-Jan-15 1:04
Amy Dev29-Jan-15 1:04 
AnswerRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Agent__00729-Jan-15 0:34
professionalAgent__00729-Jan-15 0:34 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Amy Dev29-Jan-15 1:06
Amy Dev29-Jan-15 1:06 
JokeRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Munchies_Matt29-Jan-15 0:41
Munchies_Matt29-Jan-15 0:41 
AnswerRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
megaadam29-Jan-15 0:50
professionalmegaadam29-Jan-15 0:50 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Amy Dev29-Jan-15 1:08
Amy Dev29-Jan-15 1:08 
AnswerRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
User 1013254629-Jan-15 0:50
User 1013254629-Jan-15 0:50 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Amy Dev29-Jan-15 1:09
Amy Dev29-Jan-15 1:09 
AnswerRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
OriginalGriff29-Jan-15 0:54
mveOriginalGriff29-Jan-15 0:54 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
User 1013254629-Jan-15 1:01
User 1013254629-Jan-15 1:01 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Amy Dev29-Jan-15 1:15
Amy Dev29-Jan-15 1:15 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
User 1013254629-Jan-15 1:23
User 1013254629-Jan-15 1:23 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Amy Dev29-Jan-15 1:10
Amy Dev29-Jan-15 1:10 
AnswerRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
_Maxxx_29-Jan-15 0:54
professional_Maxxx_29-Jan-15 0:54 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Amy Dev29-Jan-15 1:11
Amy Dev29-Jan-15 1:11 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
_Maxxx_29-Jan-15 1:30
professional_Maxxx_29-Jan-15 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.