0

I try to write an MVC n tier application. I have used repository pattern during connecting to the database. In my repository class, I have a Context variable inside repository class. I am not sure that this approach is true. Here are my codes:

 public class TTPDbContext : DbContext
{

    public TTPDbContext() : base("TTPContext")
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<TTPDbContext>());
        Database.Log = s => Debug.WriteLine(s);
        Configuration.AutoDetectChangesEnabled = true;

    }

    public DbSet<Kisi> Kisiler { get; set; }
    public DbSet<BakanlikBirim> BakanlikBirimleri { get; set; }
    public DbSet<DisBirim> DisBirimler { get; set; }
    public DbSet<Kullanici> Kullanicilar { get; set; }

    public DbSet<Talep> Talepler { get; set; }
    public DbSet<UnvanPozisyon> UnvanPozisyonlar { get; set; }

    public DbSet<TalepDurum> TalepDurumlar { get; set; }

    public DbSet<TalepKagidi> TalepKagidi { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("OsiTtp");
    }

}

Here is the repository class:

public class TTPRepository : ITTPRepository,IDisposable
{

private IErrorHandling _errorHandling;

private TTPDbContext _context;

public TTPRepository()
{

    this._errorHandling = new ErrorHandling();
    this._context = new TTPDbContext();
}

// other sections has been dismissed for brevity.

public List<DisBirim> GetAllExternalInstitutions()
{
    List<DisBirim> result = null;
    DbSet<DisBirim> intermediaryresult = null;

    try
    {
        result = new List<DisBirim>();

        intermediaryresult = this._context.DisBirimler;

        if (intermediaryresult != null)
        {
            foreach (DisBirim institution in intermediaryresult)
            {
                result.Add(institution);
            }
        }


    }
    catch (Exception Hata)
    {
        this.yazHata(Hata);
    }

    return result;
}

public void Dispose()
{
    this._context.Dispose();
}

}

I am not sure that is a optimal approach. Do you have any recommandations? Thanks in advance.

2
  • Define what you mean by "optimal"? As written it sounds like you're polling for opinions, which is off-topic for SO. Commented Mar 13, 2017 at 13:44
  • No my intention is not a opinion based, I want to be criticised from the engineering perspective. I have read some documents about my design, however I can not understand many of them. This will be a ASP.NET MVC project, so there are many threads to run this code. In addition, I think opening connection to the database and disposal of TTPDbContext object could be business transactional based, I mean, whenever a business transaction starts, TTPDbContext should be opened and up, and whenever business transaction is over, TTPDbContext should be disposed. I mean this thing as an "optimality". Commented Mar 13, 2017 at 13:56

2 Answers 2

1

I would recommend to read the msdn documentation

Instead of this

public TTPRepository()
{
  this._errorHandling = new ErrorHandling();
  this._context = new TTPDbContext();
}

try this

public TTPRepository(TTPDbContext context,ErrorHandling errorHandler)
{
  this._errorHandling = errorHandler;
  this._context = context;
}

By this your repository is ready to work with a context and Error Handler. I always recommend to use something like IErrorHandler and IDbContext rather than concrete classes.

So you have freedom to initialize like this. Even if you use IoC containers you can control the lifetime of your Context.

var yourRepo = new TTPRepository(new TTPDbContext());
Sign up to request clarification or add additional context in comments.

Comments

0

When use repository pattern and interface best practice is that use IoC Container for inject DbContext to repository constructor.

If you are using an IoC Container, you can control the lifetime of the DbContext to ensure that all instances of Repository get the same Context.

you must one of IoC containers likes Unity, Ninject, Autofac, ...

Documentation of unity usage Dependency Injection in ASP.NET MVC - An Introduction

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.