RavenDB Index on SubClasses - ravendb

I am trying to create an indexes for ProviderProfileId, Email, and Address1
I have created queries that work, but not indexes. I know the inheriting from List for the collections might be part of the problem. List is a carry over from when I had to do a significant amount of XmlSerialization on much older projects, and became a habit in my modeling. I also noticed that in Raven the serialization is much cleaner that if AddressCollection were just List. Any thoughts?
Model is similar to
public class Customer {
public string Id {get;set}
public string Name {get;set;}
public AddressCollection {get;set;}
public SocialMediaAliasCollection {get;set;}
}
public class SocialMediaAliasCollection:List<SocialMedialProfile>{}
public class SocialMediaProfile{
public string ProviderProfileId {get;set;}
public string Email {get;set;}
}
public class AddressCollection:List<Address>{}
public class Address{
public string Address {get;set;}
public string City {get;set;}
public string State {get;set;}
public string Zip {get;set;}
}

I now answered it, basically I didn't know linq well enough. Makes sense once I figured it out. I was trying to make an index for a sub collection, in this case addresses. Not 100% this works, but it does compile and when I push the index to the server it does not blow up.
Map = collection => from item in collection where item.AddressCollection != null
from item2 in item.AddressCollection
select new {
item2.city
}

Related

Automapper map registers (static data)

I have a DTO like this (note this is only a sample dto) :
public class SomeClassDTO
{
public guid Id {get;set;}
public string Name {get;set;}
public IList<Register1> Registers1 {get;set;}
public IList<Register2> Registers2 {get;set;}
}
and the class:
public class SomeClass
{
public guid Id {get;set;}
public string Name {get;set;}
}
I need something like this:
Mapper.CreateMap<SomeClass, SomeClassDTO>()
.ForMember(dto=>dto.Registers1, opt=>opt.MapFrom(src=>_session.Query<Register1>.ToList()))
.ForMember(dto=>dto.Registers2, opt=>opt.MapFrom(src=>_session.Query<Register2>.ToList()));
Where _session is an instance per request of the nhibernate session.
Ok, I need to get the collections in the dto populated from the values i get from DB using nhibernate session. (of course those collection has nothing to do with the class: SomeClass and they don't have any relations to it.
Dont know how to inject ISession to MapFrom or ResolveUsing.
The DI Container is Autofac, and the project type is WebApi2
Please Help

Several unique constrains on a single document in RavenDb

Let's say I have a class:
public class Person
{
public string Name{get;set;}
public string Email {get;set;}
public string GoogleId {get;set;}
public string FacebookId {get;set;}
}
If I want to make the email unique I will use the unique constraint bundle.
But I want to make both the googleId and the facebookId properties as a single unique constraint side by side with the email constraint (while non of them is the id).
Is it possible?
Use the UniqueConstraints bundle:
public class Person
{
public string Name {get;set;}
[UniqueConstraint]
public string Email {get;set;}
public string GoogleId {get;set;}
public string FacebookId {get;set;}
[UniqueConstraint]
public string GoogleAndFacebookIds { get;set; }
}
Just make sure you update GoogleAndFacebookIds everytime you update either GoogleId or FacebookId. I was doing this so much I ended up using a simple interface on all my classes that did this sort of thing:
public interface ICombinedConstraints
{
string UniqueId { get; set; }
void UpdateConstraints();
}
So,
public class Person : ICombinedConstraints
{
public string Name{get;set;}
[UniqueConstraint]
public string Email {get;set;}
public string GoogleId {get;set;}
public string FacebookId {get;set;}
[UniqueConstraint]
public string UniqueId { get; set; }
public void UpdateConstraints()
{
UniqueId = GoogleId + FacebookId;
}
}
You cannot do that. Raven doesn't provide any possibility to enforce unique constraints on the property. If you want to do that you need separate sets of documents (see link below)
Unique constraints in Raven
Update:
It seems that you can also try to use bundles to implement unique constraints in the object.
Bundle: Unique constraints

PetaPoco mapping properties within properties

I am new to PetaPoco and initially I was liking it but then hit a wall which I simply dont know how to search for.
I have a object which needs to set a property within one of its properties, ie Job.Min.BaseValue. The source of this data is "min_mb".
So basically my object is not a direct mapping of the source table
public class Usage
{
public Decimal BaseValue {get;set;}
public Decimal BaseScale {get;set;}
public Decimal BaseUnit {get;set;}
}
[PetaPoco.TableName("data")]
[PetaPoco.PrimaryKey("date, client_name")]
[PetaPoco.ExplicitColumns]
public class Job
{
[PetaPoco.Column("date")]
public DateTime Date {get;set;}
[PetaPoco.Column("client_name")]
public String ClientName {get;set;}
public Usage Min {get;set;}
public CommvaultJob() { Min = new Usage() { BaseScale=1024, BaseUnit="MB" }; }
}
I think you're just missing the extra type when you call Fetch or Query. This worked for me :
Calling PetaPoco :
var allData = _db.Fetch<TestJobPoco,Usage>("select * from dataTEST");
return View( allData);
The pocos :
[PetaPoco.ExplicitColumns]
public class Usage
{
public Usage()
{
BaseScale=1024;
BaseUnit="MB";
}
[PetaPoco.Column("base_value")]
public Decimal BaseValue {get;set;}
[PetaPoco.Ignore]
public Decimal BaseScale {get;set;}
[PetaPoco.Ignore]
public string BaseUnit {get;set;}
}
[PetaPoco.TableName("dataTEST")]
[PetaPoco.PrimaryKey("id")]
[PetaPoco.ExplicitColumns]
public class TestJobPoco
{
[PetaPoco.Column("id")]
public int Id {get;set;}
[PetaPoco.Column("date")]
public DateTime Date {get;set;}
[PetaPoco.Column("client_name")]
public String ClientName {get;set;}
public Usage Min {get;set;}
public TestJobPoco()
{
//Min = new Usage() { BaseScale=1024, BaseUnit="MB" };
}
}
My test database has an id, date, client_name and base_value columns. The primary key is id so it's slightly different than yours but this shouldn't change the way the poco mapping happens.
If your objects do not map with the table structure, an ORM can't help much.
You will need to do the mapping manually or made new shadow properties that copy the values of the other fields, but this added complexity will defeat the purpose of an ORM.

PetaPoco queries in a ViewModel

If I had a viewmodel that looked something like this
public class AddressViewModel
{
public int AddressID {get;set;}
public string Street {get;set;}
public string Town {get;set;}
public SelectList Countries { get; set; }
}
How would I go about populating this?
Would it be 2 queries to the database, 1 to populate the address info and another to return all countries into the SelectList?
I think so yes, maybe even cache it if the list values don't change.

Fluent NHibernate - Map 2 Identical classes to same table

I've seen this (unanswered) question asked once before, but in a different context. I'm looking to have two domain objects map to the same table, WITHOUT a discriminator. The two classes are:
public class Category
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual ReadOnlyCategory ParentCategory { get; private set; }
}
and
public class ReadOnlyCategory
{
public virtual int Id { get; private set; }
public virtual string Name { get; private set; }
public virtual ReadOnlyCategory ParentCategory { get; private set; }
}
The main difference is that all public properties of ReadOnlyCategory are read-only. My idea here is that I want all users of this class to know that they should only mess with the category they are currently 'looking' at, and not any other categories in the hierarchy. (I've left off other properties regarding the subcategories.)
Clearly, in the database, Category and ReadOnlyCategory are the same thing, and NHibernate should treat them very similarly when persisting them. There are three problems wrapped into one here:
1) How do I do the mapping?
2) When instantiating the objects, how do I control whether I instantiate Category or ReadOnlyCategory?
3) When persisting the objects, will the mapping be smart enough, or do I need to use an extensibility point here?
Any pointers on how I can get this to happen?
(Or am I crazy?)
This looks like wrong object model design to me. I don't see a good reason to introduce a new class just for authorisation reasons (whether user allowed to modify a given category object?). You may as well use one class and throw for example InvalidOperationException if an end user is not supposed to modify a category.