Design Pattern - Structural - Repository

Scenario:

Create repository for data store.

Solution:

Structural Pattern is used to mediate between the domain and data mapping layers like an in memory object collection.

Example: Create repository for data store [DataRepositoryPattern/MockRepositoryPattern].
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    public class DataRepositoryPattern<T> : IRepositoryPattern<T>
        {
            public bool TryRunStoredProcedure<T>(out T result, string name, params Param[] param)
            {
                try
                {
                    result = RunStoredProcedure<T>(name, param).FirstOrDefault();
                    return true;
                }
                catch (System.Exception)
                {
                    result = default(T);
                    return false;
                }
            }
    
            private IEnumerable<T> RunStoredProcedure<T>(string name, Param[] param)
            {
                throw new NotImplementedException();
            }
        }
    
        public class MockRepositoryPattern<T> : IRepositoryPattern<T>
        {
            public bool TryRunStoredProcedure<T>(out T result, string name, params Param[] param)
            {
                try
                {
                    result = RunStoredProcedure<T>(name, param).FirstOrDefault();
                    return true;
                }
                catch (System.Exception)
                {
                    result = default(T);
                    return false;
                }
            }
    
            private IEnumerable<T> RunStoredProcedure<T>(string name, Param[] param)
            {
                throw new NotImplementedException();
            }
        }
    
        interface IRepositoryPattern<T>
        {
            bool TryRunStoredProcedure<T>(out T result, string name, params Param[] param);
        }
    
        public class Param
        {
        }
    
        public class RepositoryPattern
        {
            static void Main(string[] args)
            {
                DataRepositoryPattern<string> pattern = new DataRepositoryPattern<string>();
                if (pattern.TryRunStoredProcedure(out string result, "GetUsersById", new Param[1] { new Param() }))
                {
    
                }
    
                MockRepositoryPattern<string> mockPattern = new MockRepositoryPattern<string>();
                if (mockPattern.TryRunStoredProcedure(out string mockResult, "GetUsersById", new Param[1] { new Param() }))
                {
    
                }
            }
        }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...