Deserialization of GeoJSON - geojson

I have problem with deserialization of response:
In one case I will get polygon, in other - multipolygon, so for this cases description of property coordinates will be different:
in case of polygon :
public class Geometry
{
public string type { get; set; }
public List<List<List<double>>> coordinates { get; set; }
}
in case of multipolygon:
public class Geometry
{
public string type { get; set; }
public List<List<List<List<double>>>> coordinates { get; set; }
}
And I don’t know in advance which object I will receive. How to solve this problem?

It looks like you are working in .NET. Consider using a library like GeoJSON.NET or the GeoJSON extension to NetTopologySuite (good if you want to do a bunch of calculations).
Here are some useful resources:
https://github.com/bamcis-io/GeoJSON
https://github.com/GeoJSON-Net/GeoJSON.Net
https://www.nuget.org/packages/NetTopologySuite.IO.GeoJSON

Related

copy contents of dto to another similar dto

I have 2 very similar dto object. I have shown a sample code below, but the actual 2 dto with which I am working have 39 and 40 properties in it.
My question is that is there an easier way to copy contents of QuoteDto to Quote2Dto.
I am makinga call to a legacy project which gives me QuoteDto object. I than have to make call to a new rest service project which only accepts Quote2Dto object
Let me know if you need any more code.
public abstract class QuoteDto
{
public virtual bool IsWaive { get; set; }
public virtual bool IsExpired { get; set; }
}
public abstract class Quote2Dto
{
public virtual bool IsWaive { get; set; }
public virtual bool IsExpired { get; set; }
public virtual bool IsCancel { get; set; }
}
Usually Automapper (or similar library) is used. It can copy same properties without any pre-configuration needed. But you can always configure more advanced property mappings.
Here you can find Getting Started Guide.
You have two options here.
Your first options is to use AutoMapper to copy properties. There are some advanced configurations to AutoMapper if you would need more advanced configuration.
The second option is to create a method inside your DTO, that takes the other DTO and copies the properties.(Basically copying the properties manually.)
It would look something like this:
class FirstSampleDTO
{
public int RandomProperty { get; set; }
public int RandomProperty2 { get; set; }
public int RandomProperty3 { get; set; }
private void CopyDTOData(SecondSampleDTO dto)
{
dto.RandomProperty = this.RandomProperty;
dto.RandomProperty2 = this.RandomProperty2;
dto.RandomProperty3 = this.RandomProperty3;
}
}
class SecondSampleDTO
{
public int RandomProperty { get; set; }
public int RandomProperty2 { get; set; }
public int RandomProperty3 { get; set; }
private void CopyDTOData(FirstSampleDTO dto)
{
dto.RandomProperty = this.RandomProperty;
dto.RandomProperty2 = this.RandomProperty2;
dto.RandomProperty3 = this.RandomProperty3;
}
}
(This in in case you already have the data of the dto.)
I would suggest using the second method if you are not going to do that much of a mapping.

How can I get Vertices with relations and related Vertices in Neo4j 1.9RC1 via Gremlin

Neo4j will support tree pattern in 2.x versions.(We could not use tree function) We are using 1.9RC1.
I need to get Users with Followers and Friends.
public class User
{
public long Id { get; set; }
public string Name { get; set; }
...............
}
public class UserModel
{
public long Id { get; set; }
public string Name { get; set; }
public string DetailedInformation { get; set; }
public IEnumerable<UserModel2> Followers { get; set; }
public IEnumerable<UserModel2> Friends{ get; set; }
}
public class UserModel2
{
public long Id { get; set; }
public string Name { get; set; }
}
I want to get tree structured UserModel response.
How can be done via Gremlin..
We were using paths function.
g.v(4582).inE.outV.paths{it}
But there is data duplication problem for it.
It returns paths not tree.
PS: We are using C#.
If you look how tree() is implemented, it is simply path() aggregated at join points. The join points are vertices at the same depth. Thus, you could create your own tree data structure from the results of path() (--or paths() in older versions of Gremlin). Let me explain how to implement it via example. If you have a set of paths like this:
[1,2,3,4]
[1,3,5,6]
[1,2,3,5]
Then the tree representation would be:
4
/
2-3
/ \
1 5
\
3-5-6
A smart use of embedded HashMaps will deliver the functionality you desire. Please look over the Pipes 2.x codebase to see how Tree is implemented and copy (and adjust) to your needs.

Using CreateSourceQuery in CTP4 Code First

I'm guessing this is impossible, but I'll throw it out there anyway. Is it possible to use CreateSourceQuery when programming with the EF4 CodeFirst API, in CTP4? I'd like to eagerly load properties attached to a collection of properties, like this:
var sourceQuery = this.CurrentInvoice.PropertyInvoices.CreateSourceQuery();
sourceQuery.Include("Property").ToList();
But of course CreateSourceQuery is defined on EntityCollection<T>, whereas CodeFirst uses plain old ICollection (obviously). Is there some way to convert?
I've gotten the below to work, but it's not quite what I'm looking for. Anyone know how to go from what's below to what's above (code below is from a class that inherits DbContext)?
ObjectSet<Person> OSPeople = base.ObjectContext.CreateObjectSet<Person>();
OSPeople.Include(Pinner => Pinner.Books).ToList();
Thanks!
EDIT: here's my version of the solution posted by zeeshanhirani - who's book by the way is amazing!
dynamic result;
if (invoice.PropertyInvoices is EntityCollection<PropertyInvoice>)
result = (invoices.PropertyInvoices as EntityCollection<PropertyInvoice>).CreateSourceQuery().Yadda.Yadda.Yadda
else
//must be a unit test!
result = invoices.PropertyInvoices;
return result.ToList();
EDIT2:
Ok, I just realized that you can't dispatch extension methods whilst using dynamic. So I guess we're not quite as dynamic as Ruby, but the example above is easily modifiable to comport with this restriction
EDIT3:
As mentioned in zeeshanhirani's blog post, this only works if (and only if) you have change-enabled proxies, which will get created if all of your properties are declared virtual. Here's another version of what the method might look like to use CreateSourceQuery with POCOs
public class Person {
public virtual int ID { get; set; }
public virtual string FName { get; set; }
public virtual string LName { get; set; }
public virtual double Weight { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Book {
public virtual int ID { get; set; }
public virtual string Title { get; set; }
public virtual int Pages { get; set; }
public virtual int OwnerID { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
public virtual Person Owner { get; set; }
}
public class Genre {
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual Genre ParentGenre { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class BookContext : DbContext {
public void PrimeBooksCollectionToIncludeGenres(Person P) {
if (P.Books is EntityCollection<Book>)
(P.Books as EntityCollection<Book>).CreateSourceQuery().Include(b => b.Genres).ToList();
}
It is possible to add a method to you derived context that creates a source query for a given navigation on an entity instance. To do this you need to make use of the underlying ObjectContext which includes a relationship manager which exposes underlying entity collections/references for each navigation:
public ObjectQuery<T> CreateNavigationSourceQuery<T>(object entity, string navigationProperty)
{
var ose = this.ObjectContext.ObjectStateManager.GetObjectStateEntry(entity);
var rm = this.ObjectContext.ObjectStateManager.GetRelationshipManager(entity);
var entityType = (EntityType)ose.EntitySet.ElementType;
var navigation = entityType.NavigationProperties[navigationProperty];
var relatedEnd = rm.GetRelatedEnd(navigation.RelationshipType.FullName, navigation.ToEndMember.Name);
return ((dynamic)relatedEnd).CreateSourceQuery();
}
You could get fancy and accept a Func for the navigation property to avoid having to specify the T, but here is how the above function is used:
using (var ctx = new ProductCatalog())
{
var food = ctx.Categories.Find("FOOD");
var foodsCount = ctx.CreateNavigationSourceQuery<Product>(food, "Products").Count();
}
Hope this helps!
~Rowan
It is definately possible to do so. If you have marked you collection property with virtual keyword, then at runtime, you actual concrete type for ICollection would be EntityCollection which supports CreateSourceQuery and all the goodies that comes with the default code generator. Here is how i would do it.
public class Invoice
{
public virtual ICollection PropertyInvoices{get;set}
}
dynamic invoice = this.Invoice;
dynamic invoice = invoice.PropertyInvoices.CreateSourceQuery().Include("Property");
I wrote a blog post on something similar. Just be aware that it is not a good practice to rely on the inner implementation of ICollection getting converted to EntityCollection.
below is the blog post you might find useful
http://weblogs.asp.net/zeeshanhirani/archive/2010/03/24/registering-with-associationchanged-event-on-poco-with-change-tracking-proxy.aspx

How should be my DTO object for ASP.Net MVC View?

i'd like to know, I have a application in asp.net mvc and nhibernate. I've read about that in the Views on asp.net mvc, shouldn't know about the Domain, and it need use a DTO object. So, I'm trying to do this, I found the AutoMapper component and I don't know the correct way to do my DTOS, for some domain objects. I have a domain class like this:
public class Entity
{
public virtual int Id { get; set; }
public virtual bool Active { get; set; }
}
public class Category : Entity
{
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; set; }
public Category() { }
}
public class Product : Entity
{
public virtual string Name { get; set; }
public virtual string Details { get; set; }
public virtual decimal Prince { get; set; }
public virtual int Stock { get; set; }
public virtual Category Category { get; set; }
public virtual Supplier Supplier { get; set; }
public Product() { }
}
public class Supplier : Entity
{
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; set; }
public Supplier() { }
}
I'd like to get some example of how can I do my DTOs to View ? Need I use only strings in DTO ? And my controllers, it should get a domain object or a DTO and transform it on a domain to save in repository ?
Thanks a lot!
Cheers
There is no guidelines on this matter and it depends on your personal chice. I have few advices that have proven useful in practice:
1. Use flat DTOs - this means that the properties of the DTO must be as primitive as possible. This saves you the need for null reference checking.
For example if you have a domain object like this:
public class Employee
{
prop string FirstName{get; set;}
prop string LastName{get; set;}
prop Employee Boss{get; set;}
...
}
And you need to output in a grid a list of employees and display information for their 1st level boss I prefer to create a DTO
public class EmployeeDTO
{
prop string FirstName{get; set;}
prop string LastName{get; set;}
prop bool HaveABoss{get;set}
prop string BossFirstName{get; set;}
prop string BossLastName{get; set;}
...
}
or something like this (-:
2. Do not convert everything to sting - this will bind the DTO to a concrete view because you'll apply special formatting. It's not a problem to apply simple formatting directly in the view.
3. Use DTOs in your post actions and than convert them to domain objects. Usually controller's actions are the first line of deffence against incorrect data and you cannot expect to be able to allways construct a valid domain object out of the user's input. In most cases you have to do some post-processing like validation, setting default values and so on. After that you can create your DTOs.

Problem with DataAnnotations in partial class

So in my mvc project's Project.Repository I have
[MetadataType(typeof(FalalaMetadata))]
public partial class Falala
{
public string Name { get; set; }
public string Age { get; set; }
internal sealed class FalalaMetadata
{
[Required(ErrorMessage="Falala requires name.")]
public string Name { get; set; }
[Required(ErrorMessage = "Falala requires age.")]
public string Age { get; set; }
}
}
I use Falala as a model in my Project.Web.AccountControllers, and use a method to get violations.
Validating worked when I had
public class Falala
{
[Required]
public string Name { get; set; }
[Required(ErrorMessage="error")]
public string Age { get; set; }
}
but not after using the partial class from above.
I really need to use a partial class. What am I doing wrong here?
Thanks!
I tend to use Metadata classes as followed.
[MetadataType(typeof(FalalaMetadata))]
public partial class Falala
{
public string Name { get; set; }
public string Age { get; set; }
}
public class FalalaMetadata
{
[Required(ErrorMessage="Falala requires name.")]
public string Name { get; set; }
[Required(ErrorMessage = "Falala requires age.")]
public string Age { get; set; }
}
Which works fine for me.
The following should also work (and is a better way to implement metadata classes):
[MetadataTypeAttribute(typeof(Falala.FalalaMetaData))]
public partial class Falala
{
internal sealed class FalalaMetadata
{
[Required(ErrorMessage="Falala requires name.")]
public string Name { get; set; }
[Required(ErrorMessage = "Falala requires age.")]
public string Age { get; set; }
}
}
I ran into a similar problem and finally got it working by putting both the Model class and the Metadata "buddy" class in the same namespace, even though my references seemed ok. I'm kind of a .net noob though so I'm not exactly comfortable with namespaces, could be something else.
Could Internal on the nested class be the reason...?
I had a similiar problem and it seemed to all boiled down to not making the individual fields in the nested metadata class public - wonder if making the whole class internal causes the same problem?
Not sure if this help, but I had a similar problem and spend days on it. At the end it was just a minor change which did the trick for me.
I changed UnobtrusiveJavaScriptEnabled to false in the config file
Good luck

Resources