Automapper map registers (static data) - asp.net-web-api2

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

Related

how to make httppost actionresult bind to only part of the viewModel

I have the following sudo-code
class viewModel
{
public ICollection<modelA> parentModel
public modelC formModel
}
class modelA
{
public int ID {get;set;}
public virtual Icollection<modelB> {get;set;}
}
class modelB
{
public int ID {get;set;}
public string SomeString {get;set;}
public virtual modelA ModelA {get;set;}
}
class modelC
{
public int ModelAID {get;set;}
public string SomeString {get;set;}
}
So. the view model contains a collection of As. Each A contains a collection of Bs and there is a separate model for posting back as a form: the form will be repeated on the page, once in each instance of A with the A.ID passed in to ModelAID as a hidden field. Only one form posting is allowed on the page, The id of the form fields are formModel.ModelAID and .formModel.SomeString as they are derived from the non-parent element of the viewModel.
How do I get the ActionResult to bind only to formModel?
[HttpPost]
Public ActionResult Input(formModel vm)
{
... by default the view model being passed back is full VM, I only want the formModel so the post signature does not match
}
You can try something like
public ActionResult Input([Bind(Prefix = "formModel ")]modelC model)
{
}

Complex Entity aggregation

Hi have are very complex design of entities aggregation in project.
I'm using domain driven development.
To map entites i'm using FluentNHibernate
Here is my design of domain
public abstract class Domain
{
public virtual long Id{get;set;}
}
public ClassA:Domain
{
public virtual DateTtime Time{get;set;}
public virtual ClassC C1 {get;set;}
public virtual IList List{get;set;}
}
public ClassC:Domain
{
public virtual string Schedule{get;set;}
}
public ClassD:Domain
{
public virtual string PropClassD{get;set;}
}
public ClassE:ClassD
{
public virtual string PropClassE{get;set;}
}
public ClassF:ClassD
{
public virtual string PropClassF{get;set;}
}
public ClassG:Domain
{
//Composite key ClassA+ClassD
public virtual ClassA C1 {get;set;}
public virtual ClassD C2 {get;set;}
}
I tried different settings with mapping, but nothing is working. My aggregate root is ClassA.
Following Single class inhertance for ClassE and ClassF table.
Please help to make it work.
When I don't know how to map the objects I use Entity Developer http://www.devart.com/entitydeveloper/ They have a free version that supports up to eight entities. At least can give you some hints in a concrete case. Given the relative lack of Fluent NHibernate documentation, this tool is also educational. You can see an example of the mappings produced by this tool here http://www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat

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.

How do I map the same child entity to different tables if it is appears under different entities in Entity Framework 4.3?

I need to map the following class hierarchy using Entity Framework 4.3.
public abstract class Rule
{
public long Id {get;set;}
public abstract ICollection<Parameter> Parameters {get;set;}
}
public class Entity1
{
public long Id {get;set;}
public ICollection<Rule> Rules {get;set;}
// Map Rule to table Entity1Rules and
// Parameters to table Entity1RuleParameters
}
public class Entity2
{
public long Id {get;set;}
public ICollection<Rule> Rules {get;set;}
// Map Rule to table Entity2Rules and
// Parameters to table Entity2RuleParameters
}
Thanks
It is not possible. Each entity can be mapped only once. It would require you to involve some inheritance in Rule and Parameter classes but at the end your Entity1 and Entity2 will need to reference derived rule to set correct structure.

RavenDB Index on SubClasses

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
}