Hi All,
We are planning to create Angular .NET core project, so i am unable to decide what will be best approach to implement DB Context below is my current.
But i invite everyone to share thier best experience.
Dal
public partial class ANGWorksContext : DbContext
{
//public ANGWorksContext()
//{
//}
public ANGWorksContext(DbContextOptions<ANGWorksContext> options)
: base(options)
{
}
public virtual DbSet<Dept> Dept { get; set; }BAL
public class DeptService : IDeptService
{
//private ANGWorksContext ANGDB = new ANGWorksContext();
private readonly ANGWorksContext _context;
public DeptService(ANGWorksContext context)
{
_context = context;
}
public List<Dept_View> GetDepts()
{
//AutoMapper for Multiple Initialiser Generic
var dept = Mapper.Map<List<Dept_View>>(GetDepartment());
//Individual Mapper
//var dept = Mapper.Map<List<Dept>, List<Dept_View>>(GetDepartment());
return dept;
}
public List<Dept> GetDepartment()
{
try
{
var dept = from d in _context.Dept select d;
return dept.ToList();
}
catch (Exception)
{
throw;
}
//finally
//{
// _context.Dispose();
//}
}
}COntroller
namespace AngCoreData.Controllers
{
public class DeptController : Controller
{
//DeptService objDeptService = new DeptService();
private IDeptService _deptservice;
public DeptController(IDeptService deptService)
{
_deptservice = deptService;
}
[HttpGet]
[Route("api/Dept/Index")]
public IEnumerable<Dept_View> Index()
{
var d = _deptservice.GetDepts();
return d.ToList();
//var dept = objDeptService.GetDepts();
//return dept;
}
}
}Startup.CS
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AngDalLib.Models.ANGWorksContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ANGWorksContext")));
services.AddMvc();
Mapper.Initialize(c => c.AddProfile<MapperProfile>());
services.AddTransient<IDeptService, DeptService>();
}Many Thanks,
Shabb