Skip to content

Commit fffaad5

Browse files
authored
update readme (#893)
1 parent 8f4c6d9 commit fffaad5

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

uSync.Extend/readme.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# uSync.Extend.
2+
Helper classes to make it easy(er) to extend uSync.
3+
4+
uSync.Extend contains some helper classes and implimentations of ISyncHandlers and ISyncSerializers
5+
that mean you can extend the functionality of uSync to include your own custom objects and data without
6+
having to write the whole thing from scratch.
7+
8+
The extend package contains two base classes that simplfiy Handler and serializer creation for uSync.
9+
10+
### SyncObjectHandler
11+
In uSync a handler is the thing that controlls the Input/Output of data to and from the file system.
12+
its the thing that is called when an item is saved or when uSync exports or imports anything.
13+
14+
The SyncObjectHandler is a base class that you can inherit from to create your own handlers.
15+
It provides some basic functionality and structure to make it easier to create your own handlers.
16+
17+
Your own handler then only needs to provide the basic info on how to get and save your items.
18+
19+
```cs
20+
/// <summary>
21+
/// the attribute set it all up. this is how it's discovered and registered in uSync
22+
/// </summary>
23+
[SyncHandler("MyCustomObjectHandler",
24+
"My Custom Object Handler",
25+
"MyCustomObjects",
26+
uSyncConstants.Priorites.USYNC_RESERVED_UPPER + 100, // after all the core things
27+
Icon = "icon-files",
28+
EntityType = "myCustomObject")]
29+
public class MyCustomObjectHandler : SyncObjectHandler<MyCustomObject>
30+
{
31+
public override string Group => "My Custom Group";
32+
33+
private readonly IMyCustomObjectService _myCustomObjectService;
34+
35+
public MyCustomObjectHandler(
36+
ILogger<SyncHandlerRoot<MyCustomObject, MyCustomObject>> logger,
37+
AppCaches appCaches,
38+
IShortStringHelper shortStringHelper,
39+
ISyncFileService syncFileService,
40+
ISyncEventService mutexService,
41+
ISyncConfigService uSyncConfig,
42+
Core.ISyncItemFactory itemFactory,
43+
IMyCustomObjectService myCustomObjectService) : base(logger, appCaches, shortStringHelper, syncFileService, mutexService, uSyncConfig, itemFactory)
44+
{
45+
_myCustomObjectService = myCustomObjectService;
46+
}
47+
48+
// here you would get all your items from your data source.
49+
protected override async Task<IEnumerable<MyCustomObject>> GetAllItems()
50+
=> await _myCustomObjectService.GetAllAsync();
51+
52+
protected override string GetItemName(MyCustomObject item)
53+
=> item.Name;
54+
}
55+
```
56+
57+
### SyncObjectSerializer
58+
The SyncObjectSerializer is a base class that you can inherit from to create your own serializers.
59+
60+
```cs
61+
[SyncSerializer("c61e5987-020b-4ba8-899f-957d63443ac1", // uniqe id need to be a GUID
62+
"My Custom Object Serializer", // name
63+
"MyCustomObject")] // the object type (node name in the xml)
64+
public class MyCustomObjectSerializer : SyncObjectSerializer<MyCustomObject>
65+
{
66+
private readonly IMyCustomObjectService _service;
67+
68+
public MyCustomObjectSerializer(
69+
ILogger<SyncSerializerRoot<MyCustomObject>> logger,
70+
IMyCustomObjectService service) : base(logger)
71+
{
72+
_service = service;
73+
}
74+
public override MyCustomObject CreateItem(XElement node)
75+
{
76+
return new MyCustomObject
77+
{
78+
Key = node.GetKey(),
79+
Alias = node.GetAlias(),
80+
};
81+
}
82+
83+
public override async Task DeleteItemAsync(MyCustomObject item)
84+
=> await _service.DeleteAsync(item);
85+
86+
public override async Task<MyCustomObject?> FindItemAsync(Guid key)
87+
=> await _service.FindByKeyAsync(key);
88+
89+
public override async Task<MyCustomObject?> FindItemAsync(string alias)
90+
=> await _service.FindByAliasAsync(alias);
91+
92+
public override string ItemAlias(MyCustomObject item) => item.Alias;
93+
94+
public override Guid ItemKey(MyCustomObject item) => item.Key;
95+
96+
public override Task SaveItemAsync(MyCustomObject item)
97+
=> _service.SaveAsync(item);
98+
}
99+
```

0 commit comments

Comments
 (0)