开发者

c# Interface namespace prefix

Let's suppose I have this interface IRepository

 public interface IRepository
 {
    #region User
        IUser AddUser(String userName, String password, String email);
        IUser FindUser(String identifier);
    #en开发者_运维知识库dregion User

    #region Product
        IProduct AddProduct(...);
        void RemoveProduct(...);
    #endregion Product
}

This is very basic until now and I can do things like

IRepository MyRepository = new Repository(...);
MyRepository.AddUser(...);
MyRepository.RemoveProduct(...);

Ideally, I want something similare to this

public interface IRepository
{
    #region User
        IUser User.User(String userName, String password, String email);
        IUser User.Find(String identifier);
    #endregion User

    #region Product
        IProduct Product.Add(...);
        void Product.Remove(...);
    #endregion Product
}

Unfortunately, this is not accepted in C#. I cannot add these prefixes.

I want to use this kind of namespace to write something similar to this

IRepository MyRepository = new Repository(...);
MyRepository.User.Add(...);
MyRepository.Product.Remove(...);

I don't want to add User and Product objects to my Interface as all I need is a way to improve the reading by this kind of prefixes.

Any idea?

Thanks


Split it into two separate interfaces.

public interface IRepositoryUser
{
    IUser Add(String userName, String password, String email);
    IUser Find(String identifier);
}

public interface IRepositoryProduct
{
    IProduct Add(...);
    void Remove(...);
}

public interface IRepository
{
    IRepositoryUser User { get; }
    IRepositoryProduct Product { get; }
}


What you want, is not legal in the C# syntax as you already realized.
I suggest you stick with your first version. It is clear, readable and conforms to the default naming conventions.


What about defining the Add method in IUser and the Remove method in IProduct and then in your IRepository just define IUser User {get; set;} and IProduct Product {get; set;}.

I believe then your syntax MyRepository.User.Add(...); and MyRepository.Product.Remove(...); will be fine.


Answer if you're talking about repository pattern:

In my humild opinion, and based on my experience, why are you mixing products and users?

Why you want to prefix repository methods?

Just create a repository for any domain object type "IUserRepository" and "IProductRepository" and you won't have such strange requirement.

I don't know if you have this feature in another programming language, but, even if you've it or not, I find a bad design making a repository reponsible of more than a domain object type.


Answer if you're talking about anything else

You would need to separate concerns too.

For example, you would have a data service and you want to manage users and products. I'd suggest you create two managers: UsersManager and ProductsManager.

Both would be associated to a DataServiceManager so you can have something like this:

DataServiceManager dataServiceMan = new DataServiceManager();
dataServiceMan.Users.RegisterUser(...);
dataServiceMan.Products.CreateProduct(...);


I think is better separate logic for performing different Entities in different Repositories classes and in base IRepository keep only common methods like Add Remove Save Update etc. for example

interface IRepository
{
   void Save();
   //other common methods
}
interface IProductRepository:IRepository
 {
   //Methods specified for products
 }


I suppose that you are need explicit interface implementation, like sample below:

    ....................
    interface IOne
    {
       void MethodOne();
    }

    interface ITwo
    {
        void MethodTwo();
    }

    interface IThree
    {
        void MethodThree();
    }

    class TestIf : IOne, ITwo, IThree
    {
        void IThree.MethodThree()
        {
            MessageBox.Show("Method three");                
        }

        void IOne.MethodOne()
        {
            MessageBox.Show("Method one");
        }

        void ITwo.MethodTwo()
        {
            MessageBox.Show("Method two");
        }
    }
    ...................
    private void button1_Click(object sender, EventArgs e)
    {
        TestIf t = new TestIf();

        IOne one = t as IOne;
        ITwo two = t as ITwo;
        IThree three = t as IThree;

        one.MethodOne();
        two.MethodTwo();
        three.MethodThree();

    }

Pay attention, in such approach you can call interface methods only by explicitly convert object to specified interface


The is generally solved using generics so you would have

  interface IRepository<T>

{
   T FindAll()
   T FindSingle(string id)

}

then implement derived classes based on the interface, almost always bloating your interfaces isnt the way to go and breaks ISP

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜