C# 代理和 var 关键字

发布于 2024-07-07 01:45:12 字数 1362 浏览 9 评论 0原文

这个问题与我之前的一篇文章此处相关。 基本上,我想将 DAO 注入到实体中,即

public class User
{
   IUserDAO userDAO;
   public User()
   {
         userDAO = IoCContainer.Resolve<IUserDAO>;
   }
  public User(IUserDAO userDAO)
   {
         this.userDAO = userDAO;
   }
   //Wrapped DAO methods i.e
   public User Save()
   {
       return userDAO.Save(this);
   }

}

如果我的 DAO 中有自定义方法,那么我基本上必须将它们包装在实体对象中。 因此,如果我有一个 IUserDAO.Register(),我就必须创建一个 User.Register() 方法来包装它。

更好的方法是创建一个代理对象,其中 DAO 中的方法动态分配给 User 对象。 所以我可能有这样的东西:

var User = DAOProxyService.Create(new User());
User.Save();

这意味着我可以将 User 实体保留为一个非常愚蠢的类,适合通过网络传输数据,但也神奇地给它一堆 DAO 方法。

但这远远超出了我的舒适区,我想知道我需要什么才能实现这一目标? 我可以使用 Castles 动态代理吗? C# 编译器是否能够处理这个问题并了解动态添加的方法?

如果这是废话,请随时告诉我。

编辑:

我们需要以某种方式在编译时将 DAOProxyService.Create() 声明为返回 User 对象。 这可以通过泛型来完成。

这并不完全正确,我想要返回的不是 User 对象,而是具有动态添加 UserDAO 方法的 User 对象。 由于此类没有在任何地方定义,编译器将不知道如何使用它。

我本质上返回的是一个新对象,如下所示: User : IUserDAO,所以我想我可以根据需要进行转换。 但这看起来很混乱。

看起来我正在寻找的内容与此类似: 混入

This question is related to a previous post of mine Here. Basically, I want to inject a DAO into an entity i.e.

public class User
{
   IUserDAO userDAO;
   public User()
   {
         userDAO = IoCContainer.Resolve<IUserDAO>;
   }
  public User(IUserDAO userDAO)
   {
         this.userDAO = userDAO;
   }
   //Wrapped DAO methods i.e
   public User Save()
   {
       return userDAO.Save(this);
   }

}

Here if I had a custom methods in my DAO then I basically have to wrap them in the entity object. So if I had a IUserDAO.Register() I would then have to create a User.Register() method to wrap it.

What would be better is to create a proxy object where the methods from the DAO are dynamically assign to the User object. So I may have something that looks like this:

var User = DAOProxyService.Create(new User());
User.Save();

This would mean that I can keep the User entity as a pretty dumb class suitable for data transfer over the wire, but also magically give it a bunch of DAO methods.

This is very much out of my confort zone though, and I wondered what I would need to accomplish this? Could I use Castles Dynamic proxy? Also would the C# compiler be able to cope with this and know about the dynamically added methods?

Feel free to let me know if this is nonsense.

EDIT:

What we need to do it somehow declare DAOProxyService.Create() as returning a User object -- at compile time. This can be done with generics.

This isnt quite true, what I want to return isn't a User object but a User object with dynamically added UserDAO methods. As this class isn't defnied anywhere the compiler will not know what to make of it.

What I am essentially returning is a new object that looks like: User : IUserDAO, so I guess I could cast as required. But this seems messy.

Looks like what I am looking for is similar to this: Mixins

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-07-14 01:45:12

我最初想说的是你所要求的不起作用。 但通过一些调整,我们也许能够让它发挥作用。

var 只是一个编译器功能。 当你说。

 var x = GetSomeValue();

编译器说“‘GetSomeValue’被定义为返回一个字符串,因此程序员必须编写‘string x = GetSomeValue();’”。 请注意,编译器是这么说的; 此更改是在编译时完成的。

您想要定义一个类(DAOProxyService),它本质上返回一个对象。 这可以工作,但“var User”将与“Object user”相同。

我们需要以某种方式在编译时将 DAOProxyService.Create() 声明为返回 User 对象。 这可以通过泛型来完成:

class DAOProxyService
{
     static DAOProxyService<T> Create<T>(T obj) { ......} 
}

I was initially going to say what you ask cannot work. But with some tweaking, we might be able to get it to work.

var is just a compiler feature. When you say.

 var x = GetSomeValue();

the compiler says "'GetSomeValue' is defined as returning a string, so the programmer must of meant to write 'string x = GetSomeValue();'". Note that the compiler says this; this change is done at compile time.

You want to define a class (DAOProxyService) which essentially returns an Object. This will work, but "var User" would be the same as "Object user".

What we need to do it somehow declare DAOProxyService.Create() as returning a User object -- at compile time. This can be done with generics:

class DAOProxyService
{
     static DAOProxyService<T> Create<T>(T obj) { ......} 
}
哭了丶谁疼 2024-07-14 01:45:12

它不是完全自动的,但您可以考虑使用 Oleg Sych 方法的变体来生成装饰器类。 每当 IUserDAO 更改(新方法等)时,只需重新生成文件。 比手动维护更好:-)

http://www.olegsych.com/2007/12/how-to-use-t4-to-generate-decorator-classes/

It's not entirely automatic, but you might consider using a variation of Oleg Sych's method for generating decorator classes. Whenever IUserDAO changes (new method, etc) just regenerate the file. Better than maintaining it manually :-)

http://www.olegsych.com/2007/12/how-to-use-t4-to-generate-decorator-classes/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文