标签:选择 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等循环
标签:选择 hand DDM add 循环 type sof tst contex
原文地址:https://www.cnblogs.com/carlpeng/p/13154236.html