SubSonic 3 generated ActiveRecord code with warnings

When using SubSonic 3 with ActiveRecord T4 templates, the generated code shows a lot of warnings about CLS compatibility, unused elements and the lack of GetHashCode () implementation.

To avoid them, I made the following changes:

// Structs.tt
[CLSCompliant(false)]                                    // added
public class <#=tbl.CleanName#>Table: DatabaseTable
{ ...

// ActiveRecord.tt
[CLSCompliant(false)]                                    // added
public partial class <#=tbl.ClassName#>: IActiveRecord
{
    #region Built-in testing
    #pragma warning disable 0169                         // added
    static IList<<#=tbl.ClassName#>> TestItems;
    #pragma warning restore 0169                         // added
    ...

    public override Int32 GetHashCode()                  // added
    {
      return this.KeyValue().GetHashCode();
    }

    ...

Is there a better way to get rid of warnings? Or is the best implementation of GetHashCode ()?

+3
source share
2 answers

Currently, the only way to get rid of warnings is to update your t4 templates and submit bugs / patches to Rob. Or wait until someone else does.

GetHashCode, , . - . -, . , , , - .

, , , , , , . :

public partial class Foo
{
    public override int GetHashCode()
    {
        int? result = null;
        TryGetHashCode(ref result);
        if (result.HasValue)
            return result.Value;
        return new Random().Next();
    }

    partial void TryGetHashCode(ref int? result);
}

public partial class Foo
{
    partial void TryGetHashCode(ref int? result)
    {
        result = 5;
    }
}

TryGetHashCode, TryGetHashCode, result, , , , - .

+2

. , , GetHashCode , , int.

, . 273 ActiveRecord.tt

<#      if(tbl.PK.SysType=="int"){#>

        public override int GetHashCode() {
            return this.<#=tbl.PK.CleanName #>;
        }
<#      }#>
<#      else{#>
        public override int GetHashCode() {
            throw new NotImplementedException();
        }
<#      }#>

GetHashCode , , ( ).

, - - , .

+1

Source: https://habr.com/ru/post/1711918/


All Articles