-
-
Save fractos/6c7f53dd88aeb4c00b5f to your computer and use it in GitHub Desktop.
blog 21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Xml; | |
using Inversion; | |
using Newtonsoft.Json.Linq; | |
namespace Harness.Example.Model | |
{ | |
public class User : IData | |
{ | |
private readonly string _username; | |
private readonly string _password; | |
private readonly UserMetadata _metadata; | |
public string Username { get { return _username; } } | |
public string Password { get { return _password; } } | |
public UserMetadata Metadata { get { return _metadata; } } | |
public User(User user) | |
{ | |
this._username = user.Username; | |
this._password = user.Password; | |
this._metadata = new UserMetadata(user.Metadata); | |
} | |
public User(string username, string password, UserMetadata metadata) | |
{ | |
this._username = username; | |
this._password = password; | |
this._metadata = metadata; | |
} | |
public User Mutate(Func<Builder, User> mutator) | |
{ | |
Builder builder = new Builder(this); | |
return mutator(builder); | |
} | |
public class Builder | |
{ | |
public string Username { get; set; } | |
public string Password { get; set; } | |
public UserMetadata.Builder Metadata { get; set; } | |
public Builder() | |
{ | |
this.Metadata = new UserMetadata.Builder(); | |
} | |
public Builder(User user) | |
{ | |
this.Username = user.Username; | |
this.Password = user.Password; | |
this.Metadata = user.Metadata; | |
} | |
public Builder(string username, string password, UserMetadata metadata) | |
{ | |
this.Username = username; | |
this.Password = password; | |
this.Metadata = new UserMetadata(metadata); | |
} | |
public User ToModel() | |
{ | |
return new User(this.Username, this.Password, this.Metadata); | |
} | |
public Builder FromModel(User user) | |
{ | |
this.Username = user.Username; | |
this.Password = user.Password; | |
this.Metadata = new UserMetadata(user.Metadata); | |
return this; | |
} | |
public static implicit operator User(Builder builder) | |
{ | |
return builder.ToModel(); | |
} | |
public static implicit operator Builder(User model) | |
{ | |
return new Builder(model); | |
} | |
} | |
public object Clone() | |
{ | |
return new User(this); | |
} | |
public void ToXml(XmlWriter writer) | |
{ | |
writer.WriteStartElement("user"); | |
writer.WriteElementString("username", this.Username); | |
writer.WriteElementString("password", this.Password); | |
this.Metadata.ToXml(writer); | |
writer.WriteEndElement(); | |
} | |
public Newtonsoft.Json.Linq.JObject Data | |
{ | |
get | |
{ | |
JObject j = new JObject | |
{ | |
{"username", this.Username}, | |
{"password", this.Password}, | |
{"metadata", ((IData) (this.Metadata)).Data} | |
}; | |
return j; | |
} | |
} | |
public void ToJson(Newtonsoft.Json.JsonWriter writer) | |
{ | |
writer.WriteStartObject(); | |
writer.WritePropertyName("username"); | |
writer.WriteValue(this.Username); | |
writer.WritePropertyName("password"); | |
writer.WriteValue(this.Password); | |
this.Metadata.ToJson(writer); | |
writer.WriteEndObject(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections.Immutable; | |
using System.Xml; | |
using Inversion; | |
using Newtonsoft.Json.Linq; | |
namespace Harness.Example.Model | |
{ | |
public class UserMetadata : IData | |
{ | |
private readonly ImmutableDictionary<string, string> _metadata; | |
public ImmutableDictionary<string, string> Metadata { get { return _metadata; } } | |
public UserMetadata(UserMetadata usermetadata) | |
{ | |
ImmutableDictionary<string, string>.Builder b = ImmutableDictionary.CreateBuilder<string, string>(); | |
b.AddRange(usermetadata.Metadata); | |
this._metadata = b.ToImmutable(); | |
} | |
public UserMetadata(IDictionary<string, string> metadata) | |
{ | |
ImmutableDictionary<string, string>.Builder b = ImmutableDictionary.CreateBuilder<string, string>(); | |
b.AddRange(metadata); | |
this._metadata = b.ToImmutable(); | |
} | |
public UserMetadata Mutate(Func<Builder, UserMetadata> mutator) | |
{ | |
Builder builder = new Builder(this); | |
return mutator(builder); | |
} | |
public class Builder | |
{ | |
public Dictionary<string, string> Metadata { get; set; } | |
public Builder() | |
{ | |
this.Metadata = new Dictionary<string, string>(); | |
} | |
public Builder(UserMetadata usermetadata) | |
{ | |
this.Metadata = new Dictionary<string, string>(usermetadata.Metadata); | |
} | |
public Builder(IDictionary<string, string> metadata) | |
{ | |
this.Metadata = new Dictionary<string, string>(metadata); | |
} | |
public UserMetadata ToModel() | |
{ | |
return new UserMetadata(this.Metadata); | |
} | |
public Builder FromModel(UserMetadata usermetadata) | |
{ | |
this.Metadata = new Dictionary<string, string>(usermetadata.Metadata); | |
return this; | |
} | |
public static implicit operator UserMetadata(Builder builder) | |
{ | |
return builder.ToModel(); | |
} | |
public static implicit operator Builder(UserMetadata model) | |
{ | |
return new Builder(model); | |
} | |
} | |
public object Clone() | |
{ | |
return new UserMetadata(this); | |
} | |
public void ToXml(XmlWriter writer) | |
{ | |
writer.WriteStartElement("usermetadata"); | |
foreach (KeyValuePair<string, string> kvp in this.Metadata) | |
{ | |
writer.WriteElementString(kvp.Key, kvp.Value); | |
} | |
writer.WriteEndElement(); | |
} | |
Newtonsoft.Json.Linq.JObject IData.Data | |
{ | |
get | |
{ | |
return JObject.FromObject(this.Metadata); | |
} | |
} | |
public void ToJson(Newtonsoft.Json.JsonWriter writer) | |
{ | |
writer.WriteStartObject(); | |
foreach (KeyValuePair<string, string> kvp in this.Metadata) | |
{ | |
writer.WritePropertyName(kvp.Key); | |
writer.WriteValue(kvp.Value); | |
} | |
writer.WriteEndObject(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment