Entity Framework 5 Code first custom key logic? - entity-framework-5

I have an entity (several, actually) that I want to have assigned a key value based on custom logic when they're created. I know I could do it in the entity constructor, but ideally I'd like to do it as part of the data context logic for when a new entity is created.
I found how to turn off autogeneration, but what I'd rather do is replace auto generation w/ my own logic, ideally handled by the C# code (I"ve seen techniques for doing it via stored procs also, that I'd rather not use).
Is this even possible to do centrally?

Autogeneration happens on the server side and is not done by the EF. This is a setting on a table key column. So EF does not generate any keys - if autogeneration is turned on this is the database that generates keys, if autogeneration is off this is the user that is responsible for generating keys. If you don't want to generate keys when saving changes you may want to override SaveChanges and generate keys for all newly added entities.

Related

JOOQ Code generation could take into account composite unique constraints

Jooq - 3.13
We are using Jooq Codegen to generate using the org.jooq.meta.extensions.ddl.DDLDatabase generation strategy.
When I have a table ex: Employee with a unique key constraint on say employee_number (this column is not a primary key) it generates a very useful method fetchOneByEmployeeNumber however I cannot say the same about composite keys.
If there is a composite unique key on (department_id, employee_number) then I'd like a method findOneByDepartmentIdEmployeeNumber(Long departmentId, Long employeeNumber). Is this possible? (Obviously I can roll my own method for this purpose but we have quite a few of these constraints and auto generated methods will be very helpful)
This isn't being done right now for DAOs in jOOQ's code generator. I've created a feature request for this: https://github.com/jOOQ/jOOQ/issues/10597
As a workaround, you can extend jOOQ's JavaGenerator::generateDaoClassFooter method to generate your own, see: https://www.jooq.org/doc/latest/manual/code-generation/codegen-custom-code/

Hazelcast Spring (boot) JPA - saving a new entity

I have an existing client/server, Spring boot project that uses JPA. I followed the spring-data-jpa-hazelcast-migration sample to create a Hazelcast client & server. There is existing data in a db that populates the Hazelcast Map. Each of these db entries has an Integer id which becomes the Map key. During this loadAllKeys() on the server, I populate a Set which is distributed/managed by Hazelcast; in the MemCenter UI, I see the populated Map & the Set containing the expected number of Integer keys.
Per the example mentioned above, the client application has a service which uses a HazelcastRepository to save() new entities. Since a new entity does not have an id, on the client side (where a new entity is created), I have an IdGenerator that returns nextKey(). The (distributed) Set is asked if it contains said id/key to prevent key collisions.
Then, once this save() on the client side makes its way to the server, I attempt to add the new key to the Set to avoid any collisions in the future. In doing so, I get the following exception:
Thread[hz._hzInstance_1_dev.partition-operation.thread-3,5,_hzInstance_1_dev] cannot make remote call: com.hazelcast.collection.impl.collection.operations.CollectionAddOperation{serviceName='hz:impl:setService', identityHash=1523576050, partitionId=64, replicaIndex=0, callId=0, invocationTime=-1 (1969-12-31 15:59:59.999), waitTimeout=-1, callTimeout=60000, name=com.intelligrated.hazelcast.server.maploader.ScannersMapStore_Set}
For some reason, I have not found a single example of how to handle the creation of new data especially when data already exists. The examples/test are trivial and when a new object is saved, the id is hard-coded (usually to '1').
I devised the above solution hoping that I can make this work. If there is a better way, kindly point me to an useful example.
So, I found this post helpful.
Basically, you can't have "operations" in MapStore b/c of the write through.:
"Because writethrough map store operations run on partition thread, and using another partition based operation(like Containskey) can cause deadlock. That is why we have a check and an exception there"
So, removed all the Set business from my MapStore & added necessary logic to a separate "Listener" class. The map store sends "set update events" on a Topic to which said Listener is subscribed to.

Xcode 4.3 Core Data how to make a list?

I am new to iOS development (and objective-C & Xcode 4.3) and I want to create an app with profiles for users. I understand how to use Core Data to make a table with entities and that's all fine, but I don't know how I would go about creating a model so that a user can save a list of items (ie a MutableArray).
For example I need to have a username (for the profile) and than a list of strings that are saved to his account.
Would I need to just create an table with username and string item and than just query for all tuples that contain the username. Is there a simple way to serialize an object and save it the same way it's done in java.
Thank You.
You need to create a one-to-many relationship (which makes a NSSet of "destination" objects, or if you want to keep them in the specified order, click Ordered and it will be a NSOrderedSet).
Once you have the relationship, you can add multiple items to it and access it via the set.

Help applying DDD to dynamic form application

I am designing an application that will display dynamically-generated forms to the user who will then enter values into the form fields and submit those values for persistence. The form represents an employee evaluation.
One use case allows an administrator (from HR) to define the form fields. They should be able to create a new form, add/remove fields from a form and mark a form as 'deleted'.
The second use case is when a manager views the form and enters values into the form fields for a specific employee. They should be able to save the values at any time and recall the saved values when viewing the form again for the same employee.
Finally, when the manager is satisfied with the values they've entered for that employee, they can 'submit' the form data which persists the flattened data into the data warehouse for reporting purposes. When this is done, the 'working' copy of the data is removed so the form will display empty the next time they view it for that employee.
I am not concerned with the front-end at this point and working on the back-end service application that sits between the client and the data store. The application must provide a course-grained interface for all of the behavior required.
My question is how many aggregate roots do I actually have (and from that, how many repositories, etc)? Do I separate the form definition from the form data even though I need both when displaying the form to the user?
I see two main entities, 'EmployeeEvaluationSchema' and 'EmployeeEvaluation'. The 'EmployeeEvaluationSchema' entity would have a collection of 'FieldDefinition' value objects which would contain the properties that define a field, the most basic being the name of the field. The 'EmployeeEvaluation' entity would have a collection of 'FieldValue' value objects which contain the values for each field from the definition. In the simplest case, it would have a field name and value property. Next, the 'EmployeeEvaluation' could have a reference to 'EmployeeEvaluationSchema' to specify which definition the particular evaluation is based on. This can also be used to enforce the form definition in each evaluation. You would have two repositories - one for each entity. If you were to use an ORM such as NHibernate, then when you retrieve a 'EmployeeEvaluation' entity, the associated 'EmployeeEvaluationSchema' would also be retrieved even though there is a dedicated repository for it.
From your description it sounds like your objects don't have any behavior and are simple DTOs. If that is the case maybe you should not bother doing DDD. Can you imagine your entities without having getters? There are better ways to do CRUDish application than DDD. Again this is only valid if your "domain" does not have relevant behavior.

non-database field on ClearQuest form

Is there a way to use form fields that does not correspond to database field for temporary processings?
I.e. I want to add:
temp fields item1, item2
database field sum
button with record hook that sets sum = item1 + item2
As far as I know it's simply not possible with ClearQuest.
I've tried to do something similar and was told by our IBM consultant that the only way is to create a DB field for all variables.
You can't attach data to form fields really - those are representations of the underlying data, not something scripts interact with directly.
Adding temporary data to the underlying record (entity) itself sounds unlikely as well. Perhaps it's possible to abuse the perl API and dynamically attach data to entity objects but I personally wouldn't try it, you're liable to lose your data at the whim of CQ then ;-)
That does not however mean it's impossible to have temporary data.
The best way seems to me to be using the session object, which is explicitly intended for that purpose.
From the helpfile:
IBM Rational ClearQuest supports the
use of sessionwide variables for
storing information. After you create
sessionwide variables, you can access
them through the current Session
object using functions or subroutines,
including hooks, that have access to
the Session object. When the current
session ends, all of the variables
associated with that Session object
are deleted. The session ends when the
user logs out or the final reference
to the Session object ceases to exist.
There's some helpful documentation on this subject in file:///C:/Program%20Files/Rational/ClearQuest/doc/help/cq_api/c_session_vars.htm (Presuming a default installation on a windows machine, of course.)
Translating the code example in there into what you seem to be wanting, first you store the data you have calculated inside the session object:
$session->SetNameValue("item1", $value1);
$session->SetNameValue("item2", $value2);
Then in your calculation hook you retrieve the stored values and set the value of that totals field like this:
my $item1 = GetNameValue("item1");
my $item2 = GetNameValue("item2");
my $sum = $item1 + $item2;
$entity->SetFieldValue("some_totals_record", $sum);
Adjust to taste of course ;-)
ClearQuest schema designers often include 'temporary' fields in their record types. They do this so they perform operations on hooks to generate another value.
For example, for the Notes fields, there is a 'temporary' Notes_entry field that the user types the most recent note into, and when the record is saved, the value is added to the Notes_Log field. The next time the record is edited the Notes_entry field is cleared so the user can type a new Notes_entry.

Resources