-
Notifications
You must be signed in to change notification settings - Fork 14
CustomDataExchanger and Bind (Attribute)
CustomDataExchanger and Bind attributes are a powerful way to fine tune what you want to exchange across the network with SignalGo. With this attributes you can limit sending and receiving any object (class, property of class, method parameters etc.) You can use data exchanger attributes for example when:
-
You don't want to send/receive one or more properties or types to/from client.
-
You need speed tranfer optimization. In fact you limit to exchange only data really needed. All unnecessary objects are simply not exchanged: less band used.
-
If you need security tuning. For example, if you don't want send Password property, or send it in any method you don't need to set it NULL manually, just ignore it with attributes (or also using supplied class for already instantiated objects).
You can use CustomDataExchangerAttribute over your service methods, models and properties of models and customize your include or exclude properties.
Use CustomDataExchangerAttribute over Property:
public class UserInfo
{
public int Id { get; set; }
public string Username { get; set; }
[CustomDataExchanger(ExchangeType = CustomDataExchangerType.Ignore, LimitationMode = LimitExchangeType.OutgoingCall)]
public string Password { get; set; }
public int Age { get; set; }
}
Use CustomDataExchangerAttribute over class or model:
[CustomDataExchanger("Title", "Text", ExchangeType = CustomDataExchangerType.TakeOnly, LimitationMode = LimitExchangeType.OutgoingCall)]
public class PostInfoTest
{
public int Id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string Abstract { get; set; }
}
Use CustomDataExchangerAttribute over server method:
[SignalGo.Shared.DataTypes.ServiceContract("TestServerModel", ServiceType.ServerService, InstanceType = SignalGo.Shared.DataTypes.InstanceType.SingleInstance)]
public interface ITestServerModel
{
[CustomDataExchanger(typeof(UserInfo), "Id", "Password", ExchangeType = CustomDataExchangerType.TakeOnly, LimitationMode = LimitExchangeType.Both)]
List<UserInfoTest> GetListOfUsers();
}
- ExchangeType:
Type of Exchanging that you want to set to allow sending or ignoring properties or classes. If you want ignore some of properties or ignore some types just set ExchangeType = CustomDataExchangerType.Ignore. If you want take some of properties or some types just set ExchangeType = CustomDataExchangerType.TakeOnly.
- LimitationMode:
The plan who sets limitations of the exchanger for incoming/outgoing data data. When a client calls a server method this is incoming call from the client to the server. When the server wants to return a result to the client after the client itself called a method, this is an outgoing call from the server to the client. If you want to set a limit on incoming calls just set LimitationMode = LimitExchangeType.IncomingCall If you want to set a limit on outgoing calls just set LimitationMode = LimitExchangeType.OutgoingCall If you want to set a limit on both incoming and outgoing calls just set LimitationMode = LimitExchangeType.Both
if you want use the DataExchanger power at runtime on instances of objects or types or in if conditions etc. just use DataExchanger class.
public List<PostInfoTest> GetCustomPostsOfUser(int userId)
{
List<PostInfoTest> results = new List<PostInfoTest>();
results.Add(new PostInfoTest() { Id = 1, PostSecurityLink = "securityLink", Text = "hello guys...", Title = "work finished" });
results.Add(new PostInfoTest() { Id = 2, PostSecurityLink = "securityLink2", Text = "today were good but...", Title = "good day" });
results.Add(new PostInfoTest() { Id = 3, PostSecurityLink = "securityLink3", Text = "today were bad but...", Title = "bad day" });
results.Add(new PostInfoTest() { Id = 4, PostSecurityLink = "securityLink4", Text = "today were bad but...", Title = "bad day" });
results.Add(new PostInfoTest() { Id = 5, PostSecurityLink = "securityLink5", Text = "today were bad but...", Title = "bad day" });
results.Add(new PostInfoTest() { Id = 6, PostSecurityLink = "securityLink6", Text = "today were bad but...", Title = "bad day" });
results.Add(new PostInfoTest() { Id = 7, PostSecurityLink = "securityLink7", Text = "today were bad but...", Title = "bad day" });
//ignore object of results[6]
DataExchanger.Ignore(results[6]);
//ignore "PostSecurityLink" property of results[5]
DataExchanger.Ignore(results[5], "PostSecurityLink");
//take only "Id" property of results[4]
DataExchanger.TakeOnly(results[4], "Id");
return results;
}
[SignalGo.Shared.DataTypes.ServiceContract("TestServerModel", ServiceType.ServerService, InstanceType = SignalGo.Shared.DataTypes.InstanceType.SingleInstance)]
public interface ITestServerModel
{
bool HelloBind([Bind(Include = "Id")]UserInfoTest userInfoTest, [Bind(Exclude = "Username")]UserInfoTest userInfoTest2,
[Bind(Includes = new string[] { "Id", "Username" })]UserInfoTest userInfoTest3);
}