-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
218 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.IO; | ||
using System.Runtime.Serialization; | ||
using Xunit; | ||
using Materializer; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using System.Reflection; | ||
|
||
namespace Materializer.Tests | ||
{ | ||
public class Materializer_Newtonsoft | ||
{ | ||
public Lazy<Materializer> _lazy = new Lazy<Materializer>(() => new Materializer("Dynamic_Assembly_for_Materializer_Newtonsoft_Tests", false)); | ||
|
||
public interface IOne | ||
{ | ||
int Prop1 { get; set; } | ||
} | ||
|
||
|
||
[Fact] | ||
public void SimpleInterface_NewtonsSoft_JsonSerialize() | ||
{ | ||
var materializer = _lazy.Value; | ||
|
||
var before = materializer.New<IOne>(); | ||
before.Prop1 = 3; | ||
var json = JsonConvert.SerializeObject(before); | ||
|
||
var after = (IOne)JsonConvert.DeserializeObject(json, materializer.ConcreteTypeOf<IOne>()); | ||
|
||
Assert.Equal(3, after.Prop1); | ||
} | ||
|
||
|
||
[Fact] | ||
public void SimpleInterface_NewtonsSoft_JsonSerialize_AlternativeSample() | ||
{ | ||
var materializer = _lazy.Value; | ||
T DeserializeInterface<T>(string json) where T : class | ||
{ | ||
return (T)JsonConvert.DeserializeObject(json, materializer.ConcreteTypeOf<T>()); | ||
} | ||
|
||
var before = materializer.New<IOne>(); | ||
before.Prop1 = 3; | ||
var json = JsonConvert.SerializeObject(before); | ||
|
||
var after = DeserializeInterface<IOne>(json); | ||
|
||
Assert.Equal(3, after.Prop1); | ||
} | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.IO; | ||
using System.Runtime.Serialization; | ||
using Xunit; | ||
using Materializer; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using System.Reflection; | ||
|
||
namespace Materializer.Tests | ||
{ | ||
public class Materializer_Serializable | ||
{ | ||
public Lazy<Materializer> _lazy = new Lazy<Materializer>(() => new Materializer("Dynamic_Assembly_for_Materializer_Serializable_Tests", true)); | ||
|
||
public interface IOne | ||
{ | ||
int Prop1 { get; set; } | ||
} | ||
|
||
|
||
[Fact] | ||
public void SimpleInterface_BinaryFormatter() | ||
{ | ||
var materializer = _lazy.Value; | ||
|
||
var before = materializer.New<IOne>(); | ||
before.Prop1 = 3; | ||
|
||
IFormatter formatter = new BinaryFormatter(); | ||
using var stream = new MemoryStream(); | ||
formatter.Serialize(stream, before); | ||
|
||
stream.Seek(0, SeekOrigin.Begin); | ||
var after = (IOne)formatter.Deserialize(stream); | ||
|
||
Assert.Equal(3, after.Prop1); | ||
} | ||
|
||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,54 @@ | ||
# Materializer | ||
Define your DTOs as interfaces | ||
Classes can only be combined by inheriting, while interfaces can be combined. | ||
|
||
With `Materializer` you can define your DTOs as interfaces without the need to create classes for each of the combinations you'll want to use. | ||
|
||
Given the following interfaces: | ||
```csharp | ||
inferface IOne | ||
{ | ||
int First { get; set; } | ||
string Second { get; set; } | ||
} | ||
|
||
inferface ITwo | ||
{ | ||
DateTime Third { get; set; } | ||
Double Fourth { get; set; } | ||
} | ||
|
||
inferface ICombined : IOne, ITwo | ||
{ | ||
} | ||
``` | ||
Without `Materializer` you would need to write a class for each of the ones you want to use as DTO, producing a lot of overhead code not really worth anything. | ||
With `Materializer` you can instantiate each interface directly. | ||
|
||
```csharp | ||
var materializer = new Materializer(); | ||
|
||
// create an object representing the simple IOne interface | ||
var obj1 = materializer.New<IOne>(); | ||
obj1.First = 5; | ||
obj1.Second = "Mary Poppins"; | ||
|
||
// create an object representing the combination interface, ICombined | ||
var obj2 = materializer.New<ICombined>(); | ||
obj2.First = 10; | ||
obj2.Second = "Peter Pan"; | ||
obj2.Third = DateTime.Now; | ||
obj2.Fourth = 1.0; | ||
``` | ||
|
||
## Serializable support | ||
|
||
### NewtonSoft.Json | ||
The created objects are serializable with `NewtonSoft.Json` without setting any options. (See the tests for sample code.) | ||
|
||
### .NET serialization | ||
To be serializable with .NET serialization, a class must have the `[Serializable]` attribute. You can turn this on with the `forSerializable` option, like this: | ||
```csharp | ||
var materializer = new Materializer(forSerializable: true); | ||
``` | ||
When you set the `forSerializable` option on a serializer, all classes made with that serializer will have the `[Serializable]` attribute: | ||
|