Gia*_*rra 20 c# asp.net-mvc repository-pattern asp.net-core-mvc
作为ASP.NET Core 1.0 MVC的新手,我决定将一个Repository Pattern用于MVC Core应用程序; 我正在使用SQL DB作为数据层SampleDbContext,我希望为我的一些业务实体提供一个Repository类.到目前为止,我已经做在下面startup.cs,CustomerController.cs和CustomerRepository.cs文件,其中一个样本实体是"客户".
在ConfigureServices启动类的方法中:
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<SampleDbContext>(options =>
       options.UseSqlServer(Configuration.GetConnectionString("SampleDB")));
}
在控制器中:
public class CustomerController : Controller
{
    private SampleDBContext _context;
    private CustomerRepository = new CustomerRepository (new SampleDBContext());
    public CustomerController(SampleDBContext context)
    {
        _context = context;
    }
}
在存储库中:
public class CustomerRepository
{
    private SampleDBContext _context;
    public CustomerRepository(SampleDBContext context)
    {
        _context = context;
    }
}
通过这种设计,我在一次插入SampleDbContext服务startup.cs,然后为每个Controller(接收依赖注入)我实例化一个相应的Repository传递的新实例SampleDbContext.这种重复的DB上下文实例是多用户环境的良好设计吗?我想我可以将每个Repository添加为服务startup.cs但是看起来不太好.请告诉我一个好的设计实现我的情况,或者如果我迷路了就把我放在正确的轨道上.
Ale*_*xan 29
您可以看到如何使用存储库模式的简单示例:
您创建存储库接口:
using System.Collections.Generic;
namespace TodoApi.Models
{
    public interface ITodoRepository
    {
        void Add(TodoItem item);
        IEnumerable<TodoItem> GetAll();
        TodoItem Find(long key);
        void Remove(long key);
        void Update(TodoItem item);
    }
}
然后实现它:
using System;
using System.Collections.Generic;
using System.Linq;
namespace TodoApi.Models
{
    public class TodoRepository : ITodoRepository
    {
        private readonly TodoContext _context;
        public TodoRepository(TodoContext context)
        {
            _context = context;
            Add(new TodoItem { Name = "Item1" });
        }
        public IEnumerable<TodoItem> GetAll()
        {
            return _context.TodoItems.ToList();
        }
        public void Add(TodoItem item)
        {
            _context.TodoItems.Add(item);
            _context.SaveChanges();
        }
        public TodoItem Find(long key)
        {
            return _context.TodoItems.FirstOrDefault(t => t.Key == key);
        }
        public void Remove(long key)
        {
            var entity = _context.TodoItems.First(t => t.Key == key);
            _context.TodoItems.Remove(entity);
            _context.SaveChanges();
        }
        public void Update(TodoItem item)
        {
            _context.TodoItems.Update(item);
            _context.SaveChanges();
        }
    }
}
然后在ConfigureServices中注册:
services.AddSingleton<ITodoRepository, TodoRepository>();
然后将其注入Controller:
namespace TodoApi.Controllers
{
    [Route("api/[controller]")]
    public class TodoController : Controller
    {
        public TodoController(ITodoRepository todoItems)
        {
            TodoItems = todoItems;
        }
        public ITodoRepository TodoItems { get; set; }
    }
}
| 归档时间: | 
 | 
| 查看次数: | 31067 次 | 
| 最近记录: |