nHibernate3 + FluentNHibernate - "CreateDate" member not available on Update - nhibernate

My domain objects have a "CreateDate" and "ModidfyDate" members (DateTime).
When a user update a domain object (Asp.Net MVC) my view model does not hold these values. (It's to be set in my repository "Create" and "Update" methods)
So when I Update an object, I do not have the "CreateDate" available and therefore the Update method will fail.
I seems to have 2 options, and I don't like either:
1) Have my viewmodel tag along the "CreateDate" property (hidden field in form) so I have the original CreateDate available.
2) Or, in my "Update" repository method, first get the original record from the database and set the object I'm about to update's CreateDate member (an unnecessary call to the db)
What is the "normal" way to work with this?

A nice way to handle CreateDate and ModifyDate would be by using NHibernate's event listeners.
Here are some samples on how to create simple auditing using IPreInsertEventListener and IPreUpdateEventListener:
http://ayende.com/blog/3987/nhibernate-ipreupdateeventlistener-ipreinserteventlistener
http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx
On a side note, you shouldn't have problems with CreateDate on your Update method. How does your Update method look like?
The usual workflow is to get your view model in POST ActionMethod, load the entity object from the database using NHibernate's ISession, or your custom repository and then map your properties from the view model to the entity, either by hand, or using a tool like AutoMapper. Properties like CreateDate should be ignored in the viewmodel to entity mapping.

Related

Neo4j APOC trigger and Manual Index on Relationship Properties

I'd like to setup Neo4j APOC trigger that will add all relationship properties to manual index, something like the following:
CALL apoc.trigger.add('HAS_VALUE_ON_INDEX',"UNWIND {createdRelationships} AS r MATCH (Decision)-[r:HAS_VALUE_ON]->(Characteristic) CALL apoc.index.addRelationship(r,['property_1','property_2']) RETURN count(*)", {phase:'after'})
The issue is that I don't know the exact set of HAS_VALUE_ON relationship properties because I use the dynamic properties approach with Spring Data Neo4 5.
Is it possible to change this trigger declaration to be able to add all of the HAS_VALUE_ON relationship properties(existing and ones that will be created in future) to the manual index instead of the preconfigured ones( like ['property_1','property_2'] in the mentioned example) ?
If you do not know the set of properties in advance, then you can use the keys function to add all properties of the created relationships to the index:
CALL apoc.trigger.add(
'HAS_VALUE_ON_INDEX',
'UNWIND {createdRelationships} AS r MATCH (Decision)-[r:HAS_VALUE_ON]->(Characteristic)
CALL apoc.index.addRelationship(r, keys(r)) RETURN count(*)',
{phase:'after'}
)

Can I delete an entity that is not in cache?

I want to delete a record from the DB that hasn't been retrieved from a breeze query. The entity hasn't been retrieved so it's not in the cache, but I know the KEY of the record from another operation. Here's what I've tried:
create a new entity from the manager:
manager.createEntity(entityNames.book);
setting the ID :
bookToDelete().bookID(1); // bookToDelete is a ko observable from step 1
updating the state:
bookToDelete().entityAspect.setDeleted();
When I save changes, this transaction is not included in the JSON
You almost have it. Calling entityAspect.setDeleted on an 'Added' entity moves it directly to a 'Detached' state, which in effect removes it from the EntityManager, and hence it cannot be saved. This is deliberate. It handles the case where you create an entity and later delete it. In this case, there is no entity to save.
So, in your case, you have to make the entity either 'Modified' or 'Unchanged' before you call entityAspect.setDeleted. You can do this by either calling entityAspect.setUnchanged or entityAspect.setModified before calling entityAspect.setDeleted or you can call entityAspect.acceptChanges.
Note that you will also have to insure that the 'clone' entity passes validation and if you have a concurrency field on the entity, you will need to set this appropriately as well.
UPDATE Dec 7th
You can create the book entity in the marked-for-delete state in a single step as shown:
var book = manager.createEntity(entityNames.book,
{ BookID: 1 }, // use initializer to set the key
breeze.EntityState.Deleted); // creates the entity in the Deleted state
Be sure to initialize it with all other properties that are necessary for the entity to pass validation and optimistic concurrency checks on the server.
No problem if you don't have these checks. Not sure how you'd get those values without querying the server if you did have such checks.
got it. cant delete entity while still in added state. I first setModified. then setdeleted. didnt see any side affects.

Entity Framework 4.1 Dynamically retrieve mapped column name

I am trying to construct an SQL statement dynamically.
My context is created dynamically, using reflection finding classes deriving from EntityTypeConfiguration and adding them to DbModelBuilder.Configuration.
My EntityTypeConfiguration classes specify HasColumnName to map the Entity property name to db table column name, which I need to construct my SQL statement.
namespace MyDomain {
public class TestEntityConfig : EntityTypeConfiguration<TestEntity>{
Property("Name").HasColumnName("dbName");
}
}
From What I have researched, it seems I can get access to this information through MetadataWorkspace, which I can get to through ObjectContext.
I have managed to retrieve the the entity I am interested in with MetadataWorkspace.GetItem("MyDomain.TestEntity",DataSpace.OSpace), which gives me access to Properties, but none of the properties, of Properties, give me the name of the mapped db column, as specified with HasColumnName.
Also I am not clear what DataSpace.OSpace is and why my model is constructed in this space.
If Anyone can shed some light on this I would be grateful
UPDATE
Further to #Ladislav's comments. I discovered I can get the information as follows
For the class properties
ctx.MetadataWorkspace.GetItem<ClrEntityType>("MyDomain.TestEntity", DataSpace.OSpace)).Members
For the table properties
ctx.MetadataWorkspace.GetItem<EntityType>("CodeFirstDatabaseSchema.TestEntity",SSpace).Members
So given that I only know the type MyDomain.TestEntity and Memeber "Name". How would I go about to get "dbName". Can I always assume that my mapped class will be created in CodeFirstDatabaseSchema, om order to dynamically construct the identity to retrieve it from SSpace and how would I get to the correct Member in SSpace. Can I do something like
var memIndex = ctx.MetadataWorkspace.GetItem<ClrEntityType>("MyDomain.TestEntity", DataSpace.OSpace)).Members["Name"].Index;
var dbName = ctx.MetadataWorkspace.GetItem<EntityType>("CodeFirstDatabaseSchema.TestEntity",SSpace).Members[memIndex];
MetadataWorkspace contanis several containers specified by DataSpace. Interesting for you are:
CSpace - description of conceptual model (this should contain properties)
CSSpace - mapping of conceptual model to storage model (this should contain how classes / properties are mapped to tables / columns)

NHibernate: How to get mapped values?

Suppose I have a class Customer that is mapped to the database and everything is a-ok.
Now suppose that I want to retrieve - in my application - the column name that NH knows Customer.FirstName maps to.
How would I do this?
You can access the database field name through NHibernate.Cfg.Configuration:
// cfg is NHibernate.Cfg.Configuration
// You will have to provide the complete namespace for Customer
var persistentClass = cfg.GetClassMapping(typeof(Customer));
var property = persistentClass.GetProperty("FirstName");
var columnIterator = property.ColumnIterator;
The ColumnIterator property returns IEnumerable<NHibernate.Mapping.ISelectable>. In almost all cases properties are mapped to a single column so the column name can be found using property.ColumnInterator.ElementAt(0).Text.
I'm not aware that that's doable.
I believe your best bet would be to use .xml files to do the mapping, package them together with the application and read the contents at runtime. I am not aware of an API which allows you to query hibernate annotations (pardon the Java lingo) at runtime, and that's what you would need.
Update:
Judging by Jamie's solution, NHibernate and Hibernate have different APIs, because the Hibernate org.hibernate.Hibernate class provides no way to access a "configuration" property.

Stored Proc in RIA Services

I want to load some data with a SP.
I've put a SP in a Linq to SQL Class and I don't know how to use it for loading it's data in a datagrid.
In LinqToSqlDomainService I can't figure out how to call a SP.
What steps should I use.
Any samples of this ? All samples use a table.
Thank's
This post should hopefully be of help:
http://blogs.msdn.com/brada/archive/2009/08/24/business-apps-example-for-silverlight-3-rtm-and-net-ria-services-july-update-part-24-stored-procedures.aspx
You can create empty view with the same structure of your sproc and map that stored procedure to your function in your DomainService
See sample on http://cid-289eaf995528b9fd.skydrive.live.com/self.aspx/Public/sproc.zip
I found the following excellent step-by-step guide at this site -
http://betaforums.silverlight.net/forums/p/218383/521023.aspx
1) Add a ADO Entity Data Model to your Web project; Select generate from database option; Select your Database instance to connect to.
2) Choose your DB object to import to the Model. You can expand Table node to select any table you want to import to the Model. Expand Stored Procedure node to select your Stored Precedure as well. Click Finish to finish the import.
3) Right click the DB model designer to select Add/Function Import. Give the function a name (same name as your SP would be fine) and select the Stored Procedure you want to map. If your SP returns only one field, you can map the return result to a collection of scalars. If your SP returns more than one field, you could either map the return result to a collection or Entity (if all the field are from a single table) or a collection of Complex types.
If you want to use Complex type, you can click Get Column button to get all the columns for your SP. Then click Create new Complex type button to create this Complex type.
4) Add a Domain Service class to the Web project. Select the DataModel you just created as the DataContext of this Service. Select all the entitis you want expose to the client. The service functions should be generated for those entities.
5) You may not see the Complex type in the Entity list. You have to manully add a query function for your SP in your Service:
Say your SP is called SP1, the Complex type you generated is called SP1_Result.
Add the following code in your Domain Service class:
public IQueryable<SP1_Result> SP1()
{
return this.ObjectContext.SP1().AsQueryable();
}
Now you can compile your project. You might get an error like this: "SP1_Result does not have a Key" (if you not on RIA service SP1 beta). If you do, you need to do the following in the service metadata file:
Added a SP1_Result metadata class and tagged the Key field:
[MetadataTypeAttribute(typeof(SP1_Result.SP1_ResultMetadata))]
public partial class SP1_Result
{
internal sealed class SP1_ResultMetadata
{
[Key]
public int MyId; // Change MyId to the ID field of your SP_Result
}
}
6) Compile your solution. Now you have SP1_Result exposed to the client. Check the generated file, you should see SP1_Result is generated as an Entity class. Now you can access DomainContext.SP1Query and DomainContext.SP1_Results in your Silverlight code. You can treat it as you do with any other Entity(the entity mapped to a table) class.
Calling a stored procedure is trivial. Import it as a function and then invoke the function as a member of the DDS. The return value is an ObservableCollection<> that you can use to set up the DataContext of the object you want to bind.
...unless you want to use it in a Silverlight RIA app via the magic code generated proxy, in which case your goose is cooked unless your result rows exactly match one of the entities. If you can meet that criterion, edit the DomainService class and surface a method that wraps the ObjectContext method.