数据模型
数据模型的基本概念什么是模型(Model)?
简单来说,模型就是一个类,这个类中包含各种各样的数据属性,这些数据能够映射数据库,从而使得程序员能够把数据库中相对孤立的数据串连起来,形成数据对象链。从而使用面对对象的方法来写数据。
从用途来说,模型的主要操作为获取数据、更新数据、传递数据、保存数据等。从系统的职责来看,模型主要来处理业务逻辑,视为业务层。ViewModel能够直接与视图数据进行绑定,甚至可以再视图上做数据验证。不过,Model的定义并不明确,在工业界主要是经验为主的开发,可以是一种简单的POCO类,也有可能是一种复杂的充血型领域模型(Rich Domain Model)。
以下代码示例为一个Model的基本代码:
public class User //模型属性
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get { return FirstName + LastName; } }
public string ShortDescription { get; set; }
public decimal Phone { get; set; }
public string Email { get; set; }
public bool IsEmailConfirm { get; set; }
public bool isTokenValid { get; set; }
public void ConfirmEmail(string token) //业务逻辑
{
if (isTokenValid)
{
this.IsEmailConfirm = true;
//.......之后保存到数据库的操作
}
}
}
数据仓库的基本用法
如何从数据库中获取数据呢?在过去,我们通常使用JDBC、Ado.Net、ORM。而现在,主流的数据持久化模式为repository仓库模式,它能帮助我们不用关注数据是如何保存的,甚至是数据的保存形式我们也能忽略。通过仓库模式,可以把数据映射成对象的属性,再进行业务逻辑的一系列操作。
仓库模式的优势:
1、业务逻辑与数据模型紧密耦合,减少分层、降低代码数量
2、完全剥离数据库业务,coder能够更加专注于实现业务逻辑。
3、面向对象编程,更符合人的思路。
interface INoodleRepository //兰州拉面仓库
{
IEnumerable<Noodle> GetAllNoodles();
Noodle GetNoodleById(int Id);
}
为了模拟数据库的数据,我们可以再Models文件下添加一个模拟的假数据文件MockNoodleRepository.cs:
public class MockNoodleRepository : INoodleRepository
{
private List<Noodle> _noodles; //使用一个列表保存数据
public MockNoodleRepository()
{
if (_noodles == null)
{
InitializeNoodle();
}
}
private void InitializeNoodle()
{
_noodles = new List<Noodle> //添加假数据
{
new Noodle { Id = 1, Name = "毛细", Price = 12, ShortDescription = "如发丝般细", LongDescription = "真的好细好细好细啊", ImageUrl="/images/毛细.png"},
new Noodle { Id = 2, Name = "细", Price = 10, ShortDescription = "普通细", LongDescription = "还是挺细的", ImageUrl="/images/细.png"},
new Noodle { Id = 3, Name = "三细", Price = 11, ShortDescription = "有点粗了", LongDescription = "比细的粗点,比二细细点", ImageUrl="/images/三细.png"},
new Noodle { Id = 4, Name = "二细", Price = 10, ShortDescription = "粗了", LongDescription = "粗的才有嚼劲", ImageUrl="/images/二细.png"},
new Noodle { Id = 5, Name = "二柱子", Price = 11, ShortDescription = "太粗了", LongDescription = "粗得快咬不动了", ImageUrl="/images/二柱子.png"},
new Noodle { Id = 6, Name = "韭叶子", Price = 12, ShortDescription = "扁的", LongDescription = "韭猜叶一样宽", ImageUrl="/images/韭叶子.png"},
new Noodle { Id = 7, Name = "薄宽", Price = 11, ShortDescription = "开始宽了", LongDescription = "比韭叶宽一点,比大宽窄一点", ImageUrl="/images/薄宽.png"},
new Noodle { Id = 8, Name = "大宽", Price = 10, ShortDescription = "裤带面", LongDescription = "比嘴还宽了", ImageUrl="/images/大宽.png"},
new Noodle { Id = 9, Name = "荞麦棱子", Price = 15, ShortDescription = "立方体的", LongDescription = "好像知道师傅怎么拉出来的", ImageUrl="/images/荞麦棱子.png"},
new Noodle { Id = 10, Name = "一窝丝", Price = 15, ShortDescription = "这是啥", LongDescription = "我也没吃过", ImageUrl="/images/一窝丝.png"}
};
}
public IEnumerable<Noodle> GetAllNoodles() //实现仓库中的方法
{
return _noodles;
}
public Noodle GetNoodleById(int id)
{
return _noodles.FirstOrDefault(n => n.Id == id);
}
}
在HomeController中添加仓库:
private INoodleRepository _noodleRepository;
private IFeedbackRepository _feedbackRepository;
public HomeController(INoodleRepository noodleRepository,IFeedbackRepository feedbackRepository)
{
_noodleRepository = noodleRepository;
_feedbackRepository = feedbackRepository;
}
将数据传到viewmodel中:
public IActionResult Index()
{
var viewModel = new HomeViewModel()
{
Feedbacks = _feedbackRepository.GetAllFeedbacks().ToList(),
Noodles = _noodleRepository.GetAllNoodles().ToList()
};
return View(viewModel);
在Startup.cs中注入仓库,这里我们使用services.AddTransient添加事务的方法进行注入:
...
services.AddTransient<INoodleRepository,MockNoodleRepository>();
...
ok,点击运行项目,在页面上显示: