Skip to content

Commit 40d32af

Browse files
authored
Merge pull request #301 from phofman/Feature/baseclass_maker
Helper for base-class definition
2 parents b6af0d3 + 9342780 commit 40d32af

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

EntityFramework.Reverse.POCO.Generator/Database.tt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,16 @@
324324
{
325325
//if (t.ClassName == "User")
326326
// return ": IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>";
327+
328+
// or use the maker class to dynamically build more complex definitions
329+
//var r = new BaseClassMaker("POCO.Sample.Data.MetaModelObject");
330+
//r.AddInterface("POCO.Sample.Data.IObjectWithTableName");
331+
//r.AddInterface("POCO.Sample.Data.IObjectWithId",
332+
// t.Columns.Any(x => x.IsPrimaryKey && !x.IsNullable && x.NameHumanCase.Equals("Id", StringComparison.InvariantCultureIgnoreCase) && x.PropertyType == "long"));
333+
//r.AddInterface("POCO.Sample.Data.IObjectWithUserId",
334+
// t.Columns.Any(x => !x.IsPrimaryKey && !x.IsNullable && x.NameHumanCase.Equals("IdUsera", StringComparison.InvariantCultureIgnoreCase) && x.PropertyType == "long"));
335+
//return r.ToString();
336+
327337
return "";
328338
};
329339

EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.Core.ttinclude

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3694,4 +3694,76 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
36943694
var spReturnClassName = WriteStoredProcReturnModelName(sp);
36953695
return (returnModelCount == 1) ? string.Format("System.Collections.Generic.List<{0}>", spReturnClassName) : spReturnClassName;
36963696
};
3697+
3698+
/// <summary>
3699+
/// Helper class in making dynamic class definitions easier.
3700+
/// </summary>
3701+
public sealed class BaseClassMaker
3702+
{
3703+
private string _typeName;
3704+
private System.Text.StringBuilder _interfaces;
3705+
3706+
public BaseClassMaker(string baseClassName = null)
3707+
{
3708+
SetBaseClassName(baseClassName);
3709+
}
3710+
3711+
/// <summary>
3712+
/// Sets the base-class name.
3713+
/// </summary>
3714+
public void SetBaseClassName(string typeName)
3715+
{
3716+
_typeName = typeName;
3717+
}
3718+
3719+
/// <summary>
3720+
/// Appends additional implemented interface.
3721+
/// </summary>
3722+
public bool AddInterface(string typeName)
3723+
{
3724+
if (string.IsNullOrEmpty(typeName))
3725+
return false;
3726+
3727+
if (_interfaces == null)
3728+
{
3729+
_interfaces = new System.Text.StringBuilder();
3730+
}
3731+
else
3732+
{
3733+
if (_interfaces.Length > 0)
3734+
{
3735+
_interfaces.Append(", ");
3736+
}
3737+
}
3738+
3739+
_interfaces.Append(typeName);
3740+
return true;
3741+
}
3742+
3743+
/// <summary>
3744+
/// Conditionally appends additional implemented interface.
3745+
/// </summary>
3746+
public bool AddInterface(string interfaceName, bool condition)
3747+
{
3748+
if (condition)
3749+
{
3750+
return AddInterface(interfaceName);
3751+
}
3752+
3753+
return false;
3754+
}
3755+
3756+
public override string ToString()
3757+
{
3758+
var hasInterfaces = _interfaces != null && _interfaces.Length > 0;
3759+
3760+
if (string.IsNullOrEmpty(_typeName))
3761+
{
3762+
return hasInterfaces ? " : " + _interfaces : string.Empty;
3763+
}
3764+
3765+
return hasInterfaces ? string.Concat(" : ", _typeName, ", ", _interfaces) : " : " + _typeName;
3766+
}
3767+
}
3768+
36973769
#>

0 commit comments

Comments
 (0)