locked
How to create Generic repository and generic service RRS feed

  • Question

  • User616860969 posted

    How to create Generic repository and generic service in mvc app and web api project?

    Wednesday, January 17, 2018 1:42 PM

Answers

  • User-832373396 posted

    <g class="gr_ gr_14 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="14" data-gr-id="14">Hi</g> san,

    Sir, please refer to these guides:

    Official document, Generic Repository, in MVC 

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application#implement-a-generic-repository-and-a-unit-of-work-class 

    another one;

    Finally:

    <g class="gr_ gr_16 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="16" data-gr-id="16">USerModel</g>(Plan C# Class)

    2. Identity_User (Entity <g class="gr_ gr_17 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="17" data-gr-id="17">Framwork</g> Class)

    3. USers<g class="gr_ gr_15 gr-alert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" id="15" data-gr-id="15">,IUsers</g>(Class that implement <g class="gr_ gr_18 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="18" data-gr-id="18">Generice</g> Repository)

      #region Interfaces  
        public interface IUsersService : IService<UserModel, Identity_User, IUsers> 
        { 
        } 
      #endregion 
     
        public class UsersService : ServiceBase<UserModel, Identity_User, Users>, IUsersService 
        { 
            public UsersService() 
                : this(new Users()) 
            { 
     
            } 
            public UsersService(Users usr) 
                : base(usr) 
            { } 
        }
    public class HomeController : Controller 
        { 
            IUsersService serUSer; 
            public HomeController() 
                : this(new UsersService()) 
            { } 
            public HomeController(IUsersService usr) 
            { 
                serUSer = usr ?? new UsersService(); 
            } 
            public ActionResult Index() 
            { 
                UserModel result = new UserModel(); 
                try 
                { 
                    result.UserList = serUSer.GetAll(); 
                } 
                catch (Exception ex) 
                { 
     
                    throw ex; 
                } 
                return View(result); 
            } 
    }

    https://code.msdn.microsoft.com/generic-repository-pattern-ddea2262 

    Download link: https://github.com/MuhammadNasirKhanYousafzai/GenericServiceWithRepository  

    Then this one shows step by step:

    http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle 

    Official document, Generic Repository, in WebApi, Generic Repository Pattern With Web-API And Entity Framework

    http://www.c-sharpcorner.com/blogs/generic-repository-pattern-with-webapi-and-entity-framework

    With regards, Angelina Jolie

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 18, 2018 3:09 AM

All replies

  • User-832373396 posted

    <g class="gr_ gr_14 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="14" data-gr-id="14">Hi</g> san,

    Sir, please refer to these guides:

    Official document, Generic Repository, in MVC 

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application#implement-a-generic-repository-and-a-unit-of-work-class 

    another one;

    Finally:

    <g class="gr_ gr_16 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="16" data-gr-id="16">USerModel</g>(Plan C# Class)

    2. Identity_User (Entity <g class="gr_ gr_17 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="17" data-gr-id="17">Framwork</g> Class)

    3. USers<g class="gr_ gr_15 gr-alert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" id="15" data-gr-id="15">,IUsers</g>(Class that implement <g class="gr_ gr_18 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="18" data-gr-id="18">Generice</g> Repository)

      #region Interfaces  
        public interface IUsersService : IService<UserModel, Identity_User, IUsers> 
        { 
        } 
      #endregion 
     
        public class UsersService : ServiceBase<UserModel, Identity_User, Users>, IUsersService 
        { 
            public UsersService() 
                : this(new Users()) 
            { 
     
            } 
            public UsersService(Users usr) 
                : base(usr) 
            { } 
        }
    public class HomeController : Controller 
        { 
            IUsersService serUSer; 
            public HomeController() 
                : this(new UsersService()) 
            { } 
            public HomeController(IUsersService usr) 
            { 
                serUSer = usr ?? new UsersService(); 
            } 
            public ActionResult Index() 
            { 
                UserModel result = new UserModel(); 
                try 
                { 
                    result.UserList = serUSer.GetAll(); 
                } 
                catch (Exception ex) 
                { 
     
                    throw ex; 
                } 
                return View(result); 
            } 
    }

    https://code.msdn.microsoft.com/generic-repository-pattern-ddea2262 

    Download link: https://github.com/MuhammadNasirKhanYousafzai/GenericServiceWithRepository  

    Then this one shows step by step:

    http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle 

    Official document, Generic Repository, in WebApi, Generic Repository Pattern With Web-API And Entity Framework

    http://www.c-sharpcorner.com/blogs/generic-repository-pattern-with-webapi-and-entity-framework

    With regards, Angelina Jolie

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 18, 2018 3:09 AM
  • User616860969 posted

    thank you Angelina Jolie

    Thursday, January 18, 2018 8:45 AM