Click here to Skip to main content
15,884,628 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionCount the alphabet and Numeric from a string Pin
Sujith Karivelil31-Jan-15 1:28
professionalSujith Karivelil31-Jan-15 1:28 
AnswerRe: Count the alphabet and Numeric from a string Pin
Peter Leow31-Jan-15 2:35
professionalPeter Leow31-Jan-15 2:35 
AnswerRe: Count the alphabet and Numeric from a string Pin
Anurag Gandhi31-Jan-15 23:25
professionalAnurag Gandhi31-Jan-15 23:25 
Questionforum Pin
Member 1140791030-Jan-15 19:24
Member 1140791030-Jan-15 19:24 
AnswerRe: forum Pin
Anurag Gandhi31-Jan-15 0:17
professionalAnurag Gandhi31-Jan-15 0:17 
QuestionRe: forum Pin
ZurdoDev2-Feb-15 3:12
professionalZurdoDev2-Feb-15 3:12 
Answerform with html css but I don't know asp Pin
Member 1141563430-Jan-15 10:55
Member 1141563430-Jan-15 10:55 
QuestionWhy should I use Interface type of object in Constructor instead of Actual Class Object Pin
Amy Dev29-Jan-15 0:42
Amy Dev29-Jan-15 0:42 
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 _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 :

public CountryController(Lazy<IRepository<Country>> countryRepository)
    {
        _countryRepository = countryRepository;
    }
 
    public Country GetCountryById(int id)
    {
        Country country = _countryRepository.Value.GetById(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 7:16am.

AnswerRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Anurag Gandhi29-Jan-15 6:28
professionalAnurag Gandhi29-Jan-15 6:28 
AnswerRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
F-ES Sitecore2-Feb-15 2:32
professionalF-ES Sitecore2-Feb-15 2:32 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Erik Funkenbusch7-Feb-15 15:38
Erik Funkenbusch7-Feb-15 15:38 
GeneralRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
F-ES Sitecore8-Feb-15 13:08
professionalF-ES Sitecore8-Feb-15 13:08 
AnswerRe: Why should I use Interface type of object in Constructor instead of Actual Class Object Pin
Erik Funkenbusch7-Feb-15 15:52
Erik Funkenbusch7-Feb-15 15:52 
QuestionEmbedding Qr code Scanner[Need Guidance] Pin
Member 1140255328-Jan-15 11:32
Member 1140255328-Jan-15 11:32 
AnswerRe: Embedding Qr code Scanner[Need Guidance] Pin
Member 1140255328-Jan-15 17:38
Member 1140255328-Jan-15 17:38 
QuestionAsp.net Pin
Member 1133602728-Jan-15 10:30
Member 1133602728-Jan-15 10:30 
AnswerRe: Asp.net Pin
Richard MacCutchan28-Jan-15 21:21
mveRichard MacCutchan28-Jan-15 21:21 
AnswerRe: Asp.net Pin
Erik Funkenbusch7-Feb-15 15:55
Erik Funkenbusch7-Feb-15 15:55 
AnswerRe: Asp.net Pin
Rahul Rajat Singh13-Feb-15 0:32
professionalRahul Rajat Singh13-Feb-15 0:32 
QuestionHow to retrieve multiple values from a single SELECT statement? Pin
samflex28-Jan-15 7:02
samflex28-Jan-15 7:02 
AnswerRe: How to retrieve multiple values from a single SELECT statement? Pin
PIEBALDconsult28-Jan-15 7:09
mvePIEBALDconsult28-Jan-15 7:09 
GeneralRe: How to retrieve multiple values from a single SELECT statement? Pin
samflex28-Jan-15 7:15
samflex28-Jan-15 7:15 
GeneralRe: How to retrieve multiple values from a single SELECT statement? Pin
PIEBALDconsult28-Jan-15 7:26
mvePIEBALDconsult28-Jan-15 7:26 
GeneralRe: How to retrieve multiple values from a single SELECT statement? Pin
samflex28-Jan-15 7:32
samflex28-Jan-15 7:32 
GeneralRe: How to retrieve multiple values from a single SELECT statement? Pin
PIEBALDconsult28-Jan-15 7:41
mvePIEBALDconsult28-Jan-15 7:41 

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.