Reputation: 69
I want to know where to give database connectivity configuration in clean architecture dot net 7 project? I created 3 layers and Asp.net web api project in dot net 7.Ususally i will write connection string in appsettings.json file and will do the CORS and database configuration in program.cs file .Then will create one class file for dbcontext and call that context class file in controller to get the data from database.But in this clean architecture the logic is written in AuthorRepository class file under AuthorInfrastructure layer to get the list and all written in .Now my doubt is how to call the db context in AuthorRepository file to get the data from database.For sample i hardcode the list.Please clear my doubts.Thanks
Layers
1)Author.Domain 2)Author.Application 3)Author. Infrastructure
Author Domain Layer
In Author domain layer i created AuthorViewModel class
AuthorViewModel
public class AuthorViewModel
{
[Key]
[Column("UID")]
public int AuthorUID { get; set; }
[Required]
public String AuthorFirstName { get; set; }
[Required]
public String AuthorLastName { get; set; }
public int PublisherID { get; set; }
}
Author Application Layer
In application layer i created one class and two interface
1)AuthorService 2)IAuthorRepository 3)IAuthorService
1)AuthorRepository
public interface IAuthorRepository
{
List<Domain.AuthorViewModel> GetAllAuthor();
}
2)IAuthorService
public interface IAuthorService
{
List<Domain.AuthorViewModel> GetAllAuthor();
}
3)AuthorService
public class AuthorService : IAuthorService
{
private readonly IAuthorRepository authorRepository;
public AuthorService(IAuthorRepository authorRepository)
{
this.authorRepository = authorRepository;
}
List<Domain.AuthorViewModel> IAuthorService.GetAllAuthor()
{
return this.authorRepository.GetAllAuthor();
}
}
Author Infrastructure Layer
I created AuthorRepository class in AuthorInfrastructure layer
AuthorRepositiry
public class AuthorRepository : IAuthorRepository
{
public static List<Domain.AuthorViewModel> lstAuthor = new List<Domain.AuthorViewModel>()
{
new Domain.AuthorViewModel{ AuthorUID =1 ,AuthorFirstName= "Kirtesh Shah", AuthorLastName ="G" },
new Domain.AuthorViewModel{ AuthorUID =2 ,AuthorFirstName= "Mahesh Shah", AuthorLastName ="S" },
new Domain.AuthorViewModel{ AuthorUID =3 ,AuthorFirstName= "Nitya Shah", AuthorLastName ="G" },
new Domain.AuthorViewModel{ AuthorUID =4 ,AuthorFirstName= "Dilip Shah", AuthorLastName ="S" },
new Domain.AuthorViewModel{ AuthorUID =5 ,AuthorFirstName= "Hansa Shah", AuthorLastName ="S" },
new Domain.AuthorViewModel{ AuthorUID =6 ,AuthorFirstName= "Mita Shah", AuthorLastName ="G" }
};
public List<Domain.AuthorViewModel> GetAllAuthor()
{
return lstAuthor;
}
}
Finally api controller
AuthorController
[Route("api/[controller]/[action]")] [ApiController] public class AuthorController : ControllerBase {
private readonly IAuthorService authorService;
public AuthorController(IAuthorService authorService)
{
this.authorService = authorService;
}
// GET: api/<MembersController>
[HttpGet]
public ActionResult<IList<AuthorViewModel>> Get()
{
return Ok(this.authorService.GetAllAuthor());
}
}
Upvotes: 0
Views: 1045
Reputation: 11
Hopefully not necro-ing, was looking for an answer for the exact same problem and this is what ended up doing.
In dependency injection point,
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration databaseConfiguration)
{
var cs = databaseConfiguration.GetConnectionString("DefaultConnection");
services.AddDbContext<ApplicationDbContext>(opt => opt.UseSqlServer(cs));
}
from the entry point program or startup class, the call looks like
builder.Services.AddInfrastructure(builder.Configuration);
Upvotes: 1
Reputation: 21
In your IoC layer, add:
services.AddDbContext<yourDbContextName>(options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"))
);
of for Lazy Loading:
services.AddDbContext<yourDbContextName>(options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")).UseLazyLoadingProxies()
);
Upvotes: 1