报错了,IWorkContext(工作上下文,用于获取用户信息等数据)依赖Domain,删除它。这个过程可能要删除不少文件,直到项目不再报错。完成后我们的项目结构如下,注意我们将Nop.Core中的实体基类移到了Nop.Domain中,到这一步,我们的基础框架结构已经大致出来了。
三、添加数据库、数据实体、映射、业务层代码
1. 在本地Sqlserver中,新建数据库MyProject,添加表Test。
USE [MyProject] GO /****** Object: Table [dbo].[Test] Script Date: 05/24/2017 23:51:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Test]( [Id] [int] NOT NULL, [Name] [nvarchar](50) NOT NULL, [Description] [nvarchar](200) NULL, [CreateDate] [datetime] NULL, CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
2. 添加实体类和映射。在Domain项目下面新建Test目录,添加TestEntity。Data项目Mapping下新建Test目录,添加EF映射类。
public class TestEntity: BaseEntity { public virtual string Name { get; set; } public virtual string Description { get; set; } public virtual DateTime? CreateDate { get; set; } }
3. 添加业务层方法。
在Nop.Services项目里,在我们之前添加的接口和类下面添加几个常用的CURD方法,并实现它。这样我们就已经实现的业务层的代码了。
/// <summary> /// Test service interface /// </summary> public partial interface ITestService { /// <summary> /// Gets all tests /// </summary> /// <returns>Tests</returns> IList<TestEntity> GetAllTests(); /// <summary> /// Gets a test /// </summary> /// <param>The test identifier</param> /// <returns>Test</returns> TestEntity GetTestById(int testId); /// <summary> /// Inserts a test /// </summary> /// <param>Test</param> void InsertTest(TestEntity test); /// <summary> /// Updates the test /// </summary> /// <param>Test</param> void UpdateTest(TestEntity test); /// <summary> /// Deletes a test /// </summary> /// <param>Test</param> void DeleteTest(TestEntity test); }
/// <summary> /// Test service /// </summary> public partial class TestService : ITestService { #region Constants #endregion #region Fields private readonly IRepository<TestEntity> _testRepository; #endregion #region Ctor public TestService(IRepository<TestEntity> testRepository) { this._testRepository = testRepository; } #endregion #region Methods /// <summary> /// Gets all tests /// </summary> /// <returns>Tests</returns> public virtual IList<TestEntity> GetAllTests() { return _testRepository.Table.Where(p => p.Name != null).ToList(); } /// <summary> /// Gets a topic /// </summary> /// <param>The test identifier</param> /// <returns>Test</returns> public virtual TestEntity GetTestById(int testId) { if (testId == 0) return null; return _testRepository.GetById(testId); } /// <summary> /// Inserts a test /// </summary> /// <param>Test</param> public virtual void InsertTest(TestEntity test) { if (test == null) throw new ArgumentNullException("test"); _testRepository.Insert(test); } /// <summary> /// Updates the test /// </summary> /// <param>Test</param> public virtual void UpdateTest(TestEntity test) { if (test == null) throw new ArgumentNullException("test"); _testRepository.Update(test); } /// <summary> /// Deletes a test /// </summary> /// <param>Test</param> public virtual void DeleteTest(TestEntity test) { if (test == null) throw new ArgumentNullException("test"); _testRepository.Delete(test); } #endregion }
四、添加Presentation项目
有了业务服务,现在可以添加表现层项目来测试了。为什么不直接写测试项目?因为测试项目使用Mock模拟数据,不能完整展示整个功能。
1. 添加mvc模板项目,通过nuget引入Autofac和Autofac.Mvc5。
2. 添加容器注册类DependencyRegistrar,实现IDependencyRegistrar接口,这一步非常关键,我们将要用的接口和实现类注入到容器中。