6

我有一个看起来像这样的类:

public class MyClass
{
    string _value;

    public static implicit operator MyClass (string value)
    {
        return new MyClass(value);
    }

    MyClass(string value)
    {
        // Do something...
        _value = value;
    }

    public override string ToString()
    {
         // Do something...
         return _value;
    }
}

因此,我可以像这样使用这个类:

MyClass a = "Hello!";

但在 Raven DB 中,它只会像这样存储

"SomeProperty": {}

因为它没有公共属性。这是完全没用的。

为了解决这个问题,我将 _value 私有成员改为公共属性,如下所示:

public string Value { get; set; }

Raven DB 将存储

"SomeProperty": { "Value": "Hello!" }

它将是可反序列化的。

但我不想要这个公共财产。我可以以某种方式使 Raven DB 像字符串一样对类进行序列化和反序列化吗?像:

"SomeProperty": "Hello!"
4

2 回答 2

12

嗨,我知道这已经过时了,但我想我会在 Ayendes 的回复中添加一些内容,以帮助喜欢我的人遇到同样的问题并花费数小时在论坛上寻找答案(其中有一些但没有任何例子你可以跟随),这并不难弄清楚,但举个例子,我可以在 10 分钟内解决这个问题,而不是花几个小时。

我的问题是我们的应用程序中有自定义值类型结构,我将使用的示例是 EmailAddress。不幸的是,在 Ravendb 中,如果不定义自定义序列化程序,我们就无法对这些类型运行查询。

我们的值类型看起来像这样:

[DataContract(Namespace = DataContractNamespaces.ValueTypes)]
public struct EmailAddress : IEquatable<EmailAddress>
{
    private const char At = '@';

    public EmailAddress(string value) : this()
    {
        if (value == null)
        {
            throw new ArgumentNullException("value");
        }

        this.Value = value;
    }

    public bool IsWellFormed
    {
        get
        {
            return Regex.IsMatch(this.Value, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        }
    }

    public string Domain
    {
        get
        {
            return this.Value.Split(At)[1];
        }
    }

    [DataMember(Name = "Value")]
    private string Value { get; set; }

    public static bool operator ==(EmailAddress left, EmailAddress right)
    {
        return left.Equals(right);
    }

    public static bool operator !=(EmailAddress left, EmailAddress right)
    {
        return !left.Equals(right);
    }

    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return false;
        }

        return this.Equals(new EmailAddress(obj.ToString()));
    }

    public override int GetHashCode()
    {
        return this.Value.GetHashCode();
    }

    public override string ToString()
    {
        return this.Value;
    }

    public bool Equals(EmailAddress other)
    {
        return other != null && this.Value.Equals(other.ToString(), StringComparison.OrdinalIgnoreCase);
    }
}

我们想要保存和查询的文档类型看起来像这样

public class Customer
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public EmailAddress Email { get; set; }
}

自定义序列化器将我们的电子邮件存储为原始字符串,然后在检索时将其转换回其值类型,如下所示:

public class EmailConverterTest : JsonConverter
{

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(EmailAddress);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        EmailAddress actualAddress =  new EmailAddress(reader.Value.ToString());

        return actualAddress;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        EmailAddress actualAddress = (EmailAddress)value;
        string stringEmail = actualAddress.ToString();
        writer.WriteValue(stringEmail);
    }
}

最后我把它连接起来,并且能够查询所有内容,如下所示:

    public static void serializercustom(Newtonsoft.Json.JsonSerializer serialiser)
    {
        serialiser.Converters.Add(new EmailConverterTest());
    }

    public static void TestCustomer()
    {
        using (var documentStore = new DefaultDocumentStore())
        {
            documentStore.ConnectionStringName = Properties.Settings.Default.SandBoxConnection;
            documentStore.Initialize();
            documentStore.Conventions.CustomizeJsonSerializer = new Action<Newtonsoft.Json.JsonSerializer>(serializercustom);

            var customer = new Customer
            {
                Id = Guid.NewGuid(),
                FirstName = "TestFirstName",
                LastName = "TestLastName",
                Email = new EmailAddress("testemail@gmail.com")
            };

            // Save and retrieve the data
            using (var session = documentStore.OpenSession())
            {
                session.Store(customer);
                session.SaveChanges();
            }

            using (var session = documentStore.OpenSession())
            {
                var addressToQuery = customer.Email;
                var result = session.Query<Customer>(typeof(CustomerEmailIndex).Name).Customize(p => p.WaitForNonStaleResults()).Where(p => p.Email == addressToQuery);

                Console.WriteLine("Number of Results {0}", result.Count()); // This always seems to return the matching document
            }
        }
    } 
于 2012-08-24T00:38:25.413 回答
6

您可以编写一个 JsonConverter 并教 RavenDB 如何存储数据。编写转换器后,将其注册到 store.Conventions.CustomizeSerializer 事件中。

于 2012-05-18T07:33:05.533 回答