gpt4 book ai didi

c# - 首先使用实体​​框架代码和 IOC 容器来创建没有默认构造函数的实体

转载 作者:行者123 更新时间:2023-11-30 16:53:17 26 4
gpt4 key购买 nike

我想构建一个加密其某些属性的 Entity Framework 代码优先数据层。为此,在使用从数据库加载的数据创建实例时,应该注入(inject) ICryptographer

是否可以使用像 unity 这样的 IOC 容器注入(inject) ICryptographer

DAL 示例:

public class context : DbContext
{
public DbSet<Credentials> Credentials {get;set;}
}

public CredentialsConfiguration()
{
ToTable("Profiles");
HasKey(p => p.ProfileName);
Ignore(p => p.SecretKey);
Ignore(p => p.AccessKey);
Property(p => p._accessKey)
.HasColumnName("AccessKey");
Property(p => p._secretKey)
.HasColumnName("SecretKey");
}

通用实体程序集:(内部结构对 DAL 可见)

public class Credentials
{
private readonly ICryptographer _cryptographer;

public Credentials(ICryptographer cryptographer)
{
_cryptographer = cryptographer;
}


internal string _accessKey { get; set; }
public string AccessKey
{
get { return _cryptographer.Decrypt(_accessKey); }
set { _accessKey = _cryptographer.Encrypt(value); }
}

}

最佳答案

这是一个非常人为的例子,但可能会把你推向正确的方向

我们需要一种方法来知道何时将属性注入(inject)实体类,因为它们被标记为使用

public interface ICryptographerUser {
ICryptographer Cryptographer { get; set; }
}

ICryptographer 定义为

public interface ICryptographer {
string Decrypt(string value);
string Encrypt(string value);
}

将被注入(inject)的实例定义为

public class Cryptographer : ICryptographer {
public string Decrypt(string value) {
return "Decrypted";
}

public string Encrypt(string value) {
return "Encrypted";
}
}

当添加一个实体时,我们需要使用一个Factory,它知道我们需要注入(inject)属性(您可以使用 IOC 来做到这一点)

public static class EntityFactory {
public static T CreateInstance<T> () {
var entity = Activator.CreateInstance<T>();
if (entity is ICryptographerUser) {
//INJECT INSTANCE HERE
(entity as ICryptographerUser).Cryptographer = new Cryptographer();
}

return entity;
}
}

现在添加我们可以使用的实体

var entity = EntityFactory.CreateInstance<Credentials>();
entity.SetAccessKey("123");
entity.SecretKey = "456";
entity.ProfileName = "a";

contect.Set<Credentials>().Add(entity);

当从 Context 查询对象时,以下代码将注入(inject)对象,但这是在它们各自的属性已经设置之后完成的

public MyContext() {
IObjectContextAdapter objectContextAdapter = (this as IObjectContextAdapter);
objectContextAdapter.ObjectContext.ObjectStateManager.ObjectStateManagerChanged += ObjectStateManager_ObjectStateManagerChanged;
}

private void ObjectStateManager_ObjectStateManagerChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e) {
// we are only interested in entities that
// have been added to the state manager
if (e.Action != CollectionChangeAction.Add)
return;

IObjectContextAdapter objectContextAdapter = (this as IObjectContextAdapter);

var state = objectContextAdapter.ObjectContext.ObjectStateManager.GetObjectStateEntry(e.Element).State;

// we are only interested in entities that
// are unchanged (that is; loaded from DB)
if (state != EntityState.Unchanged)
return;

OnObjectMaterialized(e.Element);
}

private void OnObjectMaterialized(object e) {
if (e is ICryptographerUser) {
//INJECT INSTANCE HERE
(e as ICryptographerUser).Cryptographer = new Cryptographer();
}
}

因为实例只会在 Entity 被具体化后注入(inject),所以我需要修改你的 Entity 定义如下

public class Credentials : ICryptographerUser {
public string ProfileName { get; set; }

internal string _secretKey { get; set; }
internal string _accessKey { get; set; }


public string SecretKey { get; set; }

public string AccessKey {
get { return _accessKey; }
private set { _accessKey = value; }
}

public string AccessKeyDecrypted {
get { return Cryptographer.Decrypt(_accessKey); }
}

public void SetAccessKey(string value) {
_accessKey = Cryptographer.Encrypt(value);
}

public ICryptographer Cryptographer { get; set; }
}

请注意 AccessKey 具有 private set { _accessKey = value; } 允许 EFobject 实现时设置属性,但是当您设置属性值时,您必须调用 public void SetAccessKey(string value) 使用 Cryptographer 来加密字段。

关于c# - 首先使用实体​​框架代码和 IOC 容器来创建没有默认构造函数的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31695642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com