0

I can't seem to find a good way to create separate Service Layer and Repository Layer assemblies while using ASP.NET Core applications. I am trying to do it with EntityFrameworkCore and Azure Storage. I have found a few workarounds where they use .Net Core apps as assemblies but it cause all kinds of other issues for me as well.

Any help would be greatly appreciated.

Daniel Jerome
  • 301
  • 1
  • 3
  • 13
  • You didn't write what's the issue. It's no way different than with any other framework or stack (except for the limitations of `dotnet`/Powershell commands only working when its an application) – Tseng Feb 27 '17 at 17:49

2 Answers2

4

Setting up a multi layer Application.

A few things to keep in mind:

  • The power lays in abstraction, being able to easily replace layers.
  • A layer/Project has dependencies which can not have circular dependencies!

say your main project has Db models and controllers, and DataAccessLayer has the services. then the DataAccessLayer needs the models from the main project and the Controller needs the services from the DataAccessLayer and thus creating a circular dependency.

Project structure

A simple abstract setup can be:

  • DbModels project (containing only plain simple objects equal to database tables)
  • IDataAccess project contain all the interfaces of the DataAcess services. (needs dependency on DbModels)
  • DataAccessLayer project, implementation of IDataAccess. dependencies: IDataAccess & DbModels
  • Toplayer project: contains the webApi/controllers. Dependencies: IDataAccess,DbModels and an implementation of IDataAccess, in this case DataAccessLayer.

The DataAccessLayer will ofcourse need EntityFramework as a reference. Also the DatabaseContext should be in this project (and the database migrations).

Also make sure to use the DI/IOC container to register and resolve the services.

This will be your setup for a repository project.

what other problems you run into?

why a repository pattern:

repository pattern Credits to microsoft

As you can see in this image. A repository pattern is made to being able to easily switch Database.

If you are never going to switch database or use unit tests on dataAccess layer, it't basically not worth it to implement a repository pattern. (too much work, vs the benefit)

Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
  • Wow, not even close to what I was asking for. I'm asking for a solid solution of using .NET Core class libraries to do multi tier apps using Entity Core and Azure storage. You can't even properly install the frameworks into the class libraries so that you can access the needed objects. I know how to do multi tier enterprise class applications. I eat and breath the "Gang of Four" type stuff all day long. This would be a great answer for somebody asking how for a multi tier app how to on anything but .net core. – Daniel Jerome Mar 09 '17 at 23:40
  • 1
    @DanielJerome sorry for mis interpreting your question but it seems like you want a multi tier application? `create separate Service Layer and Repository Layer assemblies`. Are you then unable to create class libraries in dotnet core? or what is your real problem? – Joel Harkes Mar 10 '17 at 08:47
0

I'm not sure what issue you're encountering, but here is a good multi-project starter solution for ASP.NET Core web applications, broken up into projects by responsibility:

  • Core (domain model, abstractions, services)
  • Infrastructure (implementation-specific classes)
  • UI (asp.net core mvc web project)

https://github.com/ardalis/cleanarchitecture

Does that help?

ssmith
  • 8,092
  • 6
  • 52
  • 93
  • Thanx, I'll look into this as soon as I get back to my Desktop w/ VS 2017. I spent the last two days listening to Microsoft lie during the launch event. They kept repeating that there is no problem with running VS 2015 and VS 2017. They need a BS button on their Channel 9 player app. – Daniel Jerome Mar 10 '17 at 00:07