Open Closed

Is it a problem if I take and use dependency injection instance in the application layer? #7805


0
wcdagency created

hello, my question is, is it a problem if I use dependency injection instance in the .application layer? Isn't the .application part backend?

merhaba sorunu sorum , .application katmanı içinde dependency injection instance alıp kullanmam bir sorun oluşturur mu? .application kısmı backend (arka taraf) değil mi?


12 Answer(s)
  • 0
    ismcagdas created

    Hi @wcdagency

    Could you ask your question in English ? If not, please contact info@aspnetzero.com for futher assitance.

    Thanks,

  • 0
    wcdagency created

    Merhaba, neden ingilizce sormamı istedin? neyse, My question is, is it ok to use dependency injection in application layer end use entity framework in it?

  • 0
    alper created

    The support team helps you from different countries, that's why to communicate with them please write in English.

  • 0
    demirmusa created

    You can use dependency injection in application layer. If you check, you will see that we use it in a lot of places.

    You should use generic repository IRepository in application layer to access your table. Otherwise some of our logics will not effect your db requests.

    See for more information: Documents/Repositories

  • 0
    wcdagency created

    So;image.png

    I could use this approach no problem ,right?

  • 0
    maliming created

    You need to get the DbContext using the IDbContextProvider interface.

    see https://github.com/aspnetboilerplate/aspnetboilerplate/issues/4059

  • 0
    alper created

    If you want to get all courses you can do it with _courseRepository No need to inject DbContext to your AppService (bad practise)

  • 0
    wcdagency created

    thanks @maliming I am checking it out now.

    hi alper, of course ,_courseRepository but I would like to use linq approach. I would need such

    _courseCustomService.Courses.Where(x=>x.Id>3).tolist() ...

    this is my aim.

  • 0
    alper created

    also you can run LinQ queries on the repository class.

    _courseRepository.GetAll().Where(x=>x.Name == "Test").FirstOrDefault();
    

    GetAll() allows you to run LinQ.

    See examples : https://github.com/aspnetzero/aspnet-zero-core/blob/a679d93a71d4aee20597f251407f6eda195e9b9c/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Application/Auditing/AuditLogAppService.cs#L98

  • 0
    wcdagency created

    hello alper

    _courseRepository.GetAll().Where(x=>x.Name == "Test").FirstOrDefault();

    .GetAll() returns IEnumerable or IQueryable, not .tolist() right? if so , .tolist() will very slow down the system .

  • 0
    maliming created

    GetAll returns IQueryablee, which is a delayed query.

    I don't know why it causes performance problems. It doesn't query all entities into memory and then filter.

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/query-execution

  • 0
    alper created

    .GetAll() returns IQueryable