locked
DB Context Approach RRS feed

  • Question

  • User-1256377279 posted

    Hi All,

    We are planning to create Angular .NET core project, <g class="gr_ gr_107 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="107" data-gr-id="107">so <g class="gr_ gr_112 gr-alert gr_tiny gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling multiReplace" id="112" data-gr-id="112">i</g></g> am unable to decide what will be <g class="gr_ gr_160 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="160" data-gr-id="160">best</g> approach to implement DB Context below is my current.

    But <g class="gr_ gr_246 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="246" data-gr-id="246">i</g> invite everyone to share <g class="gr_ gr_265 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="265" data-gr-id="265">thier</g> 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

    Thursday, July 5, 2018 11:38 AM

All replies

  • User1168443798 posted

    Hi shabbier_215,

    I think it is enough to implement your current project structure with different layers.

    If your project is complex and you may consider one more layer by using Repository to define the generic DbContext operation.

    Best Regards,

    Edward

    Friday, July 6, 2018 5:35 AM
  • User-1256377279 posted

    <g class="gr_ gr_35 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="35" data-gr-id="35">Thanks</g> Edward for your comments, Is possible to share some example about repository concept.

    Regards,

    Shabbir

    Monday, July 9, 2018 9:53 AM