如何在 mvc 中更改业务模型属性的可访问性
How to change business model properties accessibility in mvc
所以,我有这个 class
public abstract class Entity<T> : IEntity<T>, IAuditableEntity,ISoftDeletable where T : struct
{
public T Id { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public DateTime UpdatedDate { get; set; }
public string UpdatedBy { get; set; }
public bool Deleted { get; set; }
}
这让我可以创建 class 类
public class User : Entity<int>
{
public string Username { get; set; }
public string Password { get; set; }
public byte LoginTypeId { get; set; }
public virtual LoginType LoginType { get; set; }
}
它将被像这样的存储库操纵:
public abstract class Repository<TEntity,TKey> : IRepository<TEntity, TKey> where TEntity : Entity<TKey> where TKey : struct
{
protected DbContext Context;
protected readonly IDbSet<TEntity> Set;
protected Repository(DbContext context)
{
Context = context;
Set = Context.Set<TEntity>();
}
public TEntity Get(TKey key)
{
return Set.FirstOrDefault(o => o.Id.Equals(key));
}
public virtual IEnumerable<TEntity> GetAll()
{
return Set.AsEnumerable();
}
}
objective 是这将在某些 WebApi2 控制器中使用,但按照良好的规则规定,我不应按原样公开用户 class(我不允许这样做)。
我的要求是基本属性(Id、Datetime、Deleted 等)在控制器级别变为只读,但显然 read/write 在存储库级别。
比如controller不能直接设置Deleted属性,必须在repository调用Delete操作的时候设置
我可以隐藏继承的属性,但我不能禁止任何人这样做
var u= new User();
var baseEntity = (Entity<int>) u;
那么,我如何才能在控制器级别将具有基本属性的用户实体设置为只读?
假设您 Entity
、Controller
和 Repository
在不同的项目中,一种可能的方法是使用 InternalsVisibleToAttribute class。
您首先应将仅对 Repository
项目可见的所有属性更改为 internal
属性。然后您将使用 InternalsVisibleTo
class 以便它们对该项目可见。
然而,在这种情况下,它们仍然对 Entity
所在的项目可见。
在您的 webapi 项目中使用视图模型以仅公开您想要的数据。请参阅此相关问题 Does it Make Sense to have ViewModels in the Webapi?
所以,我有这个 class
public abstract class Entity<T> : IEntity<T>, IAuditableEntity,ISoftDeletable where T : struct
{
public T Id { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public DateTime UpdatedDate { get; set; }
public string UpdatedBy { get; set; }
public bool Deleted { get; set; }
}
这让我可以创建 class 类
public class User : Entity<int>
{
public string Username { get; set; }
public string Password { get; set; }
public byte LoginTypeId { get; set; }
public virtual LoginType LoginType { get; set; }
}
它将被像这样的存储库操纵:
public abstract class Repository<TEntity,TKey> : IRepository<TEntity, TKey> where TEntity : Entity<TKey> where TKey : struct
{
protected DbContext Context;
protected readonly IDbSet<TEntity> Set;
protected Repository(DbContext context)
{
Context = context;
Set = Context.Set<TEntity>();
}
public TEntity Get(TKey key)
{
return Set.FirstOrDefault(o => o.Id.Equals(key));
}
public virtual IEnumerable<TEntity> GetAll()
{
return Set.AsEnumerable();
}
}
objective 是这将在某些 WebApi2 控制器中使用,但按照良好的规则规定,我不应按原样公开用户 class(我不允许这样做)。
我的要求是基本属性(Id、Datetime、Deleted 等)在控制器级别变为只读,但显然 read/write 在存储库级别。
比如controller不能直接设置Deleted属性,必须在repository调用Delete操作的时候设置
我可以隐藏继承的属性,但我不能禁止任何人这样做
var u= new User();
var baseEntity = (Entity<int>) u;
那么,我如何才能在控制器级别将具有基本属性的用户实体设置为只读?
假设您 Entity
、Controller
和 Repository
在不同的项目中,一种可能的方法是使用 InternalsVisibleToAttribute class。
您首先应将仅对 Repository
项目可见的所有属性更改为 internal
属性。然后您将使用 InternalsVisibleTo
class 以便它们对该项目可见。
然而,在这种情况下,它们仍然对 Entity
所在的项目可见。
在您的 webapi 项目中使用视图模型以仅公开您想要的数据。请参阅此相关问题 Does it Make Sense to have ViewModels in the Webapi?