码迷,mamicode.com
首页 > Windows程序 > 详细

net core 3.1 webapi的开发遇到的问题

时间:2020-06-17 20:35:48      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:选择   hand   DDM   add   循环   type   sof   tst   contex   

1.用的是工厂仓储模式

public interface IRepositoryFactory
    {
        IRepository<T> CreateRepository<T>(IPreschoolEducationOnlineDBContext dataContext) where T : class;
    }
 public class RepositoryFactory : IRepositoryFactory
    {
        public IRepository<T> CreateRepository<T>(IPreschoolEducationOnlineDBContext dataContext) where T : class
        {
            return new Repository<T>(dataContext);
        }
    }
public class Repository<T> : IRepository<T> where T : class
    {
        private readonly PreschoolEducationOnlineDBContext preschoolEducationOnlineDBContext;

        private readonly DbSet<T> dbset;
        public Repository(IPreschoolEducationOnlineDBContext _preschoolEducationOnlineDBContext)
        {
            preschoolEducationOnlineDBContext = _preschoolEducationOnlineDBContext as PreschoolEducationOnlineDBContext;
            dbset = preschoolEducationOnlineDBContext.Set<T>();
        }
}

然后在startup  的ConfigureServices注册

services.AddScoped<IPreschoolEducationOnlineDBContext, PreschoolEducationOnlineDBContext>();
            services.AddScoped<IRepositoryFactory, RepositoryFactory>();

2接口的调试,用swagger插件,需要有几个地方注意

①控制器要加路由,不然不显示,方式要加http,不然报错,返回的json格式和后台定义的不一样(大小写不一样)在注册服务中加上下面代码

 foreach (var item in GetClassName("PreschoolEducationOnline.Service"))
            {
                {
                    foreach (var typeArray in item.Value)
                    {
                        if (item.Key == typeof(Service.Interface.IBaseService))
                        {
                            continue;
                        }
                        services.AddScoped(typeArray, item.Key);
                    }
                }
                services.AddMvc().AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                    options.SerializerSettings.Formatting = Formatting.Indented;
                    options.SerializerSettings.DateFormatString = "yyyy-MM-dd";
                }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            }



Dictionary<Type, Type[]> GetClassName(string assemblyName)
        {
            if (!String.IsNullOrEmpty(assemblyName))
            {
                Assembly assembly = Assembly.Load(assemblyName);
                List<Type> ts = assembly.GetTypes().ToList();

                var result = new Dictionary<Type, Type[]>();
                foreach (var item in ts.Where(s => !s.IsInterface))
                {
                    var interfaceType = item.GetInterfaces();
                    result.Add(item, interfaceType);
                }
                return result;
            }
            return new Dictionary<Type, Type[]>();
        }

②自宿运行两种方式,一个是bin文件夹启动,里面有端口号,也可以选择api启用用debug模式,release不能调试

遗留的问题1.Automap的使用,如何将Dto与实体映射,怎么配置文件,这样只要配置一次,就可以使用

2 如何将list中的实体映射到dto中,不用foreach等循环

 

net core 3.1 webapi的开发遇到的问题

标签:选择   hand   DDM   add   循环   type   sof   tst   contex   

原文地址:https://www.cnblogs.com/carlpeng/p/13154236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!