Skip to content

CustomDataExchanger and Bind (Attribute)

Ali Yousefi edited this page Mar 30, 2018 · 6 revisions

With this attributes you can limit send and receive any class and property of class or methods and method parameters. Why we must using the data exchanger attributes?

  1. We don't want to send some properties or types to client or receive from clients.

  2. This limitation will help us to improvement our server or client speed of send and receive data,because server or client don't send any data that we don't need them.

  3. Security reason,for exmaple you don't want send Password property to client insinde of any methods or you don't want to receive permission property from client because you are save all data to database with 1 line of code and you are using model of database that you don't want to change model and don't want to set null of properties manually in methods.

How to use?

You can use CustomDataExchangerAttribute over your service methods, models and properties of models and customize your include or exclude properties.

Examples

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();
    }

How it works?

  • ExchangeType:

Type of data Exchanging that you want send or ignore 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:

Mode of limitation plan that you want limit exchanger for incomming data or outgoing data,when client is calling server method this is incomming call from client to server and if server want return a result to client after client called this is outgoing call from server to client. If you want doing limitation for incomming call just set LimitationMode = LimitExchangeType.IncomingCall If you want doing limitation for outgoing call just set LimitationMode = LimitExchangeType.OutgoingCall If you want doing limitation for both of incomming call and outgoing call just set LimitationMode = LimitExchangeType.Both

Runtime usage:

if you want do data exchanger on runtime for an instance of object or types in if condition or etc just use DataExchanger class.

Examples:

        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;
        }

Clone this wiki locally