Skip to content

Replace existing double-dispatch method by using individual TypeMapping classes - #8631

Closed
lajones wants to merge 4 commits into
devfrom
170528_7434_SeparateTypeMappings_04
Closed

lajones wants to merge 4 commits into
devfrom
170528_7434_SeparateTypeMappings_04

Conversation

@lajones

@lajones lajones commented May 29, 2017

Copy link
Copy Markdown
Contributor

... for each C# type.

This is the replacement for PR #8612. Removing the double-dispatch method we had and using separate TypeMapping classes for each C# type. Sets us up to better address issue #7434 in the future.

@lajones

lajones commented May 29, 2017 •

Copy link
Copy Markdown
Contributor Author

Note: @smitpatel to make the HasDefaultUnicode/HasDefaultSize getters more robust I'm thinking of changing the constructors of all the TypeMappings to take DefaultUnicode and DefaultSize. Then I'll update HasDefaultUnicode/HasDefaultSize to just return whether IsUnicode matches DefaultUnicode etc. I haven't done it yet but that sounds better to me - what do you think?

/// </summary>
/// <param name="storeType"> The name of the database type. </param>
public ByteTypeMapping([NotNull] string storeType)
: this(storeType, System.Data.DbType.Byte, unicode: false, size: null)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajcvickers Re your comment about requiring DbType on all the constructors. The 6-arg constructor below appears on every TypeMapping and that can always be used to specify the DbType if needed. But I found that I needed to pass in the DbType only for any DateTime or string type mappings. For all the rest there was only 1 suitable choice of DbType. So for the "convenience" constructors like the above I decided to not have it. Let me know if you strongly disagree though - I can put it back the other way, but I was always passing the same thing so I thought this was better for the "convenience" constructors.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that there should be any place where we specify a DbType in our provider-agnostic code. I think that the DbType should be null by default for any type mapping. Then if SQL Server, or some other provider, needs to specify a DbType that should be done when it is constructed, in the code for that provider. In other words, I think we should maintain the previous approach which is that DbType is null unless some provider decides to explicitly set it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I think they're useful. But I'll put them back the other way.

//LAJLAJ What should we throw here?
return "ERROR: RelationalTypeMapping.GenerateSqlLiteral() received object of type " + value.GetType() +", with value " + value;
}

@lajones lajones May 29, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to throw here, but can I get opinions about the type of exception?

You should never reach here (we don't in any of our tests). If you do it's a sign that your provider has set up a TypeMapping badly.

While we could just always return "NULL" or we could just do ToString(CultureInfo.InvariantCulture) on whatever is passed in, that looks like it might lead to data corruption to me: any non-null value should have been handled by GenerateNonNullSqlLiteral() in whatever your current TypeMapping is. So I'd prefer to throw to alert the providers (and their users) to what's going wrong.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToString(CultureInfo.InvariantCulture) seems like a reasonable default here. It seems fine to say that this is what this method does if there is no override. In other words, provider code is not required to override this unless it needs to do something different for the literal. Maybe this could be combined with a string format as a constructor parameter at this level such that many types will not need to override this?

{
private readonly SqlServerMaxLengthMapping _nvarcharmax
= new SqlServerMaxLengthMapping("nvarchar(max)", typeof(string), dbType: null, unicode: true, size: null);
private readonly SqlServerStringTypeMapping _nvarcharmax

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to rename these to e.g. _unboundedString, _keyString, _unboundedAnsiString etc as in TestRelationalTypeMapper. Any objections?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to go ahead and do this since I haven't seen any objections.

=> _typeMapping ?? Dependencies.RelationalTypeMapper.GetMappingForValue(value);
{
if (_typeMapping != null
&& _typeMapping.ClrType.IsAssignableFrom(value.GetType()))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is value known to be non-null?

@lajones lajones May 30, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - we'd get an NRE. There is one case where it is not guaranteed so I'll fix to just return _typeMapper in that case. (It shouldn't matter which one we use as all TypeMappers know how to respond to a null value.)

private RelationalTypeMapping GetTypeMapping(object value)
=> _typeMapping ?? Dependencies.RelationalTypeMapper.GetMappingForValue(value);
{
if (_typeMapping != null

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider conditional operator and expression body.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

/// The generated string.
/// </returns>
protected override string GenerateNonNullSqlLiteral([NotNull]char value)
=> string.Format(CultureInfo.InvariantCulture, "'{0}'", value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that many of these subclasses do the same thing here--that is, use string.Format (or something that can be replaced with string.Format) to generate a value, but with a different format string. I wonder if we encapsulate this into a common class, then we will be able to remove a lot of these subclasses.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can do that. BTW do you know why the relational (i.e. non-provider specific) TypeMappings for the DateTime types return TIMESTAMP '...'? The addition of the TIMESTAMP literal seems odd.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lajones re TIMESTAMP '...' notation, it is a SQL standard thing. It can be used for literals in several common databases, e.g. MySQL and PostgreSQL, but doesn't work in SQL Server. The latter even assigns a completely different meaning to the TIMESTAMP type from the standard.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@divega Good to know. Thanks!

/// not used in application code.
/// </para>
/// </summary>
public abstract class RelationalTypeMapping<T> : RelationalTypeMapping

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note sure the generic is needed. I can see how it is nice in some places, but on the fence as to whether it really adds enough value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked it as it helped produce compile-time rather than runtime errors. I've now found all of the compile-time errors I need for below, but it might be useful in the future when we or provider-writers produce new Type Mappings for other types.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajcvickers I'm in the process of removing this. Let me know if my comment above changed your mind.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with leaving it or removing it. Up to you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I'll keep it then - since I found it useful. Thanks.

@ajcvickers

Copy link
Copy Markdown
Contributor

Played with the code a bit this morning. I think this general approach is fine, but I think the amount of code could be reduced quite a bit by:

  • Making the SQL literal string format a parameter on RelationalTypeMapping with a default of {0}
  • Removing subclasses for anything where this is the default--includes all the integer types
  • Creating subclasses for anything that needs a custom format and passing that down as an argument to the base class constructor. For example, the DateTime classes. This means that these classes don't have to override anything
    • Looks like we could have a class here for all quoted literals with a format of '{0}'. This could probably always do escaping as well, removing the need for a special string class here
  • Then for really special things like byte arrays, a subclass can not use the format string and just override GenerateNonNullSqlLiteral.

@lajones

lajones commented May 30, 2017

Copy link
Copy Markdown
Contributor Author

@ajcvickers OK. The approach you put forward above is clear (and answers a lot of my other q's above). Want to make sure that we envisage that as a good approach when, later on, we include generating C# literals and/or any special operations/functions/LINQ translators for each type mapping as well - as mentioned as one of the original goals of #7434. I'd rather not drop all those classes now only to find we need them again for that work...

@ajcvickers

Copy link
Copy Markdown
Contributor

@bricelam Do you think we should keep all the subtypes now so that they are there for C# literal generation later?

.Append(typeMapping.StoreType);

// reset typeMapping so that we can use it with GenerateSqlLiteral() below
typeMapping = Dependencies.TypeMapper.GetMapping(typeof(long));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is micro-optimization perhaps, but we should either move this outside of if or add better comment.
We are not resetting the type mapping but we are setting it as per operation.StartValue, which is long.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally had it outside, but then we regen the TypeMapping even if it was the correct one to begin with. I can change the comment to "Set the typeMapping for use with operation.StartValue below" if you like?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hence "micro-optimization" 😉
I am fine with just updating comment. So that at least it becomes clearer why we update it and why we use typeof(long).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - but I'd have had the complaint the other way around if I hadn't done it. I thought the comment was clear but I'll rephrase as mentioned above.

@lajones

lajones commented May 30, 2017

Copy link
Copy Markdown
Contributor Author

Re comment above about the HasDefaultUnicode/HasDefaultSize getters, @smitpatel and I discussed and we think those getters are just in the wrong place altogether. Instead we think it would be better for the TypeMapping not to know anything about its "defaults" but instead have the ScafdfoldingTypeMapper compare the TypeMapping it receives to the default one for that C# type and see whether it needs to scaffold IsUnicode(), HasColumnType()... that way. But we'd like to discuss that approach and deal with it in a different PR - so leaving as is for now.

{ typeof(DateTimeOffset), _datetimeoffset },
{ typeof(char), _int },
{ typeof(short), _smallint },
{ typeof(char), new CharTypeMapping("char(1)") },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this change from int? I don't think we should change it, but if we do, this should be nchar.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @bricelam This isn't a string mapping, it's a numeric mapping.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree on nchar vs char comment. I don't understand the rest though - why do we want to map to an integer when we have a SQL Server data type which maps directly to a single character?

@bricelam

Copy link
Copy Markdown
Contributor

Do you think we should keep all the subtypes now so that they are there for C# literal generation later?

Yes. I think we're mostly settled on the design for code literals, and it will require the subtypes. Doing this now prevents us from breaking providers that need to override built-in type mappings.

/// <param name="storeType"> The name of the database type. </param>
/// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param>
/// <returns> The newly created mapping. </returns>
public override RelationalTypeMapping CreateCopy([NotNull] string storeType, int? size)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overriding methods should not have [NotNull] attributes. These only go where the method is defined--in the interface or base implementation of a method. R# will show them grayed out here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Will fix.

@ajcvickers

Copy link
Copy Markdown
Contributor

Based on @bricelam's comment, let's leave the sub-classes.

@lajones

lajones commented May 31, 2017

Copy link
Copy Markdown
Contributor Author

@ajcvickers @bricelam I will leave the sub-classes.

@lajones

lajones commented May 31, 2017 •

Copy link
Copy Markdown
Contributor Author

@bricelam @ajcvickers @smitpatel Latest commit addresses all comments above except the outstanding one of to what underlying SQL Server type to map a char property.

int? size,
bool hasNonDefaultUnicode = false,
bool hasNonDefaultSize = false)
: base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize)

@ajcvickers ajcvickers May 31, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor seems unnecessary because bool mappings can never be Unicode or have size.

/// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param>
/// <param name="hasNonDefaultUnicode"> A value indicating whether the Unicode setting has been manually configured to a non-default value. </param>
/// <param name="hasNonDefaultSize"> A value indicating whether the size setting has been manually configured to a non-default value. </param>
public ByteArrayTypeMapping(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove Unicode facets. Maybe collapse into one constructor with default values?

/// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param>
/// <param name="hasNonDefaultUnicode"> A value indicating whether the Unicode setting has been manually configured to a non-default value. </param>
/// <param name="hasNonDefaultSize"> A value indicating whether the size setting has been manually configured to a non-default value. </param>
public ByteTypeMapping(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed for same reasons.

int? size,
bool hasNonDefaultUnicode = false,
bool hasNonDefaultSize = false)
: base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove size params.

int? size,
bool hasNonDefaultUnicode = false,
bool hasNonDefaultSize = false)
: base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed for same reasons.

int? size,
bool hasNonDefaultUnicode = false,
bool hasNonDefaultSize = false)
: base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed for same reasons.

int? size,
bool hasNonDefaultUnicode = false,
bool hasNonDefaultSize = false)
: base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed for same reasons.

int? size,
bool hasNonDefaultUnicode = false,
bool hasNonDefaultSize = false)
: base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed for same reasons.

int? size,
bool hasNonDefaultUnicode = false,
bool hasNonDefaultSize = false)
: base(storeType, dbType, unicode, size, hasNonDefaultUnicode, hasNonDefaultSize)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed for same reasons.

@smitpatel

Copy link
Copy Markdown
Contributor

@ajcvickers - Do we really need HasDefaultSize/Unicode thing?

/// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param>
/// <param name="hasNonDefaultUnicode"> A value indicating whether the Unicode setting has been manually configured to a non-default value. </param>
/// <param name="hasNonDefaultSize"> A value indicating whether the size setting has been manually configured to a non-default value. </param>
public SqlServerByteArrayTypeMapping(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally don't have doc comments like this on internal classes, but doesn't matter much.

private static readonly LongTypeMapping _integer = new LongTypeMapping(_integerTypeName, dbType: null);
private static readonly DoubleTypeMapping _real = new DoubleTypeMapping(_realTypeName, dbType: null);
private static readonly ByteArrayTypeMapping _blob = new ByteArrayTypeMapping(_blobTypeName, dbType: null);
private static readonly StringTypeMapping _text = new StringTypeMapping(_textTypeName, dbType: null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these now be moved inline? Or are the referenced elsewhere still?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are referenced in a couple of places.

public virtual void GenerateSqlLiteral_returns_bool_literal_when_true()
{
var literal = new RelationalTypeMapping("bool", typeof(bool)).GenerateSqlLiteral(true);
var literal = new BoolTypeMapping("bool", null).GenerateSqlLiteral(true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the null needed here? Does (or can) the parameter have a default?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@p6: (DbType = DateTime)
@p7: (DbType = DateTime)
@p8: (DbType = DateTime2)
@p9: (DbType = String)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know why this changed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because DbType.String is the default (for SQL Server) if you don't specify a parameter type. But for SQL Server we now specify that properties of type DateTimeOffset now correspond to parameters of type DbType.DateTimeOffset. Is this wrong? I know we're not specifying in Relational but this is SQL Server and I thought it was right to be specific.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

@ajcvickers

Copy link
Copy Markdown
Contributor

@smitpatel I'm fine with them going away if it's handled somewhere else.

@lajones

lajones commented Jun 1, 2017

Copy link
Copy Markdown
Contributor Author

@smitpatel We agreed not to try to change the HasNonDefaultUnicode/HasNonDefaultSize issue in this PR, but to have another one to address it?

@smitpatel

Copy link
Copy Markdown
Contributor

@lajones - After discussing with @ajcvickers about it. Certainly we don't need to block this PR for changes around HasNonDefaultUnicode/Size part. But we want to get away from it overall since its scaffolding only info. Instead scaffolding (ScaffoldingTypeMapper precisely), which already has information about the current store type and what would be the model generated data type for the property, can diff both of them to figure out whether to scaffold those facets or not.
It is fine to do in separate PR. Also, if removing those 2 properties is breaking change, then for preview2 may be we can insert default static properties in type mapping, to have API settled on final format and remove those properties once scaffolding type mapper catches up.

@lajones

lajones commented Jun 1, 2017

Copy link
Copy Markdown
Contributor Author

@smitpatel Yes. That's what we agreed yesterday. Do it separately after this is in.

…e sbyte,

ushort, uint and ulong mappings. Remove unnecessary constructors and
allow DbType to be defaulted in constructors.
@lajones

lajones commented Jun 1, 2017

Copy link
Copy Markdown
Contributor Author

@ajcvickers All review comments actioned. New commit has all constructors updated as asked for above. This commit includes putting the SQL Server TypeMapping for char back in. The issue to take it out again when we figure out how to cope with that in the query pipeline is #8656.

@lajones

lajones commented Jun 1, 2017

Copy link
Copy Markdown
Contributor Author

@ajcvickers Missed updates to remove unicode references in ByteArrayTypeMapping. Have fixed with 4th commit.

@ajcvickers ajcvickers left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit: Please get into the preview2 branch so we can gather feedback.

@lajones

lajones commented Jun 1, 2017

Copy link
Copy Markdown
Contributor Author

@ajcvickers Thanks. I'll set about rebasing this based on preview2 and will submit asap.

@lajones

lajones commented Jun 1, 2017

Copy link
Copy Markdown
Contributor Author

Checked into rel/2.0.0-preview2 with commit ef9ce8e, and into dev with commit ebd225a.

@lajones lajones closed this Jun 1, 2017
@lajones
lajones deleted the 170528_7434_SeparateTypeMappings_04 branch June 1, 2017 17:40
@lajones

lajones commented Jun 1, 2017

Copy link
Copy Markdown
Contributor Author

Also commits 945874c (in rel/2.0.0-preview2) and a6d1fa5 (in dev) to update a breaking-changes file.

@lajones lajones added this to the 2.0.0-preview2 milestone Jun 2, 2017
@ajcvickers ajcvickers removed this from the 2.0.0-preview2 milestone Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants