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

2021-01-07 12:28

阅读:863

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

1.用的是工厂仓储模式

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

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

然后在startup  的ConfigureServices注册

services.AddScoped();
            services.AddScoped();

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 GetClassName(string assemblyName)
        {
            if (!String.IsNullOrEmpty(assemblyName))
            {
                Assembly assembly = Assembly.Load(assemblyName);
                List ts = assembly.GetTypes().ToList();

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

②自宿运行两种方式,一个是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


评论


亲,登录后才可以留言!