ASP.NET MVC 开源项目学习之ProDinner (一)

时间:2023-03-09 02:48:25
ASP.NET MVC 开源项目学习之ProDinner (一)

首先在github上面将ProDinner项目通过 Git Bash 克隆到本地,接下来我们开始分析这个项目吧~

系统采用.Net 4.5, Asp.net Mvc 5,VS2012,Sql server,系统的整体设计非常轻量级,却做到了整体架构分层明显,模块耦合度低的架构思想,很适合新手学习.

ASP.NET MVC 开源项目学习之ProDinner (一)

Core层实现了本地化Model和EF模型化需要的model数据,另外,Core层还为低耦合的业务逻辑和低耦合的数据访问做好了接口准备.

1.Model:

以下是表关系的大致内容:

ASP.NET MVC 开源项目学习之ProDinner (一)

我们在分析数据库表结构的时候,经常会发现表与表之间有很多共同的字段,例如Id,操作时间,操作人等等,所以可以为这些表构造公共基类,比如Entity就是所有表的基类.而事实上,对First Code而言,根本就不存在数据库的概念.

 public class Entity
{
public int Id { get; set; }
} public class Feedback : Entity
{
public string Comments { get; set; }
} public class Meal : DelEntity
{
public string Name { get; set; }
public string Comments { get; set; }
public virtual ICollection<Dinner> Dinners { get; set; }
public string Picture { get; set; }
} public class Role : Entity
{
public string Name { get; set; }
public virtual ICollection<User> Users { get; set; }
} public class User : DelEntity
{
public string Login { get; set; }
public string Password { get; set; }
public virtual ICollection<Role> Roles { get; set; }
} public class Dinner : DelEntity
{
public string Name { get; set; }
public int CountryId { get; set; }
public virtual Country Country { get; set; }
public int ChefId { get; set; }
public virtual Chef Chef { get; set; }
public string Address { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public virtual ICollection<Meal> Meals { get; set; }
} public class DelEntity : Entity, IDel
{
public bool IsDeleted { get; set; }
} public class Country : DelEntity
{
public string Name { get; set; }
} public class Chef : DelEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int CountryId { get; set; }
public virtual Country Country { get; set; }
} public interface IDel
{
bool IsDeleted { get; set; }
}

从代码中可以看出,涉及到表关联的时候,统一使用了ICollection<T>的集合,类型都是Virtual,这表明这一Model层都是接口和基类,会被重写.

接下来就是创建应用程序所需的业务类.

2.Repository(仓库)

 public interface IDelRepo<T>
{
IQueryable<T> Where(Expression<Func<T, bool>> predicate, bool showDeleted = false);
IQueryable<T> GetAll();
void Restore(T o);
}
public interface IRepo<T>
{
T Get(int id);
IQueryable<T> GetAll();
IQueryable<T> Where(Expression<Func<T, bool>> predicate, bool showDeleted = false);
T Insert(T o);
void Save();
void Delete(T o);
void Restore(T o);
}
public interface IUniRepo
{
T Insert<T>(T o) where T : Entity, new();
void Save();
T Get<T>(int id) where T : Entity;
IEnumerable<T> GetAll<T>() where T : Entity; }

从代码可以很明显看出,这是数据操作方式和数据业务逻辑的函数原型,其后service的所有具体实现的实体数据操作都会基于这些接口.

3.Security

引用了Asp.net的身份验证模块,故也进行了相应的接口限制

    public interface IFormsAuthentication
{
void SignIn(string userName, bool createPersistentCookie, IEnumerable<string> roles);
void SignOut();
}

4.Service

该层接口详细定义了四个类的操作接口:

ICrudService继承了上层接口的抽象接口

IMealService类继承了 ICrudService 同时,规定了图片保存的接口

IUserService继承了ICrudService,规定了用户验证的接口

5.ProDinnerException规定了系统自定义的抛错机制。

至此,Core层就介绍完了~