How to Save a Generic Measurement<Unit> in Core Data

Transformable and NSKeyedUnarchiveFromData

This warning appears because apple will remove it in future release because it's not secure by default. Here is thread on Apple forum.
You should use NSSecureUnarchiveFromData instead of that.

Storing and Managing unit of measurements

I think you should have a better abstraction than a String; it's little more than a primitive.

I'd write a Units class that would parse and store those Strings from the database and allow you to compare them easily.

public class Units {
private final String name;

// constructors, equals, hashCode, toString and other operations here
}

It might also be useful to have categories of Units. (You can measure pressure a lot of ways.) British and international units can describe the same thing.

You might also have categories based on physical quantities (e.g. force, mass, charge, time, etc.)

How to model attribute units in a database design?

Interesting question...

There are two obvious routes:

id   load_kW     fuel_consumption_tonnes
--------------------------------------------------
1 1154 89.4
2 1199 54.2

This is easy for humans to read, and fairly logical. However, if some readings are in "kilos", others in "tonnes", you have to convert those readings to fit into the "readings" table; this process MUST be "lossless", and idempotent. For instance, a reading of "89403 kilos" is not "89.4 tonnes", even though the business may choose to round from kilos to tonnes for convenience. There are usually some counter-intuitive rounding things that happen...

If that's the case, you could change the schema:

id      load load_unit    fuel_consumption fuel_consumption_unit
--------------------------------------------------
1 1154 kW 89403 kg
2 1199 kW 54.2 t

With a "unit" table, if you need it:

unit_id    unit_name
--------------------
kg kilogramme
t Tonne

However, this model is open to human failure - it would be easy to change the "load_unit" column without modifying the "load" column, thus breaking the data. There's nothing you can really do to your data model to avoid this. It also makes common queries fairly tricky: imagine trying to retrieve the total of "load" in a consistent unit of measurement.

I would recommend that in this case, you have two tables: "raw_readings", with the original data in the format above, and "normalized_readings", which you populate by converting all the readings to a consistent unit of measurement.

Unit of Work Pattern And Updating Entity Collection Properties

Thanks to SOfanatic's comment, the problem has solved now.

I've updated my GenericRepository's BuildQuery method to reflect SOfanatic's suggestion and it worked.

Here is the updated BuildQuery method:

internal virtual IQueryable<TEntity> BuildQuery(Expression<Func<TEntity,bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
{
IQueryable<TEntity> query = this.context.IsReadOnly ? dbSet.AsNoTracking() : dbSet;
foreach (var include in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(include);
}

if (filter != null)
query = query.Where(filter);

if (orderBy != null)
return orderBy(query);

return query;
}

DbContext.IsReadOnly is a custom property I added to my DbContext implementation. That way if I want to load entities in a kind of "read-only" mode (only selects) I disable lazy loading, proxy generation and change tracking so it increases EF performance a bit.



Related Topics



Leave a reply



Submit