C# 在存储库中而不是在MVC控制器中创建新GUID
我正在创建一个新的MVC解决方案,使用我的视频中的UDE指令。讲师使用存储库模式与模型交互,但他使用int。另一方面,我使用的是Guid,他没有显示。我知道在自动创建int时,我需要像这里一样显式创建Guid:C# 在存储库中而不是在MVC控制器中创建新GUID,c#,asp.net-mvc,repository-pattern,guid,C#,Asp.net Mvc,Repository Pattern,Guid,我正在创建一个新的MVC解决方案,使用我的视频中的UDE指令。讲师使用存储库模式与模型交互,但他使用int。另一方面,我使用的是Guid,他没有显示。我知道在自动创建int时,我需要像这里一样显式创建Guid: [HttpPost] public ActionResult CreateCarrier(Carrier carrier) { carrier.CarrierID = System.Guid.NewGuid(); carriers.Insert(carrier);
[HttpPost]
public ActionResult CreateCarrier(Carrier carrier)
{
carrier.CarrierID = System.Guid.NewGuid();
carriers.Insert(carrier);
carriers.Commit();
return RedirectToAction("CarrierList");
}
然而,由于我有许多类,我认为这将是一个额外的代码很多。我不应该在插入实体id之前创建它的Guid吗?我就是不知道如何在存储库中完成。以下是存储库代码:
public abstract class RepositoryBase<TEntity> : IRepositoryBase<TEntity> where TEntity : class
{
internal DataContext context;
internal DbSet<TEntity> dbSet;
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
}
公共抽象类RepositoryBase:IRepositoryBase其中tenty:class
{
内部数据上下文;
内部数据库集;
公共虚拟空白插入(TEntity实体)
{
添加(实体);
}
}
以下是界面:
namespace WarehouseScheduling.DAL.Contracts
{
public interface IRepositoryBase<TEntity> where TEntity : class
{
void Commit();
void Delete(TEntity entity);
void Delete(object id);
void Dispose();
IQueryable<TEntity> GetAll();
IQueryable<TEntity> GetAll(object filter);
TEntity GetById(object id);
TEntity GetFullObject(object id);
IQueryable<TEntity> GetPaged(int top = 20, int skip = 0, object orderBy = null, object filter = null);
void Insert(TEntity entity);
void Update(TEntity entity);
}
}
namespace WarehouseScheduling.DAL.Contracts
{
公共接口IRepositoryBase,其中tenty:类
{
无效提交();
无效删除(潜在实体);
作废删除(对象id);
无效处置();
IQueryable GetAll();
IQueryable GetAll(对象过滤器);
TEntity GetById(对象id);
TEntity GetFullObject(对象id);
IQueryable GetPaged(int-top=20,int-skip=0,object-orderBy=null,object-filter=null);
无效插入(TEntity实体);
无效更新(潜在实体);
}
}
您可以将泛型类型tenty强制为允许获取和设置Guid的接口
public interface IHasGuid
{
Guid Id { get; set; }
}
然后可以对存储库类/接口进行限制,使其仅适用于IHasGuid类型的实体
namespace WarehouseScheduling.DAL.Contracts
{
public interface IRepositoryBase<TEntity> where TEntity : IHasGuid
{
void Commit();
void Delete(TEntity entity);
void Delete(object id);
void Dispose();
IQueryable<TEntity> GetAll();
IQueryable<TEntity> GetAll(object filter);
TEntity GetById(object id);
TEntity GetFullObject(object id);
IQueryable<TEntity> GetPaged(int top = 20, int skip = 0, object orderBy = null, object filter = null);
void Insert(TEntity entity);
void Update(TEntity entity);
}
}
我将把接口放在哪里?在RepositoryBase类中?还是一个单独的类?@djblois应该在一个单独的文件
IHasGuid.cs
或其他文件中。@djblois并记住您的类Carrier
必须实现IHasGuid
goaty,对不起,我在原始问题中添加了更多信息。我没有意识到,因为我不熟悉接口,我的存储库库已经内置了一个接口。如何使用上面的代码?我知道,虽然int是自动创建的,但根本不是。在某个时刻,每个数据段都会被生成,或者是由客户机代码生成,或者是由数据库本身生成。int
没有“自动生成”的固有属性。通过默认约束将guid
生成移动到DB将得到非常相同的结果。
public abstract class RepositoryBase<TEntity> : IRepositoryBase<TEntity> where TEntity : IHasGuid
{
internal DataContext context;
internal DbSet<TEntity> dbSet;
public virtual void Insert(TEntity entity)
{
entity.Id = new Guid();
dbSet.Add(entity);
}
}
public class Carrier : IHasGuid
{
public Guid Id { get; set; }
// other fields below
}