ddeamaral
ddeamaral

Reputation: 1443

What are the references in an onion architecture supposed to look like

I've followed the msdn document on the onion architecture in .net core here, but it seems you can't really have the UI only know about the service layer, without duplicating code. On the last part of the article (The UI), the startup file is accessing the repo. Is this logical? If so why?

Upvotes: 0

Views: 343

Answers (1)

Win
Win

Reputation: 62290

On the last part of the article (The UI), the startup file is accessing the repo. Is this logical? If so why?

I believe you are referring the following code -

services.AddDbContext<ApplicationContext>(options => 
  options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddTransient<IUserService, UserService>();
services.AddTransient<IUserProfileService, UserProfileService>();

It is normal for UI Project to reference other projects, and register dependencies in IoC container, because Composition Root should be placed as close to the application's entry point as possible.

var userService = new UserService();

If you instantiate UserService using new keyword in UI, they become tightly coupled - a change in one class forces a change in another.

IoC container solves the dependencies issue by resolving dependencies at runtime and pass them as needed.

If you want to know more about DI, you might want to read Dependency Injection in .NET by Mark Seemann and Adaptive Code via C# by Gary McLean Hall

Upvotes: 3

Related Questions