我可以在 RavenDB 中使用构造函数重载吗?
Can I use constructor overload with RavenDB?
我一直在尝试编写一个聪明的解决方案来解决我的问题,但我失败了。我需要在我的域中以我的身份执行构造函数重载,首先是一个对象实例,另一个是 RavenDB 填充。这没什么大不了的,对吧?但是 RavenDB 不能与多个构造函数一起使用,消息如何显示:
Message = "Unable to find a constructor to use for type
Project.Domain.Business.Users.User. A class should either have a
default constructor, one constructor with arguments or a constructor
marked with the JsonConstructor attribute. Path 'Login'."
我的想法是在我的身份中保留一些域规则,当有人创建对象实例时这些规则是必需的。我们如何在以下示例中看到:
我的身份用户:
namespace Project.Domain.Business.Users
{
public class User
{
public string Id { get; private set; }
public string Login { get; private set; }
public string Password { get; private set; }
public string Role { get; private set; }
public string Token { get; private set; }
public string RecoveryToken { get; private set; }
public string GoogleClientId { get; private set; }
public string GoogleClientSecret { get; private set; }
public string FacebookAppId { get; private set; }
public string FacebookAppSecret { get; private set; }
public Person Person { get; private set; }
public User(string login, string password, string googleClientId, string googleClientSecret, string facebookAppId, string facebookAppSecret, Person person)
{
Id = Guid.NewGuid().ToString();
Login = login;
Password = SecurityHolder.EncryptPassword(password);
Role = "Common";
Token = SecurityHolder.GetHash_HMACSHA256(login + "_" + password);
RecoveryToken = SecurityHolder.GetHash_HMACSHA256(login);
GoogleClientId = googleClientId;
GoogleClientSecret = googleClientSecret;
FacebookAppId = facebookAppId;
FacebookAppSecret = facebookAppSecret;
Person = person;
}
}
}
在我的服务中创建的对象(或其他任何东西,例如可以在控制器中):
var emailObject = new Email(email);
var cellPhoneNumberObject = new Phone(cellPhoneNumber);
var naturalPerson = new NaturalPerson(name, gender, birth, emailObject, cellPhoneNumberObject, description);
var user = new User(login, password, googleClientId, googleClientSecret, facebookAppId, facebookAppSecret, naturalPerson);
var userResults = _userValidation.IsValid(user);
if (userResults.IsValid)
{
_userRepository.Create(user);
}
else
{
throw new Exception(userResults.ToString("~"));
}
我正在考虑使用创建型设计模式来创建我的对象实例,也许是工厂,并设置标识属性 public。所以,RavenDB 应该也能正常工作。但是,我不知道这样对不对。
编辑:
正如加雷问我的那样。 create方法在UserRepository中实现:
public class UserRepository : IUserRepository
{
private readonly IUnitOfWork _unitOfWork;
public UserRepository(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public User GetById(string id)
{
var session = _unitOfWork.OpenSession();
return session.Load<User>(id);
}
public void Create(User user)
{
var session = _unitOfWork.OpenSession();
session.Store(user);
session.SaveChanges();
}
public void Edit(User user)
{
var session = _unitOfWork.OpenSession();
session.SaveChanges();
}
}
工作单元模式已用于将 raven 状态会话保持在范围内。在我的服务中做用户 select 和更新很重要。
工作单位代码:
public class UnitOfWork : IUnitOfWork
{
private readonly IDocumentStore _documentStore;
private IDocumentSession _documentSession;
public UnitOfWork(IDocumentStoreHolder documentStoreHolder)
{
_documentStore = documentStoreHolder.DocumentStore;
_documentSession = null;
}
public IDocumentSession OpenSession()
{
return _documentSession ?? (_documentSession = _documentStore.OpenSession());
}
}
经过一番研究,我找到了解决方案。可以在 RavenDB 的构造函数中使用标签 [Json Constructor]
序列化它。
示例:
[JsonConstructor]
public User(string id, string login, string password, string role, string token, string recoveryToken, string googleClientId, string googleClientSecret, string facebookAppId, string facebookAppSecret, Person person)
{
Id = id;
Login = login;
Password = password;
Role = role;
Token = token;
RecoveryToken = recoveryToken;
GoogleClientId = googleClientId;
GoogleClientSecret = googleClientSecret;
FacebookAppId = facebookAppId;
FacebookAppSecret = facebookAppSecret;
Person = person;
}
因此,它允许使用构造函数重载。
在 RavenDb 文档中阅读更多内容。
我一直在尝试编写一个聪明的解决方案来解决我的问题,但我失败了。我需要在我的域中以我的身份执行构造函数重载,首先是一个对象实例,另一个是 RavenDB 填充。这没什么大不了的,对吧?但是 RavenDB 不能与多个构造函数一起使用,消息如何显示:
Message = "Unable to find a constructor to use for type Project.Domain.Business.Users.User. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Login'."
我的想法是在我的身份中保留一些域规则,当有人创建对象实例时这些规则是必需的。我们如何在以下示例中看到:
我的身份用户:
namespace Project.Domain.Business.Users
{
public class User
{
public string Id { get; private set; }
public string Login { get; private set; }
public string Password { get; private set; }
public string Role { get; private set; }
public string Token { get; private set; }
public string RecoveryToken { get; private set; }
public string GoogleClientId { get; private set; }
public string GoogleClientSecret { get; private set; }
public string FacebookAppId { get; private set; }
public string FacebookAppSecret { get; private set; }
public Person Person { get; private set; }
public User(string login, string password, string googleClientId, string googleClientSecret, string facebookAppId, string facebookAppSecret, Person person)
{
Id = Guid.NewGuid().ToString();
Login = login;
Password = SecurityHolder.EncryptPassword(password);
Role = "Common";
Token = SecurityHolder.GetHash_HMACSHA256(login + "_" + password);
RecoveryToken = SecurityHolder.GetHash_HMACSHA256(login);
GoogleClientId = googleClientId;
GoogleClientSecret = googleClientSecret;
FacebookAppId = facebookAppId;
FacebookAppSecret = facebookAppSecret;
Person = person;
}
}
}
在我的服务中创建的对象(或其他任何东西,例如可以在控制器中):
var emailObject = new Email(email);
var cellPhoneNumberObject = new Phone(cellPhoneNumber);
var naturalPerson = new NaturalPerson(name, gender, birth, emailObject, cellPhoneNumberObject, description);
var user = new User(login, password, googleClientId, googleClientSecret, facebookAppId, facebookAppSecret, naturalPerson);
var userResults = _userValidation.IsValid(user);
if (userResults.IsValid)
{
_userRepository.Create(user);
}
else
{
throw new Exception(userResults.ToString("~"));
}
我正在考虑使用创建型设计模式来创建我的对象实例,也许是工厂,并设置标识属性 public。所以,RavenDB 应该也能正常工作。但是,我不知道这样对不对。
编辑:
正如加雷问我的那样。 create方法在UserRepository中实现:
public class UserRepository : IUserRepository
{
private readonly IUnitOfWork _unitOfWork;
public UserRepository(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public User GetById(string id)
{
var session = _unitOfWork.OpenSession();
return session.Load<User>(id);
}
public void Create(User user)
{
var session = _unitOfWork.OpenSession();
session.Store(user);
session.SaveChanges();
}
public void Edit(User user)
{
var session = _unitOfWork.OpenSession();
session.SaveChanges();
}
}
工作单元模式已用于将 raven 状态会话保持在范围内。在我的服务中做用户 select 和更新很重要。
工作单位代码:
public class UnitOfWork : IUnitOfWork
{
private readonly IDocumentStore _documentStore;
private IDocumentSession _documentSession;
public UnitOfWork(IDocumentStoreHolder documentStoreHolder)
{
_documentStore = documentStoreHolder.DocumentStore;
_documentSession = null;
}
public IDocumentSession OpenSession()
{
return _documentSession ?? (_documentSession = _documentStore.OpenSession());
}
}
经过一番研究,我找到了解决方案。可以在 RavenDB 的构造函数中使用标签 [Json Constructor]
序列化它。
示例:
[JsonConstructor]
public User(string id, string login, string password, string role, string token, string recoveryToken, string googleClientId, string googleClientSecret, string facebookAppId, string facebookAppSecret, Person person)
{
Id = id;
Login = login;
Password = password;
Role = role;
Token = token;
RecoveryToken = recoveryToken;
GoogleClientId = googleClientId;
GoogleClientSecret = googleClientSecret;
FacebookAppId = facebookAppId;
FacebookAppSecret = facebookAppSecret;
Person = person;
}
因此,它允许使用构造函数重载。
在 RavenDb 文档中阅读更多内容。