设为首页 加入收藏

TOP

某电商平台开发记要(二)
2019-09-17 17:48:55 】 浏览:137
Tags:电商 平台 开发
State。删除父对象时,一对一的导航实体不会自动跟着删除;若是一对多的情况,那么只要删除父对象,导航实体会自动被删除;多对多的情况未验证,因为涉及到映射表,推测会自动删除映射关系,即删除映射表里的相关记录。经测试,多对多情况,也无法自动删除。


AutoMapper

AutoMapper提供的自定义映射——Custom value resolvers 和 Projection,乍看之下似乎差不多,侧重解决点是不一样的,但是它们似乎又通用。。。在使用上,后者MapFrom方法的其中一个重载接收表达式树(Expression)类型的参数,因此涉及到稍微复杂的语句,可能出现如下图所示的情况:

这个时候只能采用前者的ResolveUsing方法了,如下:

还有个IValueResolver接口,与IMemberValueResolver的区别在于IValueResolver不指定source类的哪个属性需要转换,这就导致了转换时自定义逻辑可能要引用source类,如果其它类也有类似转换,那么就不能复用了。

6.0.1.0版本,如下写法,则只有最后一个Resolver起作用。

改成下面写法,则无问题。

 

注意到上面opt => opt.ResolveUsing<ShopGradeResolver>(),每次Mapper.Map的时候都会new一个ShopGradeResolver对象,其实是没必要的,因为只执行逻辑而状态无关。所以可改为单例模式:opt => opt.ResolveUsing(Singleton<ShopGradeResolver>.Instance)。

另,当source类有导航属性时,会在Mapper.Map时去数据库里查,因此若用不到该导航属性则应该设置映射规则时ignore之。

Mapper.Initialize调用多次,最后一次会覆盖前面的,因此如果映射是由各个项目自己处理,那么应该考虑使用Profile,然后在主项目中 Mapper.Initialize(cfg => cfg.AddProfiles(typeFinder.GetAssemblies())); AutoMapper will scan the designated assemblies for public classes inheriting from Profile and add them to the configuration. 更多请看 Configuration


Autofac

Lifetime Scope Instance Scope,我们获取实例,都要先BeginLifetimeScope(),而后根据组件注册时的InstanceScope策略,获取组件实例。InstanceScope中,InstancePerRequest在Asp.net MVC等站点开发时比较常用,即对每一请求返回同一实例,though, it’s still just instance per matching lifetime scope——MatchingScopeLifetimeTags.RequestLifetimeScopeTag,MVC中为“AutofacWebRequest”,在。注意,ASP.NET Core uses Instance Per Lifetime Scope rather than Instance Per Request. 如何在MVC中使用,请参看文档:http://docs.autofac.org/en/latest/faq/per-request-scope.html?highlight=RequestLifetimeScope#implementing-custom-per-request-semantics

It is important to always resolve services from a lifetime scope and not the root container. Due to the disposal tracking nature of lifetime scopes, if you resolve a lot of disposable components from the container (the “root lifetime scope”), you may inadvertently cause yourself a memory leak. The root container will hold references to those disposable components for as long as it lives (usually the lifetime of the application)。

Autofac主张LifetimeScope不要线程共享,否则,You can get into a bad situation where components can’t be resolved if you spawn the thread and then dispose the parent scope.即共享scope被其它线程释放导致组件无法正常获取。鉴于此,Autofac并未为多线程共享LifetimeScope提供便捷方法,若定要如此,那么只能人为处理(比如将LifetimeScope作为参数传入线程或设为全局静态变量)。

以上为4.x版本参照。


Repository模式

先来看一篇博文——博客园的大牛们,被你们害惨了,Entity Framework从来都不需要去写Repository设计模式。对于这位博友的观点,在其应用场景下我表示赞同。大部分架构和模式,都是为了达到解耦的目的,EF本身就是Repository模式实现,它让业务层与具体数据库解耦,即可较方便地切换不同数据库。那么假如说业务层需要同ORM解耦,去应对可能的ORM切换,那么我们也可以在业务层和ORM层再套一层Repository。以下为简单的实现代码:

    public partial interface IRepository<T> where T : BaseEntity
    {
        T GetById(object id);
        void Insert(T entity);
        void Insert(IEnumerable<T> entities);
        void Update(T entity);
        void Update(IEnumerable<T> entities);
        void Delete(T entity);
        void Delete(IEnumerable<T> entities);
        IQueryable<T> Table { get; }
        IQueryable<T> TableNoTracking { get; }
    }

各路ORM只要实现该接口即可,比如EF:

    public partial class EfRepository<T> : IRepository<T> where T : BaseEntity
    {
        #region Fields

        private readonly IDbContext _context;
        private IDbSet<T> _entities
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 2/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇<大话设计模式>笔记 下一篇SNF快速开发平台成长史V4.5-Sprin..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目