I'm quite new in design pattern and asp.net core. I am starting my side project and I've tried to come up with the design pattern which I think is easy to maintain for me in future. I have read stackoverflow questions and other articles but I am not sure it's the correct way to implement in the framework I've created. I have created 3 class libraries (Model, Service, Data) and one main web api project. The flow is
Main Project = reference to => Service Class Library
Service Class Library = reference to => Data Class Library
Model Class Library will be referenced from other projects
My goal is that Main project will be only exposed to Service Layer which all business logic will be implemented inside it and main project won't even need to know what are the functions for Data Layer. I have implemented as below:
Service Layer
UserService.cs
public class UserService
{
private readonly IUserFactory userFactory;
public UserService(IUserFactory userFactory)
{
this.userFactory = userFactory;
}
public void GetUsers()
{
this.userFactory.GetUserRepository();
}
}
IUserFactory.cs
public interface IUserFactory
{
void GetUserRepository();
}
UserFactory.cs
public class UserFactory : IUserFactory
{
private readonly IUserRepository userRepository;
public UserFactory()
{
userRepository = new UserRepository();
}
public void GetUserRepository()
{
this.userRepository.GetUsers();
}
}
Data Layer
IUserRepository.cs
public interface IUserRepository
{
void GetUsers();
}
UserRepository.cs
public class UserRepository : IUserRepository
{
private readonly ApiDbContext context;
public UserRepository(ApiDbContext context)
{
this.context = context;
}
public UserRepository()
{
if (this.context == null)
{
this.context = new ApiDbContext();
}
}
public void GetUsers()
{
var flag = this.context.Database.CanConnect();
var userList = this.context.Users.ToList();
var count = userList.Count;
}
}
Model Layer
ApiDbContext.cs
public class ApiDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public LifeTrackerApiDbContext(DbContextOptions<ApiDbContext> options) : base(options)
{
}
public ApiDbContext() : base(GetOptions("LOCAL"))
{
}
private static DbContextOptions GetOptions(string connectionStringName)
{
var builder = new ConfigurationBuilder().AddJsonFile("Base/connectionStrings.json", false);
var configuration = builder.Build();
var connectionString = configuration.GetConnectionString(connectionStringName);
return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
Main Project
UsersController.cs
[Route("api/[controller]")]
[ApiController]
[Produces("application/json")]
public class UsersController : ControllerBase
{
private readonly UserService userService;
public UsersController(UserService userService)
{
this.userService = userService;
}
[HttpGet]
public IEnumerable<User> GetUserList()
{
this.userService.GetUsers();
return new List<User>();
}
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(logging =>
{
logging.AddConfiguration(Configuration.GetSection("Logging"));
logging.AddFile(o => o.RootPath = Directory.GetParent(AppContext.BaseDirectory).Parent.FullName);
}
);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddScoped(typeof(UserService));
services.AddScoped<IUserFactory, UserFactory>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(allowAllPolicy);
app.UseHttpsRedirection();
app.UseMvc();
}
}
Is this correct way to implement the design pattern? I feel like the way I have created for ApiDbContext is kind of wrong or feel like something missing for it.
I have referenced from:
C# Service Layer Design Pattern
Dependency Injection and project references
Aucun commentaire:
Enregistrer un commentaire