Base solution for your next web application
Open Closed

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


User avatar
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)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @wcdagency

    Could you ask your question in English ? If not, please contact [email protected] for futher assitance.

    Thanks,

  • User Avatar
    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?

  • User Avatar
    0
    alper created
    Support Team

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

  • User Avatar
    0
    musa.demir 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

  • User Avatar
    0
    wcdagency created

    So;

    I could use this approach no problem ,right?

  • User Avatar
    0
    maliming created
    Support Team

    You need to get the DbContext using the IDbContextProvider interface.

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

  • User Avatar
    0
    alper created
    Support Team

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

  • User Avatar
    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.

  • User Avatar
    0
    alper created
    Support Team

    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

  • User Avatar
    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 .

  • User Avatar
    0
    maliming created
    Support Team

    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

  • User Avatar
    0
    alper created
    Support Team

    .GetAll() returns IQueryable