What approach should I take when serializing an object to the database, keeping in mind that the objects properties will change in time?
At first, I thought of making sure all my objects implement an interface, but what happens when an interface looses a property and gains another. What happeneds to the existing serialized data in the database upon restoration?
Should I use abstract classes or interfaces? Best approach people have used.
Thank you
The partial implementation below may give you some ideas for flexibly storing and retrieving properties. For managing property addition and removal, when you add properties no problem. When you drop properties you could not allow new properties of that type to be created in the future but preserve existing data or remove all data associated with those properties.
/// <summary>
/// The data storage could look something like this
/// create table PersistedObject (ObjectId int )
/// create table PersistedProperty (PropertyId int , PropertyName varchar(50) )
/// create table Data (ValueId int, PropertyId int, SerializedValue image )
/// </summary>
interface IFlexiblePersistence
{
object this[string propertyName] { get; set;}
void Persist();
}
class Person : IFlexiblePersistence
{
Dictionary<string, object> data;
public Person(int personId)
{
data = PopulatePersonData(personId);
}
public object this[string propertyName]
{
get { return data[propertyName]; }
set
{
data[propertyName] = value;
Persist();
}
}
public void Persist()
{
LoopThroughAllValuesAndSaveToDB();
}
}
what will the life time of the objects be? if it was only short term you could use datasets and update the properties on the fly.
That's a time honored question with no one correct answer.
It depends greatly on how the data will be used in your application. The amount of RAM you'll want to cap the use at, as well as the anticipated largest size for your database. All of those factors will push you toward or away from various approaches.
This article discusses datatables and their use within that specific app.
This article discusses the use of data transfer objects.
This forum discussion gives many opinions on data transfer objects vs domain modeling.
Approaches 2 and 3 above are fully compatible with David Silva Smiths IFlexiblePersistence approach listed in his reply. However, you potentially use more memory and lose much of the performance gains from doing the "typical" DTO. If memory and speed aren't a concern, his approach will likely be very simple.
It depends what you mean by "serialization" (it is an overloaded term). If you mean your existing object as columns, then and ORM: EF, LINQ-to-SQL, NHibernate, etc. If you mean your object as a single varbinary(max) / varchar(max):
Whatever else you do, don't use BinaryFormatter - this will break if you change the fields (for example, changing a regular property into an auto-property). You need something contract-based, most of which will be fine with changes. Suggestions:
XmlSerializer / DataContractSerializer / Json.NET (text)
protobuf-net (binary, uses google's "protocol buffers" wire format)
With the above you can generally use pretty standard DTOs; for example (compatible with DataContractSerializer and protobuf-net):
[DataContract]
public class Customer {
[DataMember(Order=1)]
public int {get;set;}
[DataMember(Order=2)]
public string Name {get;set;}
}
I wouldn't use NetDataContractSerializer for similar reasons to BinaryFormatter - fine for short-lived requests, but not good for storage. Note that most serializers use concrete classes, since very few include the type metadata in the output (and hence would need additional configuration to tie ICustomer to your Customer class).
Related
I'm using C# 4.0, Asp.Net. I have a problem regarding the proper construction of a readonly structure within a custom cache I created.
Details (summary) :
My CacheManager class (singleton) uses, as parameter, an instance of the existing MemoryCache class and wraps around a few helpful methods to deal with supplementary stuff such as object life cycle within my custom cache.
That Manager deals with a simple CachableObject that takes three variables :
object
DateTime
int (duration)
In summary, my custom cache Manager stores objects for a limited amount of time in order to protect my database from frequent big queries.
Lately, I tried to :
Got back an object from the cache (ie : stored under the key -MyList)
Casted it back to a list of complexe objects
Translated the content of some properties for each complexe objects
Stored again the freshly translated list of objects within the cache, (under another key -MyTranslatedList)
The problem :
During my testing, it appeared to me that both lists stored in the cache (raw and translated one) were refering to the same underlying objects. Therefore, once translated, those objects were actually translated in both lists.
Since each list only has references to the objects, that's a perfectly normal behavior and a silly mistake from me.
The question :
As you can easily guess now, I would like to protect myself and other users of my singleton for that kind of mistakes.
I would like to insert (or store or get) any kind of object (or list of complexe objects) so they cannot be altered by anybody getting them through the cache. I would like the data within my cache to be readonly (and deeply readonly) to avoid having that kind of problem. I want anybody to have to create a deep copy (or even better, to get one) before starting to use the data stored within the cache.
What I tried so far :
I tried to make the object readonly. It didn't work as expected.
Since I'm often storing list of complexe objects, I've found the AsReadOnly method that return a IReadOnlyCollection, but while this prevents me from altering the list (add, remove) it doesn't protect the objects that are within the list.
I hope my explanation is somewhat understandable :) Is there a neat way of dealing with that kind of situation ?
I would create a class where the properties are readonly:
class ReadonlyClass
{
private string p1;
private int p2;
public ReadonlyClass(string property1, int property2)
{
p1 = property1;
p2 = property2;
}
public string Property1
{
get { return p1; }
}
public int Property2
{
get { return p2; }
}
}
If the properties are objects/other classes, you should implement a clone function that returns a copy of the object. The clone function for the above class would looke like this:
public ReadonlyClass clone()
{
return new ReadonlyClass(p1, p2);
}
Best regards
Hans Milling...
I'm modifying an app for performance gains. The app has a class with many properties. Typically this class is populated in its entirety by a primary key that pulls a large query from a database. The application is in great part slow because this happens constantly throughout, even though much of the time only one two properties in the class are needed in a given section of code. The existing large class has only a default constructor and all of its properties are nullable or have default values. In code below ignore lack of constructors and how these objects are populated.
public class Contract
{
public enum ContractStatus
{
Draft, Active, Inactive
}
private Int32 contractId = DALC.DefaultInt32;
private String name = DALC.DefaultString;
private ContractStatus status;
private ContractType contractType = null;
private CurrencyType currencyType = null;
private Company company = null;
}
As you can see it has its own properties, and also references other classes (e.g. ContractType, Company).
A few approaches I've thought of in light of common design patterns:
1) re-factor this hugely and break up those smaller sub-sections into their own classes with their own properties. Then reconstruct that large class with all of the smaller ones when it is needed. This will be quite laborious, though even if it sounds ideal and consistent with SOLID OOD principles.
2) Create new classes that simply contain the large class, but only expose one or two of its properties. I'm still creating a full blown version of the original, large class, but I will only populate the data I need. This will be via simple DB query, thus the bulk of the class will sit there unused and its null default classes it's referencing won't ever be constructed.
public class ContractName
{
Contract contract;
public ContractName()
{
contract = new Contract();
}
public String Name
{
get { return contract.Name; }
set { contract.Name = value; }
}
}
3) Add new constructors to existing large class with a parameter indicating what chunks of code I want to actually populate. This sounds messy and kind of nasty and wrong, and would leave me in the scenario where if Contract is created by a contract ID in one section of code it has different info than if created by contract ID elsewhere.
Thanks for any ideas!
I would recommend option 1: extract the classes you think you need to extract now. The other two options are just adding more technical debt which will take even longer to resolve in the future. Well-factored code is usually much easier to optimise than big complicated classes.
In my experience, breaking up classes is not all that laborious. In fact I usually find myself surprised by how quickly I can execute refactorings like Extract Class as long as I follow the recipe.
New to MongoDB. Set up a C# web project in VS 2013.
Need to insert data as document into MongoDB. The number of Key-Value pair every time could be different.
For example,
document 1: Id is "1", data is one pair key-value: "order":"shoes"
document 2: Id is "2", data is a 3-pair key-value: "order":"shoes", "package":"big", "country":"Norway"
In this "Getting Started" says because it is so much easier to work with your own domain classes this quick-start will assume that you are going to do that. suggests make our own class like:
public class Entity
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}
then use it like:
var entity = new Entity { Name = "Tom" };
...
entity.Name = "Dick";
collection.Save(entity);
Well, it defeats the idea of no-fixed columns, right?
So, I guess BsonDocument is the the model to use and is there any good samples for beginners?
I'm amazed how often this topic comes up... Essentially, this is more of a 'statically typed language limitation' than a MongoDB issue:
Schemaless doesn't mean you don't have any schema per se, it basically means you don't have to tell the database up front what you're going to store. It's basically "code first" - the code just writes to the database like it would to RAM, with all the flexibility involved.
Of course, the typical application will have some sort of reoccurring data structure, some classes, some object-oriented paradigm in one way or another. That is also true for the indexes: indexes are (usually) 'static' in the sense that you do have to tell mongodb about which field to index up front.
However, there is also the use case where you don't know what to store. If your data is really that unforeseeable, it makes sense to think "code first": what would you do in C#? Would you use the BsonDocument? Probably not. Maybe an embedded Dictionary does the trick, e.g.
public class Product {
public ObjectId Id {get;set;}
public decimal Price {get;set;}
public Dictionary<string, string> Attributes {get;set;}
// ...
}
This solution can also work with multikeys to simulate a large number of indexes to make queries on the attributes reasonably fast (though the lack of static typing makes range queries tricky). See
It really depends on your needs. If you want to have nested objects and static typing, things get a lot more complicated than this. Then again, the consumer of such a data structure (i.e. the frontend or client application) often needs to make assumptions that make it easy to digest this information, so it's often not possible to make this type safe anyway.
Other options include indeed using the BsonDocument, which I find too invasive in the sense that you make your business models depend on the database driver implementation; or using a common base class like ProductAttributes that can be extended by classes such as ProductAttributesShoes, etc. This question really revolves around the whole system design - do you know the properties at compile time? Do you have dropdowns for the property values in your frontend? Where do they come from?
If you want something reusable and flexible, you could simply use a JSON library, serialize the object to string and store that to the database. In any case, the interaction with such objects will be ugly from the C# side because they're not statically typed.
In my current project, I have quite a few objects I need to persist to XML and inflate at runtime. I've been managing this through .NET's DataContracts. What I am doing right now is creating a separate class that represents the objects I'm serializing and reading/writing those to/from disc to avoid having too much responsibility in a single class. Here's an example:
public class Range
{
private float _min;
private float _max;
public float Min { get { return this._min; } }
public float Max { get { return this._max; } }
// Constructrs & Methods...
public SerializedRange GetAsSerializable();
}
The Range class has the complimentary class:
[DataContract]
public class SerializedRange
{
[DataMember]
public float Min;
[DataMember]
public float Max;
// Constructor...
}
My question then is, who should be responsible for actually taking the Serialized version of the object and inflating it into the actual object? I see three options, but I'm not sure which (if any of them) would be the best:
Give the Serialized version of the object an instance method that spits out an inflated instance, using the available constructors/factories of the sister class.
Give the sister class a factory that takes an instance of the serialized version to construct itself.
Don't have either the class or it's Serializable counterpart do anything- have the code that reads in the Serialized objects manually create the regular objects using whatever constructors/factories they'd regularly have.
I realize that in certain situations you'd have to do it one way or the other because of constraints outside of this somewhat contrived example. Since there's more then one way to do it though, what I'm really looking for is a general rule of thumb that yields neat, readable, and expandable code.
If you 'break' you application into constituent parts what logical components would you get? Here are few based on my understanding:
Domain Objects (Data that you are storing)
Data Layer - responsible for persisting the data (and retrieving it)
and many others (but just taken a subset as per your description)
Now, the job of the data layer is to write the content out to some storage - XML files to disk in your case.
Now, when you 'query' the file who fetches it? The data layer. Who 'should' populate the corresponding domain object? Well the data layer itself.
Should the data layer 'delegate' the responsibility of population to a separate class/factory? It depends if it's ever going to be reused by someone else. If not, concepts like inner classes can be of good help (they exist in the java world, not sure of it's equivalent in C#.NET). That way you'll have it modularized into a specific class, which is not publicly visible to other classes, unless you want it that way.
Should you go with factory? Yes, you may. But make sure it's logically correct to do so. You could land up with many object inflators - that could isolate the inflation functionality to one class and the factory could itself be a part of the data layer (if you want it that way).
Once you delineate the concerns you'll be in a better position to decided where to put that piece of code. I've provided some pointers that could come in handy.
Hope it helps...
I'm wondering what Data Structure people would recommend to do the following. I have a Class which has three main properties eg.
public class Example {
public Object One { get; }
public Object Two { get; }
public Object Three { get; }
}
Another class contains a collection of these Objects and frequently needs to enumerate over these which I do mainly with LINQ. A lot of the time though I need to lookup/enumerate only a subset of these objects based mainly on the value of property One so what I'd like to do is store these in an efficient data structure based on that property. I could do something like the following:
Dictionary<Object,List<Example>>
But this strikes me as being very inefficient, I know I need some kind of Hash Table but having never used one before in C# I'm unsure of what there is to use.
Some other requirements/notes:
All the Objects are immutable and have fixed Hash Codes which are computed from the values the class gets instantiated with in the constructors
Must be able to store multiple items that have the same value (and thus Hash Code) for property One in the same 'slot' in the data structure
Must be able to freely add and remove Objects from the collection
Indexed LINQ may be able to help you here. It provides an in-memory collection, but lets you attribute properties on your objects as Indexable so that it can create efficient queries against them.
PowerCollections (http://www.codeplex.com/PowerCollections) has a MultiDictionary container - perhaps you could try that?
or HybridDictionary