Nop Commerce3.1 中新增一个Domain Model 最详细代码及解释

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lanximu/article/details/17262255

先贴上转来的步骤:

在NopCommerce中新增一个Domain Model,需要以下几个步骤:

1. 新建一个Entity Class (Nop/Core/Domain/Entity.cs)

2. 新建一个Mapping Class (Nop/Data/Mapping/EntityMap.cs)

3. 新建一个View Model (Nop/Admin/Models/EntityModel.cs 或 Nop/Web/Models/EntityModel.cs)

4. 新建Model Validator (Nop/Admin/Validators/EntityValidator.cs 或 Nop/Web/Validators/EntityValidator.cs)

5. 为AutoMapper新建映射配置,用来完成Model和Entity之间的转换 (Nop/Admin/Infrastructure/AutoMapperStartupTask.cs 或 Nop/Web/Infrastructure/AutoMapperStartupTask.cs)

6. 编写ToModel和ToEntity (Nop/Admin/MappingExtensions.cs 或 Nop/Web/MappingExtensions.cs)

7. 创建Service和Service Interface (Nop/Services/EntityService.cs 和 Nop/Services/IEntityService.cs)

8. 最后新建的Model创建Controller和View

PS: NopCommerce不支持database migration,需要手动更新数据库。

其中,5,6步骤采用另一种方式 即不用autoMapper 而是在controller中手动写(我就是这么做的,虽然繁琐了些,但刚开始研究nop,踏实,且实现结果一样)

重:除了上面几步,还有非常重要的一步:即:在nop.Web.Framework中进行Service的注册。

最后我们达到的结果如下:(仅展示后台,前台逻辑相同)





下面我一步一步结合代码讲述:

1.Entity class: Nop.Core.Domian/Jingdian/JingdianSpot.cs

即数据库实体类,属性和数据库表里是一样的,如果继承BaseEntity,则不用额外写Id这个属性了:

namespace Nop.Core.Domain.Jingdian
{

    public partial class JingdianSpot:BaseEntity
    {
             
        public string title { get; set; }
        
        public string tbimg { get; set; }
        
        public string ch { get; set; }
        
        public double x { get; set; }
        
        public double y { get; set; }
        
        public string intro { get; set; }
        
        public double runtime { get; set; }
        
        public string start_time { get; set; }
        
        public string address { get; set; }
    }
}

2.既然实体类写好了,就顺便把数据库建了,别忘了标识主键为true,即自增:

3.Nop.Data.Mapping/Jingdian/JingdianSpotMap.cs

这里作entity到数据库表的映射,表明和数据库里的表名一致就行了

namespace Nop.Data.Mapping.Jingdian
{
    public partial class JingdianSpotMap : EntityTypeConfiguration<JingdianSpot>
    {
        public JingdianSpotMap()
        {
            this.ToTable("JingdianSpot");
            this.HasKey(pr=>pr.Id);
        }
    }
}

4. Nop.Services/Jingdian/IJingdianService和JingdianService:它们是数据库相关操作的类,

其中JingdianService 用到了IRepository, IRespository和EfRespository中的方法是可以扩展的,像我后来就写了一个获取刚插入数据库后Id的方法

IJingdianService.cs:

namespace Nop.Services.Jingdian
{
    public partial interface IJingdianService
    {

        JingdianSpot GetJingdianById(int jingdianId);
         
        void DeleteJingdian(JingdianSpot jingdianSpot);

        IPagedList<JingdianSpot> GetAllJingdian(int pageIndex, int pageSize, bool showHidden = false);

        List<JingdianSpot> GetAllJingdian();

        List<JingdianSpot> GetJingdianBych(string ch);

        void InsertJingdian(JingdianSpot jingdian);

        void UpdateJingdian(JingdianSpot jingdian);
    }
}

JingdianService.cs:

namespace Nop.Services.Jingdian
{
    public partial class JingdianService : IJingdianService
    {
        #region Fields

        private readonly IRepository<JingdianSpot> _JingdianSpotRepository;
        private readonly IEventPublisher _eventPublisher;

        #endregion

        #region Ctor

        public JingdianService(IRepository<JingdianSpot> JingdianSpotRepository,
            IEventPublisher eventPublisher)
        {
            this._JingdianSpotRepository = JingdianSpotRepository;
            this._eventPublisher = eventPublisher;
        }

        #endregion

        #region Methods

        
        public virtual JingdianSpot GetJingdianById(int JingdianId)
        {
            if (JingdianId == 0)
                return null;

            return _JingdianSpotRepository.GetById(JingdianId);
        }


        public virtual void DeleteJingdian(JingdianSpot JingdianSpot)
        {
            if (JingdianSpot == null)
                throw new ArgumentNullException("newsItem");

            _JingdianSpotRepository.Delete(JingdianSpot);
            //event notification
            _eventPublisher.EntityDeleted(JingdianSpot);
        }



        public virtual IPagedList<JingdianSpot> GetAllJingdian(int pageIndex, int pageSize, bool showHidden = false)
        {
            var query = _JingdianSpotRepository.Table;
            query = query.OrderByDescending(a => a.Id);

            var Jingdian = new PagedList<JingdianSpot>(query, pageIndex, pageSize);
            return Jingdian;
        }

        public virtual List<JingdianSpot> GetAllJingdian()
        {
            var query = _JingdianSpotRepository.Table;
                query = query.OrderBy(a=>a.Id);

               /* var query = from c in _blogCommentRepository.Table
                            orderby c.CreatedOnUtc
                            where (customerId == 0 || c.CustomerId == customerId)
                            select c;
                */
                var content = query.ToList();
                return content;
        }

        public virtual List<JingdianSpot> GetJingdianBych(string ch)
        {
            var query = from c in _JingdianSpotRepository.Table
                        orderby c.Id
                        where (c.ch == ch)
                        select c;
            var content = query.ToList();
            return content;
        }

        public virtual void InsertJingdian(JingdianSpot Jingdian)
        {
            if (Jingdian == null)
                throw new ArgumentNullException("news");

            _JingdianSpotRepository.Insert(Jingdian);

            //event notification
            _eventPublisher.EntityInserted(Jingdian);
        }


        public virtual void UpdateJingdian(JingdianSpot Jingdian)
        {
            if (Jingdian == null)
                throw new ArgumentNullException("news");

            _JingdianSpotRepository.Update(Jingdian);

            //event notification
            _eventPublisher.EntityUpdated(Jingdian);
        }

        #endregion

    }

5.Nop.Admin.Models/Jingdian/JingdianSpotModel.cs

这是View model, 即显示到页面上的属性都有哪些

namespace Nop.Admin.Models.Jingdian
{
    
    public partial class JingdianSpotModel : BaseNopEntityModel
    {
        [NopResourceDisplayName("Admin.Jingdian.Fields.title")]
        public string title { get; set; }

        [NopResourceDisplayName("Admin.Jingdian.Fields.tbimg")]
        public string tbimg { get; set; }

        [NopResourceDisplayName("Admin.Jingdian.Fields.ch")]
        public string ch { get; set; }

        [NopResourceDisplayName("Admin.Jingdian.Fields.x")]
        public double x { get; set; }

        [NopResourceDisplayName("Admin.Jingdian.Fields.y")]
        public double y { get; set; }

        [NopResourceDisplayName("Admin.Jingdian.Fields.intro")]
        public string intro { get; set; }

        [NopResourceDisplayName("Admin.Jingdian.Fields.runtime")]
        public double runtime { get; set; }

        [NopResourceDisplayName("Admin.Jingdian.Fields.start_time")]
        public string start_time { get; set; }

        [NopResourceDisplayName("Admin.Jingdian.Fields.address")]
        public string address { get; set; }

    }

6. Nop.Admin.Validators/Jingdian/JingdianSpotValidators.cs

验证类:

namespace Nop.Admin.Validators.Jingdian
{
    public class JingdianSpotValidator:AbstractValidator<JingdianSpotModel>
    {
        public JingdianSpotValidator(ILocalizationService localizationService)
        {
            RuleFor(x => x.title)
                .NotNull()
                .WithMessage(localizationService.GetResource("Admin.ContentManagement.News.NewsItems.Fields.Title.Required"));

            RuleFor(x => x.tbimg)
                .NotNull()
                .WithMessage(localizationService.GetResource("Admin.ContentManagement.News.NewsItems.Fields.Short.Required"));
            RuleFor(x => x.intro)
                .NotNull()
                .WithMessage(localizationService.GetResource("Admin.ContentManagement.News.NewsItems.Fields.Full.Required"));
        }   
    }
}

7. Nop.Admin.Infrastructure/AutoMapperStartupTask.cs:

作entity和model之间的映射,可以设置相关条件进行映射限制

 //Jingdian
            Mapper.CreateMap<JingdianSpot, JingdianSpotModel>();
            Mapper.CreateMap<JingdianSpotModel, JingdianSpot>();

8.Nop.Admin/MappingExtensions.cs

  #region Jingdian
        //JingdianSpot
        public static JingdianSpotModel ToModel(this JingdianSpot entity)
        {
            return Mapper.Map<JingdianSpot, JingdianSpotModel>(entity);
        }

        public static JingdianSpot ToEntity(this JingdianSpotModel model)
        {
            return Mapper.Map<JingdianSpotModel, JingdianSpot>(model);
        }

        public static JingdianSpot ToEntity(this JingdianSpotModel model, JingdianSpot destination)
        {
            return Mapper.Map(model, destination);
        }

        #endregion

9. Nop.Web.FrameWork/ DependencyRegistrar.cs:

builder.RegisterType<JingdianService>().As<IJingdianService>().InstancePerHttpRequest();

10.自己写controller和View (随附件代码奉上)

注:若想改变后台菜单,在Nop.Admin:sitemap.config可以改变。

附件:上述各文件代码含:controller和View的代码:

http://www.t00y.com/file/44306373


展开阅读全文

没有更多推荐了,返回首页