-1

interface:

public interface IAuthenticationRepository
    {
        Task<IEnumerable<dynamic>> GetMenuItems(string groupIds);
    }

class:

 public class AuthenticationRepository : Base, IAuthenticationRepository
    {
        public AuthenticationRepository(string sqlConnRep) : base(sqlConnRep) { }

        public async Task<IEnumerable<dynamic>> GetMenuItems(string groupIds)
        {
            var menuAndFunctions = $@"select gfm.GroupId, fmm.MenuId, cm.MenuType, cm.MenuName, fmm.FunctionId,cf.FunctionName, 
                                    cf.Description, cf.IsDefault from FunctionMenuMapping fmm 
                                    inner join CoCMenu cm 
                                    on cm.Id = fmm.MenuId 
                                    inner join CoCFunction cf 
                                    on cf.Id = fmm.FunctionId 
                                    inner join GroupFunctionMapping gfm 
                                    on gfm.FunctionId = fmm.FunctionId 
                                    where gfm.GroupId in (" + groupIds + ")";
            return await ExecQueryDynamic<dynamic>(menuAndFunctions);
        }

Create an instance for AuthenticationRepository class and assigned the instance to the AuthenticationRepository class object

public class AuthenticationLogic : IAuthenticationLogic
    {
            readonly AuthenticationRepository authenticationRepository;
            public AuthenticationLogic( AuthenticationRepository authenticationRepository)
            {
                this.authenticationRepository = new AuthenticationRepository("");
            }
    }

Create an instance for AuthenticationRepository class and assigned the instance to IAuthenticationRepository interface

public class AuthenticationLogic : IAuthenticationLogic
    {
                readonly IAuthenticationRepository IauthenticationRepository;
                public AuthenticationLogic(IAuthenticationRepository IauthenticationRepository)
                {
                    this.IauthenticationRepository = new AuthenticationRepository("");
                }
    }

What is the difference between them? both are doing the same. Is any permanence difference for it? and which one is best approach?

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • Interface is not a class. It is just a object for reading and writing data to an existing class. – jdweng Feb 10 '18 at 09:18
  • @jdweng But class also doing this same right? then i hope we don't need interface for this scenario. Is am correct? – Ramesh Rajendran Feb 10 '18 at 09:19
  • The interface is just a tool to make it easier to transfer data from the class when you access more than one property. It is not needed. You always can get the properties of the class one at a time. – jdweng Feb 10 '18 at 09:25

2 Answers2

1

An interface is a contract it allows you to work with abstractions rather than concrete implementations, meaning your code is much more decoupled and will be more reusable and testable. The second version is better as passing in the interface means your code is dependent only on the fact that the object passed in meets the 'contract' provided by that interface. The constructor should not care what the actually object is as long it meets the requirements. This allows you to change implementation details and types in the future but as long as you still fulfill that interface you don't have to change that code

Dave
  • 2,829
  • 3
  • 17
  • 44
1

Interfaces are used to hide the implementation in the places where you use the classes. The class using it only knows the interface and nothing about its implementation. This is often used in combination with dependency injection, where all your properties are passed in via the constructor as interfaces and you register at one point in your application which implementation should be used for which interface. This allows for easy testing as in your unit tests you can create a test implementation of the class to pass into the class your testing so you are only testing the class itself and none of it's dependencies. This is known as mocking and there are frameworks that automatically create mocks for you.

David Sherman
  • 320
  • 2
  • 9