Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wegel/fluentprotobufnet
Fluent protobuf-net lets you write mappings in strongly typed C# code
https://github.com/wegel/fluentprotobufnet
Last synced: 3 months ago
JSON representation
Fluent protobuf-net lets you write mappings in strongly typed C# code
- Host: GitHub
- URL: https://github.com/wegel/fluentprotobufnet
- Owner: wegel
- Created: 2013-09-20T02:02:16.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-09-20T04:09:39.000Z (over 11 years ago)
- Last Synced: 2023-08-04T01:27:32.343Z (over 1 year ago)
- Language: C#
- Homepage:
- Size: 3.6 MB
- Stars: 5
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Fluent protobuf-net
=================Fluent protobuf-net offers an alternative to [protobuf-net](https://code.google.com/p/protobuf-net/) standard attributes-based mapping and string-based mapping. Fluent protobuf-net lets you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability, more concise code and a natural seperation between mappings and classes.
Example
---------------------------------------------Mapping using the standard protobuf-net attributes.
[ProtoContract]
[ProtoInclude(1, typeof(Bird))]
[ProtoInclude(2, typeof(Mammal))]
public class Animal
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2, AsReference = true)]
public AnimalGroup Group { get; set; }
}[ProtoContract]
public class Mammal : Animal
{
[ProtoMember(1)]
public bool IsMonotreme { get; set; }
[ProtoMember(2)]
public bool LivesOnLand { get; set; }
}[ProtoContract]
public class Bird: Animal
{
[ProtoMember(1)]
public bool CanFly { get; set; }
}[ProtoContract]
public class AnimalGroup
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2, AsReference = true)]
public IList Members { get; set; }
[ProtoMember(3)]
public bool AreWarmBlooded { get; set; }
}
Now using Fluent protobuf-net. First the classes, without any special attributes:public class Animal
{
public string Name { get; set; }
public AnimalGroup Group { get; set; }
}public class Mammal : Animal
{
public bool IsMonotreme { get; set; }
public bool LivesOnLand { get; set; }
}public class Bird: Animal
{
public bool CanFly { get; set; }
}public class AnimalGroup
{
public string Name { get; set; }
public IList Members { get; set; }
public bool AreWarmBlooded { get; set; }
}
And your mapping classes:public class AnimalMap : ClassMap
{
public AnimalMap()
{
Map(a => a.Name, 1);
References(a => a.Group, 2);
}
}public class MammalMap : SubclassMap
{
public MammalMap()
{
SubclassFieldId(2);Map(m => m.IsMonotreme, 1);
Map(m => m.LivesOnLand, 2);
}
}public class BirdMap : SubclassMap
{
public BirdMap()
{
SubclassFieldId(1);Map(b => b.CanFly, 1);
}
}public class AnimalGroupMap : ClassMap
{
public AnimalGroupMap()
{
Map(g => g.Name, 1);
References(g => g.Members, 2);
Map(g => g.AreWarmBlooded, 3);
}
}
To get your RuntimeTypeModel (used to serialize/deserialize), you do:var config = Fluently.Configure()
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf())
.BuildConfiguration();
config.RuntimeTypeModel.Serialize(...);
Thanks
---------------------------------------------The basic architecture of Fluent protobuf-net was shamelessly copied from [Fluent NHibernate](https://github.com/jagregory/fluent-nhibernate). [Fluent NHibernate](https://github.com/jagregory/fluent-nhibernate) is GREAT; if you use NHibernate, use it.
Future
---------------------------------------------The library was created in just a few short hours. A lot more is possible:
- Support all of protobuf-net options (IsRequired, DataFormat, etc)
- Automapping (with a standardised way to serialize the generated mappings)
- Conventions
They will be added as I need them, and I'll gladly accept pull requests for features.Have fun!