C# C将类类型另存为预处理器
在C中是否可以像在C/C++中一样将类类型保存为预处理器指令 我有很多共享代码的多个服务。主要区别在于调用正确的DbSet和使用正确的类 从以下代码到:C# C将类类型另存为预处理器,c#,design-patterns,preprocessor-directive,C#,Design Patterns,Preprocessor Directive,在C中是否可以像在C/C++中一样将类类型保存为预处理器指令 我有很多共享代码的多个服务。主要区别在于调用正确的DbSet和使用正确的类 从以下代码到: public class TaxService { readonly DatabaseContext db; public TaxService(DatabaseContext database) { db = database; } public async Task<stri
public class TaxService
{
readonly DatabaseContext db;
public TaxService(DatabaseContext database)
{
db = database;
}
public async Task<string> DeleteAsync(int? id)
{
if (await db.Taxes.FindAsync(id) is Tax tax)
{
string title = tax.Name;
db.Taxes.Remove(tax);
try
{
await db.SaveChangesAsync();
return title;
}
catch (Exception ex)
{
throw;
}
}
return null;
}
public async Task<List<Tax>> GetAllAsync()
{
return await db.Taxes.AsNoTracking().ToListAsync();
}
}
我曾经构建过一个这样的基本控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
namespace DataAccessLayer.Controllers
{
public class BaseController<TEntity> where TEntity : class, new()
{
public virtual TEntity Get<TContext>(Expression<Func<TEntity, bool>> predicate, TContext context) where TContext : DbContext
{
var item = context.Set<TEntity>().FirstOrDefault(predicate);
return item;
}
public List<TEntity> GetList<TContext>(Expression<Func<TEntity, bool>> predicate, TContext context) where TContext : DbContext
{
var item = context.Set<TEntity>().Where(predicate).ToList();
return item;
}
public IQueryable<TEntity> GetListQueryable<TContext>(Expression<Func<TEntity, bool>> predicate, TContext context) where TContext : DbContext
{
var item = context.Set<TEntity>().Where(predicate);
return item;
}
public List<TEntity> GetAll<TContext>(TContext context) where TContext : DbContext
{
var item = context.Set<TEntity>().ToList();
return item;
}
public IEnumerable<TEntity> GetAllEnumerable<TContext>(TContext context) where TContext : DbContext
{
IEnumerable<TEntity> item = context.Set<TEntity>();
return item;
}
public virtual TEntity Update<TContext>(TEntity input, Expression<Func<TEntity, bool>> predicate, TContext context) where TContext : DbContext
{
if (input == null)
return null;
var existing = context.Set<TEntity>().FirstOrDefault(predicate);
if (existing != null) context.Entry(existing).CurrentValues.SetValues(input);
return existing;
}
public virtual TEntity Insert<TContext>(TEntity input, TContext context) where TContext : DbContext
{
context.Set<TEntity>().Add(input);
return input;
}
}
}
这在处理数据库时非常有用,数据库是由一个醉酒的程序员匆忙创建的,因为所有遵循相同模式的东西都可以使用,而所有不遵循相同模式的东西都可以通过控制器使用,控制器只是负责处理数据库中存在的任何异常
这只是一个PoC,你不必这样做。只是为了让你的梦境运行起来。存储库模式,再加上工作单元可以解决这个问题。@MortenBork感谢你的想法。我研究了一下,若每个类T都有相同的DeleteAsync方法属性名,那个么这对我来说是可行的。有时是它的标题,有时是它的名字。。我希望避免使用reflectionHi。请看一看我作为答案提供的示例。。。通过添加表达式作为谓词,您可以在控制器中定义要删除的内容。看起来不错。但是我想到了一些类似于C++的定义T税。在谷歌上搜索后,在C中是不可能的:/好吧,你可以在where上添加一个条件,你只接受税类作为T,或者所有T必须是税类。比如:公共类TaxController,其中T:Tax{}我编辑了我的第一篇文章。你给了我一个主意。谢谢
public class TaxService : TaxService<Tax>
{
public TaxService(DatabaseContext database) : base(database)
{
}
}
public class TaxService<T> where T : Tax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
namespace DataAccessLayer.Controllers
{
public class BaseController<TEntity> where TEntity : class, new()
{
public virtual TEntity Get<TContext>(Expression<Func<TEntity, bool>> predicate, TContext context) where TContext : DbContext
{
var item = context.Set<TEntity>().FirstOrDefault(predicate);
return item;
}
public List<TEntity> GetList<TContext>(Expression<Func<TEntity, bool>> predicate, TContext context) where TContext : DbContext
{
var item = context.Set<TEntity>().Where(predicate).ToList();
return item;
}
public IQueryable<TEntity> GetListQueryable<TContext>(Expression<Func<TEntity, bool>> predicate, TContext context) where TContext : DbContext
{
var item = context.Set<TEntity>().Where(predicate);
return item;
}
public List<TEntity> GetAll<TContext>(TContext context) where TContext : DbContext
{
var item = context.Set<TEntity>().ToList();
return item;
}
public IEnumerable<TEntity> GetAllEnumerable<TContext>(TContext context) where TContext : DbContext
{
IEnumerable<TEntity> item = context.Set<TEntity>();
return item;
}
public virtual TEntity Update<TContext>(TEntity input, Expression<Func<TEntity, bool>> predicate, TContext context) where TContext : DbContext
{
if (input == null)
return null;
var existing = context.Set<TEntity>().FirstOrDefault(predicate);
if (existing != null) context.Entry(existing).CurrentValues.SetValues(input);
return existing;
}
public virtual TEntity Insert<TContext>(TEntity input, TContext context) where TContext : DbContext
{
context.Set<TEntity>().Add(input);
return input;
}
}
}
public class TaxcController : BaseController<Tax>
{
}
public void dostuff()
{
TaxController taxController = new TaxController();
taxController.Insert(item, context);
}