How to serialize instances of classes generated by DbMetal? - datacontractserializer

I noticed DbMetal generates classes that don't implement the ISerializable interface, nor are marked with DataContractAttribute. What is the easiest way of serializing such classes? Is there any DbMetal parameter that could help me?

try this:
[Serializable]
public partial class YourClassName { }
See Partial Classes for further details

Related

Coder for a POJO in apache beam

I am trying to create a dummy PCollection with my custom objects as follows:
PCollection<MyClass> pipelineProcessingResults = pipeline.apply(Create.of(new MyClass(.., ..)));
MyClass class is as follows:
#DefaultSchema(JavaBeanSchema.class)
public class MyClass {
AnotherComplexClass _obj;
Urn _urn
}
I am getting the following exception:
java.lang.StackOverflowError
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
at org.apache.beam.vendor.guava.v26_0_jre.com.google.common.reflect.TypeVisitor.visit(TypeVisitor.java:66)
at org.apache.beam.vendor.guava.v26_0_jre.com.google.common.reflect.Types.getComponentType(Types.java:197)
at org.apache.beam.vendor.guava.v26_0_jre.com.google.common.reflect.TypeToken.getComponentType(TypeToken.java:563)
at org.apache.beam.vendor.guava.v26_0_jre.com.google.common.reflect.TypeToken.isArray(TypeToken.java:512)
at org.apache.beam.sdk.values.TypeDescriptor.isArray(TypeDescriptor.java:193)
at org.apache.beam.sdk.schemas.utils.ReflectUtils.getIterableComponentType(ReflectUtils.java:196)
at org.apache.beam.sdk.schemas.FieldValueTypeInformation.getIterableComponentType(FieldValueTypeInformation.java:274)
at org.apache.beam.sdk.schemas.FieldValueTypeInformation.forGetter(FieldValueTypeInformation.java:189)
at org.apache.beam.sdk.schemas.JavaBeanSchema$GetterTypeSupplier.get(JavaBeanSchema.java:74)
at org.apache.beam.sdk.schemas.utils.StaticSchemaInference.schemaFromClass(StaticSchemaInference.java:92)
at org.apache.beam.sdk.schemas.utils.StaticSchemaInference.fieldFromType(StaticSchemaInference.java:166)
The class AnotherComplexClass may contain multiple fields which in turn are composed of other classes.
Which coder will best suit my purpose? Should I create a custom coder? Using the #DefaultSchema annotation did not help me much. I tried using SerializableCoder, but it throws a compiler error:
Cannot resolve method 'of(java.lang.Class<MyClass>)'
Option 1 - Custom Coder
Since you have complex nested data types, you can define a custom coder and use it with the #DefaultCoder annotator. Details see https://beam.apache.org/documentation/programming-guide/#annotating-custom-type-default-coder.
public class MyCoder implements Coder {
public static Coder<T> of (Class<T> clazz) {...}
}
#DefaultCoder(MyCoder.class)
public class MyClass {...}
Option 2 - Serializable
You can also make sure that all your POJO classes implement Serializable and by default, Java SDK uses the SerializableCoder. But it's inefficient and non-deterministic.
Option 3 - Avro
You can use AvroCoder and use Avro to generate your classes. https://avro.apache.org/docs/current/gettingstartedjava.html
Option 4 - Protocol Buffer
Similar to Avro, you can use Protocol Buffer to define your schema and classes. https://developers.google.com/protocol-buffers/docs/javatutorial.

BsonSerializationException when passing concrete Parameter in constructor to fill more general Property with .net driver

I have a Identity class that is extended by two classes StringIdentity and GuidIdentity. I want to use them in Objects as Property and save them in a MongoDb. For Example a class would look like this:
public class MyEvent : IDomainEvent
{
public MyEvent(GuidIdentity entityId)
{
EntityId = entityId;
}
public Identity EntityId { get; }
}
IDomainEvent forces me to implement the Identity Property, so I can (and do not want to) change the property type to GuidIdentity.
When I deserialize my class I get an exception like this:
Creator map for class Microwave.Eventstores.UnitTests. MyEvent has 1
arguments, but none are configured.
Which seems logical according to the .net driver doku, as my property does not match the type of the parameter and therefore can not be serialized. I had the solution to use the Identity as constructor parameter or make the constructor private and instantiate the class with a static Create method that takes the GuidIdentity as parameter. I also messed around with the BsonClassMap, but I really do not want to write all this kind of duplicate code for every class that implements IDomainEvent
So is there a way to tell the .net Driver to use the concrete class from the constructor instead of the more general PropertyType for all Types? Some kind of silver bullet solution? ;)
I am very new to mongodb so I am not aware of all the tricks that come with it, maybe someone can help me out here.

Migrating from Unity, how to do Property or MethodInjection on all classes without registering them

Ok, lets first be clear that I know this is not correct way (use constructor injection). I do have a huge codebase to take care of and a refactoring is not an option right now.
I have a base class
public abstract class ServiceBase {
public IUnitOfWork UnitOfWork {get;set;}
}
and a lot of Concrete classes that are NOT registered in the contianer.
public class ServiceScript : ServiceBase {
}
How do I inject IUnitOfWork??
I have alot of this ServiceScript-classes that are not registred in the Container
but is using the AnyConcreteTypeNotAlreadyRegisteredSource in Autofac
In unity I did just mark my PropertyDependency with a [Dependency]-attribute and everything just works.
It looks like StructureMap has the same way of using attributes, but they don't have Prism/Xamarin-support, like Autofac has.
This same problem can be applied to a ControllerX : ControllerBase in asp.net core.
I have tried to solve this with a RegistrationSource, but can't get it to work in the controller case that I tried first.
Any input??
/Peter

EntityFramwork generating Interfaces for MEF

I am playing around building some buildingblocks based on database tables.
So I've created an UsersManager and a ValidationManager both based on the EDMX "templates".
I'd really like to loose couple those two components with MEF. But therefore i need to create Interfaces of the entityobjects exposed in the ValidationManager.
Is there an easy way of creating those Interfaces, in that manner i can still use the EDMX generated classes?
Thanx,
Paul
Using an example of a database with a Product Table, is this what you're trying to achieve....
but still use generated entity classes (using either the standard EF generator or another POCO generator of some sort).
I'm not sure - as you mention MEF and I don't see it being directly related.
The generated entity classes are partial classes which will allow you to extend the generated class which in this case you want to extend to implement an interface.
Presuming the following interface is going to be used to introduce the layer of abstraction...
public interface IEntity {
public bool IsDeleted { get; set; }
}
Create a new class file with and extended Product class...
public partial class Product : IEntity {
public bool IsDeleted {
get {
throw new NotImplementedException();
}
set {
throw new NotImplementedException();
}
}
}
You have now extended your generated entity Product with the partial class custom code - and you can use it as normal through EF.
Now instead of your UserManager and ValidationManager classes having a hard reference to Product, instead they'll only have reference to IEntity.
If I didn't understand the question, please provide more details on exactly it is you want to do.

EF Codefirst, One class, multiple tables with discriminator

I doing a little investigation and I am wondering if the following is possible.
I am looking to create a BaseEntityWithDetails class that I can reuse for any type that I would like to have extendable. For example
public abstract class EntityDetail
{
}
This class is used to persist a key and value for the entity.
"Products" would be extended by doing the following...
public class ProductDetail : EntityDetail
{
}
public class Product : BaseEntityWithDetails<ProductDetail>
{
}
The base class "BaseEntityWithDetails" will provide some helper methods for setting and getting. What do you think?
What is the most effective way of mapping this with EF CodeFirst while being super easy to allow another type implement an DetailsEntityTypeConfiguration like the following
public class ProductMap : DetailsEntityTypeConfiguration<Product, ProductDetail>
{
}
Thanks in advance!
I would like to quote someone really smart on this: Reuse is a fallacy. Don't bother doing stuff like this because it will only make your design more obfuscated and complex. Save your inheritance to the entities in your domain which really share the same behavior, don't do this type of assumptions up front.
As a side note: You can map this as a table per type if you put your "EntityDetail" into your database, but as I said before, this is just not a good idea.