Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
Dapper
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
Dapper
Commits
aa511707
Commit
aa511707
authored
Jun 28, 2011
by
Sam Saffron
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dapper contrib refactor ... pull it all into one file for nuget goodness
parent
fcd385fd
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
174 additions
and
205 deletions
+174
-205
Dapper.Contrib.csproj
Dapper.Contrib/Dapper.Contrib.csproj
+0
-2
ProxyGenerator.cs
Dapper.Contrib/Extensions/ProxyGenerator.cs
+0
-174
SqlMapperExtensions.cs
Dapper.Contrib/Extensions/SqlMapperExtensions.cs
+174
-9
TypeExtension.cs
Dapper.Contrib/Extensions/TypeExtension.cs
+0
-20
No files found.
Dapper.Contrib/Dapper.Contrib.csproj
View file @
aa511707
...
...
@@ -49,10 +49,8 @@
<Reference
Include=
"System.Xml"
/>
</ItemGroup>
<ItemGroup>
<Compile
Include=
"Extensions\ProxyGenerator.cs"
/>
<Compile
Include=
"Extensions\SqlMapperExtensions.cs"
/>
<Compile
Include=
"Properties\AssemblyInfo.cs"
/>
<Compile
Include=
"Extensions\TypeExtension.cs"
/>
</ItemGroup>
<ItemGroup>
<ProjectReference
Include=
"..\Dapper\Dapper.csproj"
>
...
...
Dapper.Contrib/Extensions/ProxyGenerator.cs
deleted
100644 → 0
View file @
fcd385fd
using
System
;
using
System.Collections.Generic
;
using
System.ComponentModel.DataAnnotations
;
using
System.Linq
;
using
System.Reflection
;
using
System.Reflection.Emit
;
using
System.Text
;
using
System.Threading
;
namespace
Dapper.Contrib.Extensions
{
//TODO: Try hiding this interface
public
interface
IProxy
{
bool
IsDirty
{
get
;
set
;
}
}
class
ProxyGenerator
{
private
static
readonly
Dictionary
<
Type
,
object
>
TypeCache
=
new
Dictionary
<
Type
,
object
>();
private
static
AssemblyBuilder
GetAsmBuilder
(
string
name
)
{
var
assemblyBuilder
=
Thread
.
GetDomain
().
DefineDynamicAssembly
(
new
AssemblyName
{
Name
=
name
},
AssemblyBuilderAccess
.
Run
);
//NOTE: to save, use RunAndSave
return
assemblyBuilder
;
}
public
static
T
GetClassProxy
<
T
>()
{
// A class proxy could be implemented if all properties are virtual
// otherwise there is a pretty dangerous case where internal actions will not update dirty tracking
throw
new
NotImplementedException
();
}
public
static
T
GetInterfaceProxy
<
T
>()
{
Type
typeOfT
=
typeof
(
T
);
if
(
TypeCache
.
ContainsKey
(
typeOfT
))
{
return
(
T
)
TypeCache
[
typeOfT
];
}
var
assemblyBuilder
=
GetAsmBuilder
(
typeOfT
.
Name
);
var
moduleBuilder
=
assemblyBuilder
.
DefineDynamicModule
(
"SqlMapperExtensions."
+
typeOfT
.
Name
);
//NOTE: to save, add "asdasd.dll" parameter
var
interfaceType
=
typeof
(
IProxy
);
var
typeBuilder
=
moduleBuilder
.
DefineType
(
typeOfT
.
Name
+
"_"
+
Guid
.
NewGuid
(),
TypeAttributes
.
Public
|
TypeAttributes
.
Class
);
typeBuilder
.
AddInterfaceImplementation
(
typeOfT
);
typeBuilder
.
AddInterfaceImplementation
(
interfaceType
);
//create our _isDirty field, which implements IProxy
var
setIsDirtyMethod
=
CreateIsDirtyProperty
(
typeBuilder
);
// Generate a field for each property, which implements the T
foreach
(
var
property
in
typeof
(
T
).
GetProperties
())
{
var
isId
=
property
.
GetCustomAttributes
(
true
).
Any
(
a
=>
a
is
KeyAttribute
);
CreateProperty
<
T
>(
typeBuilder
,
property
.
Name
,
property
.
PropertyType
,
setIsDirtyMethod
,
isId
);
}
var
generatedType
=
typeBuilder
.
CreateType
();
//assemblyBuilder.Save(name + ".dll"); //NOTE: to save, uncomment
var
generatedObject
=
Activator
.
CreateInstance
(
generatedType
);
TypeCache
.
Add
(
typeOfT
,
generatedObject
);
return
(
T
)
generatedObject
;
}
private
static
MethodInfo
CreateIsDirtyProperty
(
TypeBuilder
typeBuilder
)
{
var
propType
=
typeof
(
bool
);
var
field
=
typeBuilder
.
DefineField
(
"_"
+
"IsDirty"
,
propType
,
FieldAttributes
.
Private
);
var
property
=
typeBuilder
.
DefineProperty
(
"IsDirty"
,
PropertyAttributes
.
None
,
propType
,
new
Type
[]
{
propType
});
const
MethodAttributes
getSetAttr
=
MethodAttributes
.
Public
|
MethodAttributes
.
NewSlot
|
MethodAttributes
.
SpecialName
|
MethodAttributes
.
Final
|
MethodAttributes
.
Virtual
|
MethodAttributes
.
HideBySig
;
// Define the "get" and "set" accessor methods
var
currGetPropMthdBldr
=
typeBuilder
.
DefineMethod
(
"get_"
+
"IsDirty"
,
getSetAttr
,
propType
,
Type
.
EmptyTypes
);
var
currGetIL
=
currGetPropMthdBldr
.
GetILGenerator
();
currGetIL
.
Emit
(
OpCodes
.
Ldarg_0
);
currGetIL
.
Emit
(
OpCodes
.
Ldfld
,
field
);
currGetIL
.
Emit
(
OpCodes
.
Ret
);
var
currSetPropMthdBldr
=
typeBuilder
.
DefineMethod
(
"set_"
+
"IsDirty"
,
getSetAttr
,
null
,
new
Type
[]
{
propType
});
var
currSetIL
=
currSetPropMthdBldr
.
GetILGenerator
();
currSetIL
.
Emit
(
OpCodes
.
Ldarg_0
);
currSetIL
.
Emit
(
OpCodes
.
Ldarg_1
);
currSetIL
.
Emit
(
OpCodes
.
Stfld
,
field
);
currSetIL
.
Emit
(
OpCodes
.
Ret
);
property
.
SetGetMethod
(
currGetPropMthdBldr
);
property
.
SetSetMethod
(
currSetPropMthdBldr
);
var
getMethod
=
typeof
(
IProxy
).
GetMethod
(
"get_"
+
"IsDirty"
);
var
setMethod
=
typeof
(
IProxy
).
GetMethod
(
"set_"
+
"IsDirty"
);
typeBuilder
.
DefineMethodOverride
(
currGetPropMthdBldr
,
getMethod
);
typeBuilder
.
DefineMethodOverride
(
currSetPropMthdBldr
,
setMethod
);
return
currSetPropMthdBldr
;
}
private
static
void
CreateProperty
<
T
>(
TypeBuilder
typeBuilder
,
string
propertyName
,
Type
propType
,
MethodInfo
setIsDirtyMethod
,
bool
isIdentity
)
{
//Define the field and the property
var
field
=
typeBuilder
.
DefineField
(
"_"
+
propertyName
,
propType
,
FieldAttributes
.
Private
);
var
property
=
typeBuilder
.
DefineProperty
(
propertyName
,
PropertyAttributes
.
None
,
propType
,
new
Type
[]
{
propType
});
const
MethodAttributes
getSetAttr
=
MethodAttributes
.
Public
|
MethodAttributes
.
Virtual
|
MethodAttributes
.
HideBySig
;
// Define the "get" and "set" accessor methods
var
currGetPropMthdBldr
=
typeBuilder
.
DefineMethod
(
"get_"
+
propertyName
,
getSetAttr
,
propType
,
Type
.
EmptyTypes
);
var
currGetIL
=
currGetPropMthdBldr
.
GetILGenerator
();
currGetIL
.
Emit
(
OpCodes
.
Ldarg_0
);
currGetIL
.
Emit
(
OpCodes
.
Ldfld
,
field
);
currGetIL
.
Emit
(
OpCodes
.
Ret
);
var
currSetPropMthdBldr
=
typeBuilder
.
DefineMethod
(
"set_"
+
propertyName
,
getSetAttr
,
null
,
new
Type
[]
{
propType
});
//store value in private field and set the isdirty flag
var
currSetIL
=
currSetPropMthdBldr
.
GetILGenerator
();
currSetIL
.
Emit
(
OpCodes
.
Ldarg_0
);
currSetIL
.
Emit
(
OpCodes
.
Ldarg_1
);
currSetIL
.
Emit
(
OpCodes
.
Stfld
,
field
);
currSetIL
.
Emit
(
OpCodes
.
Ldarg_0
);
currSetIL
.
Emit
(
OpCodes
.
Ldc_I4_1
);
currSetIL
.
Emit
(
OpCodes
.
Call
,
setIsDirtyMethod
);
currSetIL
.
Emit
(
OpCodes
.
Ret
);
//TODO: Should copy all attributes defined by the interface?
if
(
isIdentity
)
{
var
keyAttribute
=
typeof
(
KeyAttribute
);
var
myConstructorInfo
=
keyAttribute
.
GetConstructor
(
new
Type
[]
{
});
var
attributeBuilder
=
new
CustomAttributeBuilder
(
myConstructorInfo
,
new
object
[]
{
});
property
.
SetCustomAttribute
(
attributeBuilder
);
}
property
.
SetGetMethod
(
currGetPropMthdBldr
);
property
.
SetSetMethod
(
currSetPropMthdBldr
);
var
getMethod
=
typeof
(
T
).
GetMethod
(
"get_"
+
propertyName
);
var
setMethod
=
typeof
(
T
).
GetMethod
(
"set_"
+
propertyName
);
typeBuilder
.
DefineMethodOverride
(
currGetPropMthdBldr
,
getMethod
);
typeBuilder
.
DefineMethodOverride
(
currSetPropMthdBldr
,
setMethod
);
}
}
}
Dapper.Contrib/Extensions/SqlMapperExtensions.cs
View file @
aa511707
This diff is collapsed.
Click to expand it.
Dapper.Contrib/Extensions/TypeExtension.cs
deleted
100644 → 0
View file @
fcd385fd
using
System
;
using
System.Linq
;
using
System.Runtime.CompilerServices
;
namespace
Dapper.Contrib.Extensions
{
public
static
class
TypeExtension
{
public
static
Boolean
IsAnonymousType
(
this
Type
type
)
{
if
(
type
==
null
)
return
false
;
var
hasCompilerGeneratedAttribute
=
type
.
GetCustomAttributes
(
typeof
(
CompilerGeneratedAttribute
),
false
).
Count
()
>
0
;
var
nameContainsAnonymousType
=
type
.
FullName
.
Contains
(
"AnonymousType"
);
var
isAnonymousType
=
hasCompilerGeneratedAttribute
&&
nameContainsAnonymousType
;
return
isAnonymousType
;
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment